-
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.
src: add writeEarlyHints function to ServerResponse
- Loading branch information
Wing Leung
committed
Aug 8, 2022
1 parent
f136e7e
commit 3c6b924
Showing
4 changed files
with
124 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
'use strict'; | ||
require('../common'); | ||
const assert = require('assert'); | ||
const http = require('http'); | ||
const debug = require('util').debuglog('test'); | ||
|
||
const testResBody = 'response content\n'; | ||
|
||
const server = http.createServer((req, res) => { | ||
debug('Server sending early hints...'); | ||
|
||
res.writeEarlyHints([ | ||
'</styles.css>; rel=preload; as=style', | ||
'</scripts.js>; rel=preload; as=script', | ||
]); | ||
|
||
debug('Server sending full response...'); | ||
res.writeHead(200, { | ||
'Content-Type': 'text/plain', | ||
'ABCD': '1' | ||
}); | ||
|
||
res.end(testResBody); | ||
}); | ||
|
||
server.listen(0, function() { | ||
const req = http.request({ | ||
port: this.address().port, | ||
path: '/' | ||
}); | ||
|
||
req.end(); | ||
debug('Client sending request...'); | ||
|
||
req.on('information', (res) => { | ||
assert.strictEqual(res.headers.link, '</styles.css>; rel=preload; as=style,</scripts.js>; rel=preload; as=script'); | ||
}); | ||
|
||
req.on('response', function(res) { | ||
let body = ''; | ||
|
||
assert.strictEqual( | ||
res.statusCode, 200, | ||
`Final status code was ${res.statusCode}, not 200.` | ||
); | ||
res.setEncoding('utf8'); | ||
res.on('data', function(chunk) { body += chunk; }); | ||
res.on('end', function() { | ||
debug('Got full response.'); | ||
assert.strictEqual(body, testResBody); | ||
server.close(); | ||
}); | ||
}); | ||
}); |
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'; | ||
require('../common'); | ||
const assert = require('assert'); | ||
const http = require('http'); | ||
const debug = require('util').debuglog('test'); | ||
|
||
const testResBody = 'response content\n'; | ||
|
||
const server = http.createServer((req, res) => { | ||
debug('Server sending early hints...'); | ||
|
||
res.writeEarlyHints('</styles.css>; rel=preload; as=style'); | ||
|
||
debug('Server sending full response...'); | ||
res.writeHead(200, { | ||
'Content-Type': 'text/plain', | ||
'ABCD': '1' | ||
}); | ||
|
||
res.end(testResBody); | ||
}); | ||
|
||
server.listen(0, function() { | ||
const req = http.request({ | ||
port: this.address().port, | ||
path: '/' | ||
}); | ||
|
||
req.end(); | ||
debug('Client sending request...'); | ||
|
||
req.on('information', (res) => { | ||
assert.strictEqual(res.headers.link, '</styles.css>; rel=preload; as=style'); | ||
}); | ||
|
||
req.on('response', function(res) { | ||
let body = ''; | ||
|
||
assert.strictEqual( | ||
res.statusCode, 200, | ||
`Final status code was ${res.statusCode}, not 200.` | ||
); | ||
res.setEncoding('utf8'); | ||
res.on('data', function(chunk) { body += chunk; }); | ||
res.on('end', function() { | ||
debug('Got full response.'); | ||
assert.strictEqual(body, testResBody); | ||
server.close(); | ||
}); | ||
}); | ||
}); |