I am using Supertest with Mocha to test an API developed with Node JS.
And I want to do a lót of different tests on the API. With almost all of them I have to set Authorization and Content-Type headers again (because the API requires them for this test).
it('Creation without an email address should fail and return error code 50040', function(done) { request .post('/mpl/entities') .set('Authorization', 'Token 1234567890') //set header for this test .set('Content-Type', 'application/json') //set header for this test .send({ firstname: "test" }) .expect('Content-Type', /json/) .expect(500) .expect(anErrorCode('50040')) .end(done); }); it('Creation with a duplicate email address should fail and return error code 50086', function(done) { request .post('/mpl/entities') .set('Authorization', 'Token 1234567890') //<-- again .set('Content-Type', 'application/json') //<-- again, I'm getting tired .send({ email: "[email protected]" }) .expect('Content-Type', /json/) .expect(500) .expect(anErrorCode('50086')) .end(done); });
Can I create an alternative request with those headers set by default?
SuperTest is an HTTP assertions library that allows you to test your Node. js HTTP servers. It is built on top of SuperAgent library, wich is an HTTP client for Node. js.
The error "Error: Can't set headers after they are sent." means that you're already in the Body or Finished state, but some function tried to set a header or statusCode. When you see this error, try to look for anything that tries to send a header after some of the body has already been written.
SuperTest is a Node. js library that helps developers test APIs. It extends another library called superagent, a JavaScript HTTP client for Node. js and the browser. Developers can use SuperTest as a standalone library or with JavaScript testing frameworks like Mocha or Jest.
If i remember correctly in superagent one can pass a hash to set
agent.set({key:value,key2:value2})
let me know if it doesnt work with supertest.
You could use a common routine to build your "default" headers as an object and pass them to the request:
//# file:config.js var config = { baseUrl: "http://localhost:8080", authorization: { "Authorization":"authvalue" } } // Content-Type left out because supertest will use Content-Type json when you use the appropriate method module.exports = config;
And now in your test.js:
//# file:test.js var request = require("supertest"); var config = require("./config"); request = request(config.baseUrl+"/api/getTokenValue") //code to get token value from request var commonHeaders = { "authorization":tokenValue, "X-Testing-Value":1, "X-Common-Header":"value" }; describe("testing", function() { it.should('present authorization header to server', function(done) { request.get('/someurl') .set(commonHeaders) .set({"X-TestSpecificHeader":"Value"}) .expect(200,done) //if not authorized you'd get 401 }); it.should('do something else', function(done) { request.get('/someUrl') .set(commonHeaders) .expect(200,done) }); })
Also, if you need to have your app get that token value at runtime (most likely) see this article for using a requested token value that is generated for the tests: https://jaketrent.com/post/authenticated-supertest-tests/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With