-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http2: add Http2ServerRequest and Http2ServerResponse options to crea…
…teServer
- Loading branch information
Peter Marton
authored and
Peter Marton
committed
Feb 9, 2018
1 parent
d1d9e2f
commit bdca0b7
Showing
5 changed files
with
95 additions
and
5 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
if (!common.hasCrypto) | ||
common.skip('missing crypto'); | ||
const assert = require('assert'); | ||
const h2 = require('http2'); | ||
|
||
class MyServerRequest extends h2.Http2ServerRequest { | ||
getUserAgent() { | ||
return this.headers['user-agent'] || 'unknown'; | ||
} | ||
} | ||
|
||
const server = h2.createServer({ | ||
Http2ServerRequest: MyServerRequest | ||
}, (req, res) => { | ||
assert.strictEqual(req.getUserAgent(), 'node-test'); | ||
|
||
res.writeHead(200, { 'Content-Type': 'text/plain' }); | ||
res.end(); | ||
}); | ||
server.listen(0); | ||
|
||
server.on('listening', common.mustCall(() => { | ||
|
||
const client = h2.connect(`http://localhost:${server.address().port}`); | ||
const req = client.request({ | ||
':path': '/', | ||
'User-Agent': 'node-test' | ||
}); | ||
|
||
req.on('response', common.mustCall()); | ||
|
||
req.resume(); | ||
req.on('end', common.mustCall(() => { | ||
server.close(); | ||
client.destroy(); | ||
})); | ||
})); |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
if (!common.hasCrypto) | ||
common.skip('missing crypto'); | ||
const h2 = require('http2'); | ||
|
||
class MyServerResponse extends h2.Http2ServerResponse { | ||
status(code) { | ||
return this.writeHead(code, { 'Content-Type': 'text/plain' }); | ||
} | ||
} | ||
|
||
const server = h2.createServer({ | ||
Http2ServerResponse: MyServerResponse | ||
}, (req, res) => { | ||
res.status(200); | ||
res.end(); | ||
}); | ||
server.listen(0); | ||
|
||
server.on('listening', common.mustCall(() => { | ||
|
||
const client = h2.connect(`http://localhost:${server.address().port}`); | ||
const req = client.request({ ':path': '/' }); | ||
|
||
req.on('response', common.mustCall()); | ||
|
||
req.resume(); | ||
req.on('end', common.mustCall(() => { | ||
server.close(); | ||
client.destroy(); | ||
})); | ||
})); |