Skip to content

Commit

Permalink
kOnHeaders bugfix (#49)
Browse files Browse the repository at this point in the history
* kOnHeaders bugfix

* trailer test
  • Loading branch information
David Mark Clements authored Jul 6, 2016
1 parent 58f0e30 commit c2c200c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/httpClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function Client (opts) {
}

this.timeoutTicker = retimer(handleTimeout, this.timeout)

this.parser[HTTPParser.kOnHeaders] = () => {}
this.parser[HTTPParser.kOnHeadersComplete] = (opts) => {
this.emit('headers', opts)
this.resData[this.cer].headers = opts
Expand Down
18 changes: 18 additions & 0 deletions test/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,23 @@ function startServer () {
return server
}

function startTrailerServer () {
const server = http.createServer(handle)

function handle (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain', 'Trailer': 'Content-MD5' })
res.write('hello ')
res.addTrailers({'Content-MD5': '7895bf4b8828b55ceaf47747b4bca667'})
res.end('world')
}

server.listen(0)

server.unref()

return server
}

// this server won't reply to requests
function startTimeoutServer () {
const server = http.createServer(() => {})
Expand Down Expand Up @@ -53,3 +70,4 @@ function startHttpsServer () {
module.exports.startServer = startServer
module.exports.startTimeoutServer = startTimeoutServer
module.exports.startHttpsServer = startHttpsServer
module.exports.startTrailerServer = startTrailerServer
19 changes: 19 additions & 0 deletions test/httpClient.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const helper = require('./helper')
const server = helper.startServer()
const timeoutServer = helper.startTimeoutServer()
const httpsServer = helper.startHttpsServer()
const trailerServer = helper.startTrailerServer()
const bl = require('bl')

test('client calls a server twice', (t) => {
Expand Down Expand Up @@ -81,6 +82,24 @@ test('client supports custom headers', (t) => {
})
})

test('client supports response trailers', (t) => {
t.plan(3)

const client = new Client(trailerServer.address())
let n = 0
client.on('body', (raw) => {
if (++n === 1) {
// trailer value
t.ok(/7895bf4b8828b55ceaf47747b4bca667/.test(raw.toString()))
}
})
client.on('response', (statusCode, length) => {
t.equal(statusCode, 200, 'status code matches')
t.ok(length > 'hello world'.length, 'length includes the headers')
client.destroy()
})
})

;[
'DELETE',
'POST',
Expand Down

0 comments on commit c2c200c

Please sign in to comment.