Skip to content
This repository was archived by the owner on Nov 16, 2025. It is now read-only.

Commit 5f56e4b

Browse files
committed
Actually postprocess the raw headers into an object for ease of use. And test the headers
1 parent 2cadcf1 commit 5f56e4b

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

index.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ module.exports = {
4646
// Data goes in one side, comes out the other. One of the simplest streams.
4747
var response = new stream.PassThrough();
4848

49-
response.headers = {}; // Look! Fake headers!
49+
response.headers = {};
50+
// We will postprocess the headers later into this object.
5051

5152
// A simple list of the raw header lines. We'll do more with them later,
5253
// but sometimes callers want to see the details.
@@ -141,6 +142,9 @@ module.exports = {
141142
// this bit, the next spin of the while loop will do the right thing.
142143
inBody = true;
143144

145+
// We have a complete set of headers, so let's post-process them into the
146+
// more useful response.headers object.
147+
completeHeaders();
144148
}
145149
} else {
146150
// No \r?\n, so we must have partial data, and just save it for the next go round.
@@ -160,5 +164,12 @@ module.exports = {
160164
// body. Because that's what we designed, right?
161165

162166
return response; // Not a terrible interface. Headers in response.headers, body as the stream.
167+
168+
function completeHeaders() {
169+
response.rawHeaders.forEach(function (header) {
170+
var pair = header.split(/: ?/, 2);
171+
response.headers[pair[0].toLowerCase()] = pair[1];
172+
});
173+
}
163174
}
164175
}

test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ tap.test('makes a simple GET request', function (t) {
3131
t.ok(res.headers, 'We get a headers object');
3232
t.ok(res.rawHeaders, 'We get a rawHeaders array');
3333
t.ok(res.rawHeaders.length, "We got headers in the array");
34-
console.warn(res.rawHeaders); // Let's see our work!
34+
35+
t.equal(res.headers['content-length'], "5", "Got a parsed content-length header");
3536
t.equal(data.toString(), 'Hello', 'Data is just the body');
3637
t.end();
3738
}));

0 commit comments

Comments
 (0)