With Supertest, can I create an alternative request with some headers set by default?
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?
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/
You can use the library superagent-defaults
as follows:
installation
npm install --save-dev supertest superagent-defaults
usage
var defaults = require('superagent-defaults');
var supertest = require('supertest');
var request = defaults(supertest(app)); // or url
// set the default headers
request.set(commonHeaders);
// use as usually
version
- supertest v3.0.0
- superagent-defaults v0.1.14
What I use to do is something like
First I create a config file to store the default Headers,
Config File:
//# file: defaults.js
var defaults = {
l5dDtab: "SomeValue",
xSignature: true,
... more headers here
}
module.exports = defaults;
Then, on each test file where I use supertest I do this:
var defaults = require('./defaults.js');
var supertest = require('supertest');
var host = 'http://localhost:8080';
var request = supertest.agent(host)
.set("l5d-dtab", defaults.l5dDtab)
.set("X-Signature", defaults.xSignature);
var response = request.get("SomePath").set("AnotherLocalHeader", "Value");