Supertest: Init request with headers

Created on 5 Jun 2019  路  2Comments  路  Source: visionmedia/supertest

Hi,
I would like to prebuild using a function my request to prevent code duplication, here a basic example

const request = require('supertest');
const initRequest = () => request(app).set('X-Custom-Header', 'test')
initRequest().get('/blog')
initRequest().get('/contact')

To prevent doing

const request = require('supertest');
request(app).get('/blog').set('X-Custom-Header', 'test').get('/blog')
request(app).get('/blog').set('X-Custom-Header', 'test').get('/contact')

Most helpful comment

@ajouve Yes, you could try using a superagent plugin for that.

TDLR:

function custom(request) {
  request.set('X-CUSTOM-HEADER', 'test');
  // other shared configuration
}

request(app).get('/blog').use(custom);

All 2 comments

@ajouve Yes, you could try using a superagent plugin for that.

TDLR:

function custom(request) {
  request.set('X-CUSTOM-HEADER', 'test');
  // other shared configuration
}

request(app).get('/blog').use(custom);

I close it.

Thanks @jonathansamines and @pcraig3 for your contribution. 馃檹

Was this page helpful?
0 / 5 - 0 ratings

Related issues

EverettQuebral picture EverettQuebral  路  3Comments

hacker0limbo picture hacker0limbo  路  3Comments

nareshbhatia picture nareshbhatia  路  6Comments

nitrocode picture nitrocode  路  4Comments

mickaeltr picture mickaeltr  路  3Comments