Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expectBody not working as expected #457

Open
matt212 opened this issue Jul 29, 2022 · 11 comments
Open

expectBody not working as expected #457

matt212 opened this issue Jul 29, 2022 · 11 comments

Comments

@matt212
Copy link

matt212 commented Jul 29, 2022

Hi @mcollina
my version "autocannon": "^7.9.0",
my codebase

let customResponse= customResponse

  url: 'http://localhost:3011/employees/api/searchtype/',
  amount: 10,
  connections: 1,
  pipelining: 1,
  //duration: 60,
  method: "POST",
  debug: true,
  expectBody: customResponse,
  headers: { "Content-Type": "application/json; charset=UTF-8", 'x-access-token': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE2NTkxMjMxMTd9.xavjahsbNggTSDBu6Vmsxzz6olPMXHTPggkFForZqaU' },
  body: JSON.stringify(customRequest),

}, (err, result) => {

  console.log(err);
  if (err != null) return; // or do some error handling
  handleResults(result);
})

The response which i am getting from api response is below
API Response

and when I compare it http://www.jsondiff.com/ it was identical
Problem statement

when i run the above i am getting mismatched results which should not happen if both the customResponse and API Response are identical

Any pointers on how to tackle this issue

@mcollina
Copy link
Owner

I don't understand the question

@matt212
Copy link
Author

matt212 commented Jul 29, 2022

@mcollina
sorry updated the above with problem statement
ok the issue is i am getting mismatched in my output even after responses are identical

@mcollina
Copy link
Owner

Can you provide steps to reproduce? We often need a reproducible example, e.g. some code that allows someone else to recreate your problem by just copying and pasting it. If it involves more than a couple of different file, create a new repository on GitHub and add a link to that.

@matt212
Copy link
Author

matt212 commented Jul 30, 2022

Hi @mcollina
approach-1
just out of box thinking can't you just compare below
let customResponse= customResponse
let APIResponse=API Response
in your expectbody test case to use what is issue there.
because that is root cause of mismatch message
approach-2
and in any case in case of reproduction perspective you want github repo to run app since it is api i would require to setup those up,
give me sometime on that in the meanwhile could proceed with approach-1
thanks ,

@mcollina
Copy link
Owner

I still don't understand your problem, so I can't help. If there is a bug, a contribution fix would be awesome.

@matt212
Copy link
Author

matt212 commented Jul 30, 2022

@mcollina let me create repo and provide you details like run and steps to reproduce
so that you can reproduce the issue.
give me sometime on that

@matt212
Copy link
Author

matt212 commented Jul 30, 2022

Hi @mcollina
I have put together this repo with all required details to reproduce the issue

NodeJS_Fastify_AutoCannon

All instructions are spelled out in readme file for above repo

Cheers !

@mcollina
Copy link
Owner

Thanks, will take a look when I have a chance. A PR to fix would be highly welcomed.

matt212 added a commit to matt212/autocannon that referenced this issue Jul 31, 2022
@matt212
Copy link
Author

matt212 commented Jul 31, 2022

Hi @mcollina
I am trying to Perform PR on this issue below is progress
Forked repo Feature branch
https://github.com/matt212/autocannon/tree/issues_457

I am trying to PR for #457
and code is patched

filename : httpClient
path : autocannon\lib
line : 14 and 109

unit test code for evaluating is in progress the issue is below

location : run.test.js Line no 330

TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer or Uint8Array. Received an instance of Object
so i move past the unit test

and I had created reproduction file in samples folder

file name customise-expectBody

the main problem there is also i cannot send json object from mock httpserver
because res.end or res.write always
supports string not objects
so if anyone can help me figure out how to pass object in mockhttpserver i am gold
btw i tried setting
res.setHeader("Content-Type", "application/json");
still the resp.body is typeof string not object

@matt212
Copy link
Author

matt212 commented Jul 31, 2022

Hi @mcollina

Just as an FYR
Temp WorkAround for this Bug is use expectbody as string only

expectBody: JSON.stringify(<tocompareJsonObjectResponse>);

@matt212
Copy link
Author

matt212 commented Jul 31, 2022

Hi @mcollina

there is deeper problem wherein resp.body always is string so my below code will only work
if resp.body follows actual api response type whether it is obj or string


filename : httpClient
path : autocannon\lib
line :  109

if (!isFn && this.opts.expectBody && this.opts.expectBody !== resp.body) {
        if (typeof resp.body === 'object') {         
          if (!compareObjects(this.opts.expectBody, resp.body) === false) {
            return this.emit('mismatch', resp.body)
          }
        } else if (typeof resp.body === 'string') {
          
          return this.emit('mismatch', resp.body)
        }
      }
    }

further investigation points to below
const HTTPParser = require('http-parser-js').HTTPParser
which parses and it seems convert all responses to string
again that is as per my investigation correct me if i am wrong

matt212 added a commit to matt212/Nodejs_Postgresql_VanillaJS_Fastify that referenced this issue Jul 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants