chai-http icon indicating copy to clipboard operation
chai-http copied to clipboard

All requests with chai-http and express turn into GET regardless of method used.

Open xmaster484 opened this issue 7 years ago • 15 comments

Hi everyone, I was wondering if anyone else has experienced this bug, or knows why it might be happening. I am currently setting up my testing framework for an Express server and I decided to go with Mocha/Chai/Chai-Http.

The server itself behaves normally, I can make requests with any of the method types and I receive the appropriate response.

However in my testing suite, all methods I use with chai.request (i.e. GET, POST, DELETE, etc...) get converted to GET.

When I test endpoints that are actually a GET the test passes and behaves like it should. But when I test an endpoints that are anything else, I obviously get 404s because those endpoints don't exist.

Example:

let chai = require('chai');
let chaiHttp = require('chai-http');
let should = chai.should();
let server = require('../../server.api').server;
chai.use(chaiHttp);

describe('Tests', () => {
   let token={ 'test' : 'test' };

   it('Endpoint', (done) => {
       chai.request(server)
           .post(POST_ENDPOINT_PATH)
           .send(token)
           .end((err,res) => {
               res.should.have.status(200);
               res.body.should.be.a('object');
               done();
           })
   })

});

Console log output:

     GET POST_ENDPOINT_PATH 404 10.549 ms - 23
     Uncaught AssertionError: expected { Object (domain, _events, ...) } to have status code 200 but 
     got 
      404
      + expected - actual

      -404
      +200

Any idea why this might be happening?

xmaster484 avatar Aug 22 '17 16:08 xmaster484

Just noticed the same issue, anyone has any thoughts?

CharismaRise avatar Aug 22 '17 18:08 CharismaRise

It seems like this would be a case if you create both HTTP and HTTPS servers under Express and chai-http would pick HTTP by default. I am having the same issue where I pass in my Express instance with both servers but chai-http doesn't let me pick which server to use. Any support for HTTPS?

CharismaRise avatar Aug 22 '17 21:08 CharismaRise

I am having the same issue but i am not using https at all. anyone got a fix?

saqibbilal avatar Mar 13 '18 07:03 saqibbilal

What happens when you curl with a post to your server? Could it be a CORS issue or a redirect issue?

keithamus avatar Mar 13 '18 12:03 keithamus

I found the issue and it is not related to chai i suppose. What happened is that I was running another project in a separate cmnder window and using ngrok on it for it to be available for mobile team. after a lot of R&D and being unable to find the issue, I happened to spot the ngrok window and think to myself,"what the...". I stopped ngrok(not the other project, it was still running) and tried my tests again and all of them passed.

saqibbilal avatar Mar 13 '18 15:03 saqibbilal

@keithamus I am pretty sure I am not using https and I am facing same issue as well. Here is the complete setup in my StackOverflow questions : https://stackoverflow.com/questions/49461144/chai-http-always-returns-404-error-code. I can seem to get 200 return code when I hit the api from Postman or any browser. Any guidance will be greatly appreciated.

newkillerbeast2017 avatar Mar 27 '18 16:03 newkillerbeast2017

Set the content-type and accept headers and see what happens.

chai.request(server)
        .post(POST_ENDPOINT_PATH)
        .send(token)
        .set('Content-Type', 'application/json')
        .set('Accept', 'application/json')
        ...

In addition your POST_ENDPOINT_PATH should be relative not absolute.

correct: /myendpoint incorrect: http://localhost:3848/api/myendpoint

Strandedpirate avatar May 09 '18 04:05 Strandedpirate

Any update on this issue? I have the same problem. When I make a 'post' call to a PHP server, the $_SERVER['REQUEST_METHOD'] is set to 'GET' not 'POST'. Meanwhile my web application makes post calls and they work as expected. Also, when I call the post call from any rest client, it works as expected. Here example:

chai.use(chaiHttp);
chai.request('https://www.mydomainl.com/api/1')
    .post('/user/accounts')
    .query({email: '[email protected]'})
    .end(function (err, res) {
    });

Please let me know if I'm doing something wrong. Thanks.

sayoub avatar Dec 29 '18 06:12 sayoub

POST_ENDPOINT_PATH may be incorrect

emrebaris9 avatar Feb 12 '19 10:02 emrebaris9

In my case the routes weren't initialised before server object was exported for chai to use (as explained here: https://stackoverflow.com/a/44185482/2498874)

denizozger avatar Oct 07 '19 13:10 denizozger

Any update on this issue? I have the same problem.

MsShivaniChauhan avatar Oct 05 '20 11:10 MsShivaniChauhan

image

I'm having the same issue, for a simple endpoint that returns {message: "it works"}

This route works fine on postman.

Emyboy avatar May 08 '21 12:05 Emyboy

@xmaster484 did you find the solution to this issue?

Emyboy avatar May 17 '21 21:05 Emyboy

It seems like this would be a case if you create both HTTP and HTTPS servers under Express and chai-http would pick HTTP by default. I am having the same issue where I pass in my Express instance with both servers but chai-http doesn't let me pick which server to use. Any support for HTTPS?

have you found any solution, i get the same error!?

DJ8140 avatar Jun 03 '21 08:06 DJ8140

This issue is prevalent, and has yet to be solved. it better to move to another testing framework

iamgabrielsoft avatar Aug 23 '21 13:08 iamgabrielsoft