-
-
Notifications
You must be signed in to change notification settings - Fork 984
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: replaced _implicitHeader #908
Open
lamweili
wants to merge
5
commits into
expressjs:master
Choose a base branch
from
lamweili:refactor/use_writeHead_instead_of_implicitHeader
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
92d05cc
Added test cases (http2) that will fail if using undocumented `res._i…
lamweili df7163e
Replaced the usage of undocumented http API with the proper writeHead…
lamweili 416ae6f
Removed explicit http2 headers for less verbosity (using default values)
lamweili 35880d6
Added test cases to check for `res._header` and `res.headersSent` equ…
lamweili 36618af
Replaced the usage of undocumented `res._header` with `res.headersSen…
lamweili File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,16 @@ var Cookie = require('../session/cookie') | |
|
||
var min = 60 * 1000; | ||
|
||
var describeHttp2 = describe.skip | ||
try { | ||
var http2 = require('http2') | ||
describeHttp2 = describe | ||
} catch (err) { | ||
if (err) { | ||
console.log('http2 tests disabled.') | ||
} | ||
} | ||
|
||
describe('session()', function(){ | ||
it('should export constructors', function(){ | ||
assert.strictEqual(typeof session.Session, 'function') | ||
|
@@ -1371,6 +1381,25 @@ describe('session()', function(){ | |
}) | ||
}); | ||
|
||
describe('res._header patch', function () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this test makes sense. |
||
it('should be equivalent to res.headersSent', function (done) { | ||
request(createServer(function(req, res) { | ||
assert.strictEqual(!!res._header, !!res.headersSent, | ||
'res._header should be equal to res.headersSent (prior state change)') | ||
|
||
var oldState = !!res._header; | ||
res.end('ended') | ||
var newState = !!res._header; | ||
assert.notStrictEqual(oldState, newState) | ||
|
||
assert.strictEqual(!!res._header, !!res.headersSent, | ||
'res._header should be equal to res.headersSent (after state change)') | ||
})) | ||
.get('/') | ||
.expect(200, 'ended', done) | ||
}) | ||
}) | ||
|
||
describe('res.end patch', function () { | ||
it('should correctly handle res.end/res.write patched prior', function (done) { | ||
function setup (req, res) { | ||
|
@@ -2294,6 +2323,34 @@ describe('session()', function(){ | |
}) | ||
}) | ||
}) | ||
|
||
describeHttp2('http2', function () { | ||
it('should work with http2 server', function (done) { | ||
var server = createHttp2Server(null, function (req, res) { | ||
res.setHeader(http2.constants.HTTP2_HEADER_CONTENT_TYPE, 'text/plain') | ||
res.end('Hello, world!') | ||
}) | ||
server.on('listening', function () { | ||
var client = createHttp2Client(server.address().port) | ||
var request = client.request() | ||
request.on('response', function (headers) { | ||
assert.strictEqual(headers[http2.constants.HTTP2_HEADER_STATUS], 200) | ||
assert.strictEqual(headers[http2.constants.HTTP2_HEADER_CONTENT_TYPE], 'text/plain') | ||
}) | ||
var chunks = [] | ||
request.on('data', function (chunk) { | ||
chunks.push(chunk) | ||
}) | ||
request.on('end', function () { | ||
closeHttp2(client, server, function () { | ||
assert.strictEqual(Buffer.concat(chunks).toString(), 'Hello, world!') | ||
done() | ||
}) | ||
}) | ||
request.end() | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
function cookie(res) { | ||
|
@@ -2317,6 +2374,48 @@ function createServer (options, respond) { | |
return server.on('request', createRequestListener(opts, fn)) | ||
} | ||
|
||
function createHttp2Server (options, respond) { | ||
var fn = respond | ||
var opts = options | ||
var server = http2.createServer() | ||
|
||
// setup, options, respond | ||
if (typeof arguments[0] === 'function') { | ||
opts = arguments[1] | ||
fn = arguments[2] | ||
|
||
server.on('request', arguments[0]) | ||
} | ||
|
||
server.on('request', createRequestListener(opts, fn)) | ||
server.listen(0, '127.0.0.1') | ||
return server | ||
} | ||
|
||
function createHttp2Client (port) { | ||
return http2.connect('http://127.0.0.1:' + port) | ||
} | ||
|
||
function closeHttp2 (client, server, callback) { | ||
if (typeof client.shutdown === 'function') { | ||
// this is the node v8.x way of closing the connections | ||
client.shutdown({}, function () { | ||
server.close(function () { | ||
callback() | ||
}) | ||
}) | ||
} else { | ||
// this is the node v9.x onwards way of closing the connections | ||
client.close(function () { | ||
// force existing connections to time out after 1ms. | ||
// this is done to force the server to close in some cases where it wouldn't do it otherwise. | ||
server.close(function () { | ||
callback() | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
function createRequestListener(opts, fn) { | ||
var _session = createSession(opts) | ||
var respond = fn || end | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it's better to make it a function like in compression expressjs/compression#129