Skip to content

Commit

Permalink
Added dynamic endstream (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
parthverma1 authored Aug 1, 2024
1 parent 6c0f5ab commit 85d5f85
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/http2/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class Http2Request extends EventEmitter {
return this.stream.write(chunk)
}

_flushHeaders () {
_flushHeaders (endStream = false) {
if (this[kHeadersFlushed]) {
throw new Error('Headers already flushed')
}
Expand All @@ -174,7 +174,7 @@ class Http2Request extends EventEmitter {
.filter(([key]) => !connectionHeaders.includes(key.toLowerCase()))
)

this.stream = this._client.request(this.requestHeaders, { endStream: false })
this.stream = this._client.request(this.requestHeaders, {endStream})

this.registerListeners()

Expand Down Expand Up @@ -210,7 +210,7 @@ class Http2Request extends EventEmitter {

end () {
if (!this[kHeadersFlushed]) {
this._flushHeaders()
this._flushHeaders(true)
}
this.stream.end()

Expand Down Expand Up @@ -317,7 +317,7 @@ class ResponseProxy extends EventEmitter {
}

get headers () {
return this.response
return Object.fromEntries(Object.entries(this.response))
}

pause () {
Expand Down
4 changes: 4 additions & 0 deletions request.js
Original file line number Diff line number Diff line change
Expand Up @@ -2008,6 +2008,10 @@ Request.prototype.end = function (chunk) {
// Reference to request, so if _reqResInfo is updated (in case of redirects), we still can update the headers
const request = self._reqResInfo.request
Promise.resolve(self.req._header).then(function (header) {
if (!header) {
request.headers = []
return
}
request.headers = parseRequestHeaders(header)
})
}
Expand Down
63 changes: 63 additions & 0 deletions tests/test-body-http2.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,69 @@ addTest('testPutMultipartPostambleCRLF', {
]
})

tape('test Delete Request', function (t) {
s.on('/', function (req, res) {
req.pipe(res)
})

request(
{
uri: 'https://localhost:' + s.port,
method: 'DELETE',
strictSSL: false,
protocolVersion: 'http2',
body: 'test-data'
},
function (err, res, body) {
t.error(err) // defaults to 'application/octet-stream' content-type
t.equal(res.statusCode, 200)
t.equal(body.toString(), 'test-data')
s.removeAllListeners('/')
t.end()
}
)
})

tape('test HEAD Request', function (t) {
request(
{
uri: 'https://github.com',
method: 'HEAD',
strictSSL: false,
protocolVersion: 'http2',
body: 'test-data'
},
function (err, res, body) {
t.error(err)
t.equal(res.statusCode, 400)
t.end()
}
)
})

tape('test GET Request with body', function (t) {
s.on('/', function (req, res) {
req.pipe(res)
})

request(
{
uri: 'https://localhost:' + s.port,
method: 'GET',
strictSSL: false,
protocolVersion: 'http2',
body: 'test-data'
},
function (err, res, body) {
t.error(err)
t.equal(res.statusCode, 200)
t.equal(body.toString(), 'test-data')
s.removeAllListeners('/')
t.end()
}
)
})

tape('testBinaryFile', function (t) {
s.on('/', function (req, res) {
req.pipe(res)
Expand Down

0 comments on commit 85d5f85

Please sign in to comment.