Skip to content

Commit

Permalink
Actually postprocess the raw headers into an object for ease of use. …
Browse files Browse the repository at this point in the history
…And test the headers
  • Loading branch information
aredridel committed Oct 25, 2015
1 parent 2cadcf1 commit 5f56e4b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
13 changes: 12 additions & 1 deletion index.js
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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];
});
}
}
}
3 changes: 2 additions & 1 deletion test.js
Expand Up @@ -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();
}));
Expand Down

0 comments on commit 5f56e4b

Please sign in to comment.