question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: Getting the headers for tests

See original GitHub issue

I feel like this is out of topic, though. Anyway –

I’m implementing JWT with Axios, and would like to send the Authorization Header through the interceptor. Question, how do I test with Sinon that I am able to send the said header? Which do I spy? Thanks.

Edit: I just checked the API, and the headers doesn’t seem to be covered in the interceptors. In the Interceptors example, is config equal to the whole request API or just the config in it?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
mzabriskiecommented, Apr 14, 2015

I should probably also mention, that you don’t have to use an interceptor for modifying headers. You can use a transformer as well.

axios.get('/some/url', {
  transformRequest: axios.defaults.transformRequest.concat(function (data, headers) {
    headers['Authorization'] = token;
  })
});

Or if you want to use the transformer on every request, not just a single request:

axios.defaults.transformRequest.push(function (data, headers) {
  headers['Authorization'] = token;
});
1reaction
mzabriskiecommented, Apr 14, 2015

This is an example using sinon and jasmine:

describe('jwt', function () {
  beforeEach(function () {
    this.xhr = sinon.useFakeXMLHttpRequest();
    var requests = this.requests = [];
    this.xhr.onCreate = function (xhr) {
      requests.push(xhr);
    }
  });

  afterEach(function () {
    this.xhr.restore();
  });

  it('should have Authorization header', function (done) {
    axios.interceptors.request.use(function (config) {
      config.headers['Authorization'] = 'token';
      return config;
    });

    axios.get('/foo');

    var requests = this.requests;

    setTimeout(function () {
      expect(requests.length).toEqual(1);
      expect(requests[0].requestHeaders['Authorization']).toEqual('token');
      done();
    }, 0);
  });
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

php - Getting status code response headers for unit tests
You can use the http_response_code() function to return the values you set: print_r("Response Code: " . http_response_code());. Result: Response Code: 422.
Read more >
Using Question Type Headers on Tests - Tamarack Software
TestGen supplies a standard question type headers for each of the six main question types: multiple choice, true/false, short answer, essay, matching, and ......
Read more >
Problem with HTTP headers in test - Laracasts
Hi guys, I'm facing a problem with some application tests. I'm using Laravel 5.7. I have a test like this one. Copy Code...
Read more >
Understanding REST Headers and Parameters - SoapUI
Headers are mostly classified as request headers and response headers, know the major request and response headers. You will have to set the...
Read more >
Testing a custom HTTP response header - Pega
Have a question? Get answers now. Visit the Support Center to ask questions, engage in discussions, share ideas, and help others.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found