-
Notifications
You must be signed in to change notification settings - Fork 29.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http: add options to http.createServer()
This adds the optional options argument to `http.createServer()`. It contains two options: the `IncomingMessage` and `ServerReponse` option. Backport-PR-URL: #20456 PR-URL: #15752 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Evan Lucas <[email protected]>
- Loading branch information
1 parent
e00e36f
commit 4994b7f
Showing
9 changed files
with
223 additions
and
11 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
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
41 changes: 41 additions & 0 deletions
41
test/parallel/test-http-server-options-incoming-message.js
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,41 @@ | ||
'use strict'; | ||
|
||
/** | ||
* This test covers http.Server({ IncomingMessage }) option: | ||
* With IncomingMessage option the server should use | ||
* the new class for creating req Object instead of the default | ||
* http.IncomingMessage. | ||
*/ | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const http = require('http'); | ||
|
||
class MyIncomingMessage extends http.IncomingMessage { | ||
getUserAgent() { | ||
return this.headers['user-agent'] || 'unknown'; | ||
} | ||
} | ||
|
||
const server = http.Server({ | ||
IncomingMessage: MyIncomingMessage | ||
}, common.mustCall(function(req, res) { | ||
assert.strictEqual(req.getUserAgent(), 'node-test'); | ||
res.statusCode = 200; | ||
res.end(); | ||
})); | ||
server.listen(); | ||
|
||
server.on('listening', function makeRequest() { | ||
http.get({ | ||
port: this.address().port, | ||
headers: { | ||
'User-Agent': 'node-test' | ||
} | ||
}, (res) => { | ||
assert.strictEqual(res.statusCode, 200); | ||
res.on('end', () => { | ||
server.close(); | ||
}); | ||
res.resume(); | ||
}); | ||
}); |
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,35 @@ | ||
'use strict'; | ||
|
||
/** | ||
* This test covers http.Server({ ServerResponse }) option: | ||
* With ServerResponse option the server should use | ||
* the new class for creating res Object instead of the default | ||
* http.ServerResponse. | ||
*/ | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const http = require('http'); | ||
|
||
class MyServerResponse extends http.ServerResponse { | ||
status(code) { | ||
return this.writeHead(code, { 'Content-Type': 'text/plain' }); | ||
} | ||
} | ||
|
||
const server = http.Server({ | ||
ServerResponse: MyServerResponse | ||
}, common.mustCall(function(req, res) { | ||
res.status(200); | ||
res.end(); | ||
})); | ||
server.listen(); | ||
|
||
server.on('listening', function makeRequest() { | ||
http.get({ port: this.address().port }, (res) => { | ||
assert.strictEqual(res.statusCode, 200); | ||
res.on('end', () => { | ||
server.close(); | ||
}); | ||
res.resume(); | ||
}); | ||
}); |
51 changes: 51 additions & 0 deletions
51
test/parallel/test-https-server-options-incoming-message.js
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,51 @@ | ||
'use strict'; | ||
|
||
/** | ||
* This test covers http.Server({ IncomingMessage }) option: | ||
* With IncomingMessage option the server should use | ||
* the new class for creating req Object instead of the default | ||
* http.IncomingMessage. | ||
*/ | ||
const common = require('../common'); | ||
const fixtures = require('../common/fixtures'); | ||
|
||
if (!common.hasCrypto) | ||
common.skip('missing crypto'); | ||
|
||
const assert = require('assert'); | ||
const http = require('http'); | ||
const https = require('https'); | ||
|
||
class MyIncomingMessage extends http.IncomingMessage { | ||
getUserAgent() { | ||
return this.headers['user-agent'] || 'unknown'; | ||
} | ||
} | ||
|
||
const server = https.createServer({ | ||
key: fixtures.readKey('agent1-key.pem'), | ||
cert: fixtures.readKey('agent1-cert.pem'), | ||
ca: fixtures.readKey('ca1-cert.pem'), | ||
IncomingMessage: MyIncomingMessage | ||
}, common.mustCall(function(req, res) { | ||
assert.strictEqual(req.getUserAgent(), 'node-test'); | ||
res.statusCode = 200; | ||
res.end(); | ||
})); | ||
server.listen(); | ||
|
||
server.on('listening', function makeRequest() { | ||
https.get({ | ||
port: this.address().port, | ||
rejectUnauthorized: false, | ||
headers: { | ||
'User-Agent': 'node-test' | ||
} | ||
}, (res) => { | ||
assert.strictEqual(res.statusCode, 200); | ||
res.on('end', () => { | ||
server.close(); | ||
}); | ||
res.resume(); | ||
}); | ||
}); |
47 changes: 47 additions & 0 deletions
47
test/parallel/test-https-server-options-server-response.js
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,47 @@ | ||
'use strict'; | ||
|
||
/** | ||
* This test covers http.Server({ ServerResponse }) option: | ||
* With ServerResponse option the server should use | ||
* the new class for creating res Object instead of the default | ||
* http.ServerResponse. | ||
*/ | ||
const common = require('../common'); | ||
const fixtures = require('../common/fixtures'); | ||
|
||
if (!common.hasCrypto) | ||
common.skip('missing crypto'); | ||
|
||
const assert = require('assert'); | ||
const http = require('http'); | ||
const https = require('https'); | ||
|
||
class MyServerResponse extends http.ServerResponse { | ||
status(code) { | ||
return this.writeHead(code, { 'Content-Type': 'text/plain' }); | ||
} | ||
} | ||
|
||
const server = https.createServer({ | ||
key: fixtures.readKey('agent1-key.pem'), | ||
cert: fixtures.readKey('agent1-cert.pem'), | ||
ca: fixtures.readKey('ca1-cert.pem'), | ||
ServerResponse: MyServerResponse | ||
}, common.mustCall(function(req, res) { | ||
res.status(200); | ||
res.end(); | ||
})); | ||
server.listen(); | ||
|
||
server.on('listening', function makeRequest() { | ||
https.get({ | ||
port: this.address().port, | ||
rejectUnauthorized: false | ||
}, (res) => { | ||
assert.strictEqual(res.statusCode, 200); | ||
res.on('end', () => { | ||
server.close(); | ||
}); | ||
res.resume(); | ||
}); | ||
}); |