chai-http
chai-http copied to clipboard
All requests with chai-http and express turn into GET regardless of method used.
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?
Just noticed the same issue, anyone has any thoughts?
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?
I am having the same issue but i am not using https at all. anyone got a fix?
What happens when you curl
with a post to your server? Could it be a CORS issue or a redirect issue?
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.
@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.
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
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.
POST_ENDPOINT_PATH may be incorrect
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)
Any update on this issue? I have the same problem.
I'm having the same issue, for a simple endpoint that returns {message: "it works"}
This route works fine on postman.
@xmaster484 did you find the solution to this issue?
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!?
This issue is prevalent, and has yet to be solved. it better to move to another testing framework