lundi 4 février 2019

Supertest returns 301 using express app. App only takes hosted server

Hi I am using supertest to test my Node js express server application.

Here is what I am trying to achieve.

let request = require('supertest');
let app = require('./server.js');

request(app).get("/api").then(data=>{//*do something here*//});

However, I am getting 301 Moved Permanently

If I actually start my server on port 8008, then change test to

let request = require('supertest');
let app = require('./server.js');
let agent = request.agent('localhost:8008');

agent.get("/api").then(data=>{//*do something here*//});

Then I get correct api responses as I expect.

Is there a way to make it work and get 200 response by using request(app) instead of using localhost:8008?

I will be running tests as part of continuous integration and I don't have full control of testing environment that I won't be able to run testing server each to have access to localhost.

Thanks.

Aucun commentaire:

Enregistrer un commentaire