Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

rawHeaders is an empty array in the response #354

Closed
mtt87 opened this issue Aug 3, 2015 · 6 comments
Closed

rawHeaders is an empty array in the response #354

mtt87 opened this issue Aug 3, 2015 · 6 comments

Comments

@mtt87
Copy link

mtt87 commented Aug 3, 2015

Hi! 馃槃
I'm using Popsicle https://github.com/blakeembrey/popsicle and using Nock for testing but seems like there is a problem, Popsicle expects rawHeaders and Nock is setting this array to empty so I can't test response headers.

Is this a bug or there is a reason behind this choice?

I think Nock shouldn't set this array to empty.

Node API:
https://nodejs.org/api/http.html#http_message_headers

Thanks 馃槃

@pgte
Copy link
Member

pgte commented Aug 3, 2015

@mtt87 if it's part of the public API then there's no reason for Nock not to include it.

PR is welcome :)

@mtt87
Copy link
Author

mtt87 commented Aug 4, 2015

@pgte I'm not that good but I'm trying, can you point me in the right direction? 馃槃

As far as I can see you deal with the headers here:
https://github.com/pgte/nock/blob/master/lib/scope.js#L104-L113
and I tried to use a this.rawHeaders but no success so far 馃槮

if (headers !== undefined) {
  this.headers = {};
  this.rawHeaders = [];

  // makes sure all keys in headers are in lower case
  for (var key2 in headers) {
    if (headers.hasOwnProperty(key2)) {
      this.headers[key2.toLowerCase()] = headers[key2];
    }
  }

  // this is what I've added
  for (var key3 in this.headers) {
    this.rawHeaders.push(key3, headers[key3]);
  }

}

Thanks

@mtt87
Copy link
Author

mtt87 commented Aug 4, 2015

I think I've found it and seems like it's working for me
request_overrider.js

var response = new IncomingMessage(new EventEmitter()),
      requestBodyBuffers = [],
      originalInterceptors = interceptors,
      aborted,
      emitError,
      end,
      ended,
      headers,
      rawHeaders,
      keys,
      key,
      i,
      l;

...

response.headers = interceptor.headers || {};
response.rawHeaders = interceptor.rawHeaders || {};

I'm gonna write a test now 馃槃

@mtt87
Copy link
Author

mtt87 commented Aug 5, 2015

Problem with this test

test("reply headers as function work", function(t) {
  var scope = nock('http://example.com')
  .get('/')
  .reply(200, 'boo!', {
    'X-My-Headers': function (req, res, body) {
      return body.toString();
    }
  });

  var req = http.get({
    host: 'example.com',
    path: '/'
  }, function (res) {
    t.equivalent(res.headers, {'x-my-headers': 'boo!'}); // 66
    t.equivalent(res.rawHeaders, ['x-my-headers', 'boo!']);  // 67
    t.end();
  });
});
# reply headers as function work
ok 66 should be equivalent
not ok 67 should be equivalent

Because

res.rawHeaders = [ 'x-my-headers', [Function] ]

Can you help me and tell me where are the headers as function converted into value?

@pgte
Copy link
Member

pgte commented Aug 5, 2015

Fix landed on v2.10.0

@lock
Copy link

lock bot commented Sep 13, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue and add a reference to this one if it鈥檚 related. Thank you!

@lock lock bot locked as resolved and limited conversation to collaborators Sep 13, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants