Skip to content

inappropriately parsing string response as JSON #37

@dblasher

Description

@dblasher

Noticed Hippie keeps trying to parse responses as JSON when I'm expecting a string back. How can I prevent Hippie from assuming the response is JSON?

My swagger.yml is using OpenAPI 3.0, however your example api.swagger.json is using 2.0, wonder if this has anything to do with it (i.e produces: became content:).

Test Code:

const app = require('../app');
const _ = require('lodash');
const { expect } = require('chai');
const request = require('supertest');
const SwaggerParser = require('swagger-parser');
const hippie = require('hippie-swagger');
const path = require('path');

const parser = new SwaggerParser();

const URLSafeBase64 = require('urlsafe-base64');

let dereferencedSwagger;

parser.dereference(path.join(__dirname, '../swagger/swagger.yml'), (err, api) => {
this.dereferencedSwagger = api;
if (err) throw err;
dereferencedSwagger = api;

return run();
});

…

  describe('AVATAR SAVE', () => {

    it('Should return 200 when provided empty avatar', (done) => {
      //request(app)
      let saveOptions = {
        //validateResponseSchema: false,
       // validateParameterSchema: false,
       // errorOnExtraParameters: false,
       // errorOnExtraHeaderParameters: false
     };
      hippie(app, dereferencedSwagger, saveOptions)
/*     .parser(function(body, fn){
              fn(null, 'ok');
        }) */
        .put('/api/v1/user/avatar')
        .header('Authorization', `Bearer ${this.jwt.token}`)
        .expectStatus(200)
        .end((err, res) => {
          console.log(res.body);
          if (err) throw err;
          done();
        });
    });

Output:

  SAVING AND ACCESSING AVATAR
        AVATAR SAVE
          Should return 200 when provided empty avatar:
    Uncaught SyntaxError: Unexpected token o in JSON at position 0
     at JSON.parse (<anonymous>)
     at Client.exports.json [as parse] (node_modules\hippie\lib\hippie\parsers.js:15:18)
     at Request._callback (node_modules\hippie\lib\hippie\client.js:435:10)
     at Request.self.callback (node_modules\request\request.js:185:22)
     at Request.<anonymous> (node_modules\request\request.js:1157:10)
     at IncomingMessage.<anonymous> (node_modules\request\request.js:1079:12)
     at endReadableNT (_stream_readable.js:1081:12)
     at process._tickCallback (internal/process/next_tick.js:63:19)

Output with .parser(function(body, fn){ fn(null, 'ok'); }) uncommented:

       SAVING AND ACCESSING AVATAR
         AVATAR SAVE
           Should return 200 when provided empty avatar:
     Uncaught Error: Received non-empty response from /api/v1/user/avatar. Expected empty response body because no "schema" property was specified in swagger path.
      at validateBody (node_modules\hippie-swagger\lib\response.js:27:11)
      at Client.response (node_modules\hippie-swagger\lib\response.js:46:5)
      at verify (node_modules\hippie\lib\hippie\client.js:476:5)
      at statusCode (node_modules\hippie\lib\hippie\expect.js:23:5)
      at verify (node_modules\hippie\lib\hippie\client.js:476:5)
      at Client.verify (node_modules\hippie\lib\hippie\client.js:477:5)
      at ..\node_modules\hippie\lib\hippie\client.js:437:12
      at Client.parse (test\avatar.spec.js:212:13)
      at Request._callback (node_modules\hippie\lib\hippie\client.js:435:10)
      at Request.self.callback (node_modules\request\request.js:185:22)
      at Request.<anonymous> (node_modules\request\request.js:1157:10)
      at IncomingMessage.<anonymous> (node_modules\request\request.js:1079:12)
      at endReadableNT (_stream_readable.js:1081:12)
      at process._tickCallback (internal/process/next_tick.js:63:19)

Swagger definition:

/api/v1/user/avatar:
  put:
    summary: Upsert the users avatar
    tags:
      - user
    requestBody:
      content:
        application/json:
          schema:
            type: object
            properties:
              encodedAvatar:
                $ref: '#/components/schemas/EncodedAvatar'
    responses:
      '200':
        description: OK
        content:
          text/html:
            schema:
              type: string

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions