Permalink
Browse files

Actually postprocess the raw headers into an object for ease of use. …

…And test the headers
  • Loading branch information...
1 parent 2cadcf1 commit 5f56e4b637aa8e5d30f4c2a088cb2e71a025a9ea @aredridel committed Oct 24, 2015
Showing with 14 additions and 2 deletions.
  1. +12 −1 index.js
  2. +2 −1 test.js
View
@@ -46,7 +46,8 @@ module.exports = {
// Data goes in one side, comes out the other. One of the simplest streams.
var response = new stream.PassThrough();
- response.headers = {}; // Look! Fake headers!
+ response.headers = {};
+ // We will postprocess the headers later into this object.
// A simple list of the raw header lines. We'll do more with them later,
// but sometimes callers want to see the details.
@@ -141,6 +142,9 @@ module.exports = {
// this bit, the next spin of the while loop will do the right thing.
inBody = true;
+ // We have a complete set of headers, so let's post-process them into the
+ // more useful response.headers object.
+ completeHeaders();
}
} else {
// No \r?\n, so we must have partial data, and just save it for the next go round.
@@ -160,5 +164,12 @@ module.exports = {
// body. Because that's what we designed, right?
return response; // Not a terrible interface. Headers in response.headers, body as the stream.
+
+ function completeHeaders() {
+ response.rawHeaders.forEach(function (header) {
+ var pair = header.split(/: ?/, 2);
+ response.headers[pair[0].toLowerCase()] = pair[1];
+ });
+ }
}
}
View
@@ -31,7 +31,8 @@ tap.test('makes a simple GET request', function (t) {
t.ok(res.headers, 'We get a headers object');
t.ok(res.rawHeaders, 'We get a rawHeaders array');
t.ok(res.rawHeaders.length, "We got headers in the array");
- console.warn(res.rawHeaders); // Let's see our work!
+
+ t.equal(res.headers['content-length'], "5", "Got a parsed content-length header");
t.equal(data.toString(), 'Hello', 'Data is just the body');
t.end();
}));

0 comments on commit 5f56e4b

Please sign in to comment.