Skip to content

Commit

Permalink
return copy header from ServerResponse._header
Browse files Browse the repository at this point in the history
  • Loading branch information
sirenkovladd committed Oct 24, 2023
1 parent e5d4b0c commit 2bda234
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
5 changes: 4 additions & 1 deletion lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,10 @@ class Response extends http.ServerResponse {

// Add raw headers
;['Date', 'Connection', 'Transfer-Encoding'].forEach((name) => {
const value = this.getHeader(name)
// TODO change this to use the header getter
const regex = new RegExp('\\r\\n' + name + ': ([^\\r]*)\\r\\n')
const [, value] = this._header.match(regex) || []
// const value = this.getHeader(name)
if (value) {
this._lightMyRequest.headers[name.toLowerCase()] = value
}
Expand Down
21 changes: 17 additions & 4 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const httpMethods = [
]

test('returns non-chunked payload', (t) => {
t.plan(6)
t.plan(7)
const output = 'example.com:8080|/hello'

const dispatch = function (req, res) {
Expand All @@ -42,7 +42,10 @@ test('returns non-chunked payload', (t) => {
t.error(err)
t.equal(res.statusCode, 200)
t.equal(res.statusMessage, 'Super')
t.ok(res.headers.date)
t.strictSame(res.headers, {
date: res.headers.date,
connection: 'keep-alive',
'x-extra': 'hello',
'content-type': 'text/plain',
'content-length': output.length.toString()
Expand All @@ -53,14 +56,17 @@ test('returns non-chunked payload', (t) => {
})

test('returns single buffer payload', (t) => {
t.plan(3)
t.plan(6)
const dispatch = function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.end(req.headers.host + '|' + req.url)
}

inject(dispatch, { url: 'http://example.com:8080/hello' }, (err, res) => {
t.error(err)
t.ok(res.headers.date)
t.ok(res.headers.connection)
t.equal(res.headers['transfer-encoding'], 'chunked')
t.equal(res.payload, 'example.com:8080|/hello')
t.equal(res.rawPayload.toString(), 'example.com:8080|/hello')
})
Expand Down Expand Up @@ -362,7 +368,7 @@ test('includes default https port in host header', (t) => {
})

test('optionally accepts an object as url', (t) => {
t.plan(3)
t.plan(5)
const output = 'example.com:8080|/hello?test=1234'

const dispatch = function (req, res) {
Expand All @@ -382,6 +388,8 @@ test('optionally accepts an object as url', (t) => {

inject(dispatch, { url }, (err, res) => {
t.error(err)
t.ok(res.headers.date)
t.ok(res.headers.connection)
t.notOk(res.headers['transfer-encoding'])
t.equal(res.payload, output)
})
Expand All @@ -401,7 +409,7 @@ test('leaves user-agent unmodified', (t) => {
})

test('returns chunked payload', (t) => {
t.plan(2)
t.plan(5)
const dispatch = function (req, res) {
res.writeHead(200, 'OK')
res.write('a')
Expand All @@ -411,6 +419,9 @@ test('returns chunked payload', (t) => {

inject(dispatch, { method: 'GET', url: '/' }, (err, res) => {
t.error(err)
t.ok(res.headers.date)
t.ok(res.headers.connection)
t.equal(res.headers['transfer-encoding'], 'chunked')
t.equal(res.payload, 'ab')
})
})
Expand Down Expand Up @@ -1629,6 +1640,8 @@ test('correctly handles no string headers', (t) => {
date: date.toString(),
true: 'true',
false: 'false',
connection: 'keep-alive',
'transfer-encoding': 'chunked',
'content-type': 'application/json'
})

Expand Down

0 comments on commit 2bda234

Please sign in to comment.