Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 427
Christiaan Westerbeek Avatar asked May 18 '14 18:05

Christiaan Westerbeek


People also ask

What does SuperTest request do?

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.

Can not set headers after they are sent to client?

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.

What is SuperTest in mocha?

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.


2 Answers

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.

like image 84
mpm Avatar answered Sep 22 '22 06:09

mpm


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/

like image 22
enorl76 Avatar answered Sep 21 '22 06:09

enorl76