Supertest: Unable to set custom headers

Created on 26 Jun 2015  路  4Comments  路  Source: visionmedia/supertest

I am unable to set a custom header
I have token middleware that will verify a jwt token in the 'x-access-token' header. but doing something like this

request(app)
  .put('/user/' + a_user_id)
  .set('x-access-token', myToken)
  .end(callback);

The 'x-access-token' header is never set

Most helpful comment

This seems to be working for me now in supertest 1.2 using superagent 1.8.

Example using mocha with express:

var express = require('express');
var supertest = require('supertest');

var app = express();

app.get('/querystring', function(req, res) {
    console.log(req.headers);
    res.sendStatus(200);
});

describe('Test custom header', function() {
  it.only('sets header', function(done) {
    supertest(app)
      .get('/querystring')
      .set('x-access-token', 'test-token')
      .expect(200)
      .end(function(err, res){
          if (err) return done(err);
          done();
      });
  });
});

Output

{ host: '127.0.0.1:63249',
  'accept-encoding': 'gzip, deflate',
  'user-agent': 'node-superagent/1.8.3',
  'x-access-token': 'test-token',
  connection: 'close' }

All 4 comments

It works, as I'm using it in my everyday :)

PoC: simple php script to show all the headers:

<?php
foreach (getallheaders() as $name => $value) {
    echo "$name: $value\n";
}
?>

and run server: php -S localhost:8000

test.js:

var request = require('../index');


request('http://localhost:8000/')
    .get('test.php')
    .set('x-access-token', 'test-token')
    .end(function(err, res){
        console.log(res.text);
        if (err) throw err;
    });

Output:

Host: localhost:8000
Accept-Encoding: gzip, deflate
User-Agent: node-superagent/1.2.0
x-access-token: Test-token
Connection: close

PS addition, but there is no header in res.headers...

I have the same problem with version 1.1.0. Inspecting the headers with a test server is not a solution for me because i'm testing a nodejs http request event handler without running a server. If i do console.log(req.headers) it doesn't print any of the headers set by me using .set (either standard headers or application specific).

Any workaround on this problem?

This seems to be working for me now in supertest 1.2 using superagent 1.8.

Example using mocha with express:

var express = require('express');
var supertest = require('supertest');

var app = express();

app.get('/querystring', function(req, res) {
    console.log(req.headers);
    res.sendStatus(200);
});

describe('Test custom header', function() {
  it.only('sets header', function(done) {
    supertest(app)
      .get('/querystring')
      .set('x-access-token', 'test-token')
      .expect(200)
      .end(function(err, res){
          if (err) return done(err);
          done();
      });
  });
});

Output

{ host: '127.0.0.1:63249',
  'accept-encoding': 'gzip, deflate',
  'user-agent': 'node-superagent/1.8.3',
  'x-access-token': 'test-token',
  connection: 'close' }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

mickaeltr picture mickaeltr  路  3Comments

DeaconDesperado picture DeaconDesperado  路  6Comments

ghost picture ghost  路  5Comments

jon301 picture jon301  路  5Comments

ahri picture ahri  路  6Comments