-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e159c32
commit ca7dd0e
Showing
30 changed files
with
1,157 additions
and
370 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
test/js/node/test/parallel/http2-byteswritten-server.test.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,44 @@ | ||
//#FILE: test-http2-byteswritten-server.js | ||
//#SHA1: e0022b18b08bdc7ede66406fb18a0f2aedd86626 | ||
//----------------- | ||
'use strict'; | ||
|
||
const http2 = require('http2'); | ||
|
||
let http2Server; | ||
let serverPort; | ||
|
||
beforeAll((done) => { | ||
http2Server = http2.createServer((req, res) => { | ||
res.socket.on('finish', () => { | ||
expect(req.socket.bytesWritten).toBeGreaterThan(0); // 1094 | ||
}); | ||
res.writeHead(200, { 'Content-Type': 'text/plain' }); | ||
res.write(Buffer.from('1'.repeat(1024))); | ||
res.end(); | ||
}); | ||
|
||
http2Server.listen(0, () => { | ||
serverPort = http2Server.address().port; | ||
done(); | ||
}); | ||
}); | ||
|
||
afterAll((done) => { | ||
http2Server.close(done); | ||
}); | ||
|
||
test('HTTP/2 server bytesWritten', (done) => { | ||
const URL = `http://localhost:${serverPort}`; | ||
const http2client = http2.connect(URL, { protocol: 'http:' }); | ||
const req = http2client.request({ ':method': 'GET', ':path': '/' }); | ||
|
||
req.on('data', jest.fn()); | ||
req.on('end', () => { | ||
http2client.close(); | ||
done(); | ||
}); | ||
req.end(); | ||
}); | ||
|
||
//<#END_FILE: test-http2-byteswritten-server.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,55 @@ | ||
//#FILE: test-http2-compat-aborted.js | ||
//#SHA1: 2aaf11840d98c2b8f4387473180ec86626ac48d1 | ||
//----------------- | ||
"use strict"; | ||
|
||
const h2 = require("http2"); | ||
|
||
let server; | ||
let port; | ||
|
||
beforeAll(done => { | ||
if (!process.versions.openssl) { | ||
return test.skip("missing crypto"); | ||
} | ||
server = h2.createServer((req, res) => { | ||
req.on("aborted", () => { | ||
expect(req.aborted).toBe(true); | ||
expect(req.complete).toBe(true); | ||
}); | ||
expect(req.aborted).toBe(false); | ||
expect(req.complete).toBe(false); | ||
res.write("hello"); | ||
server.close(); | ||
}); | ||
|
||
server.listen(0, () => { | ||
port = server.address().port; | ||
done(); | ||
}); | ||
}); | ||
|
||
afterAll(() => { | ||
if (server) { | ||
server.close(); | ||
} | ||
}); | ||
|
||
test("HTTP/2 compat aborted", done => { | ||
const url = `http://localhost:${port}`; | ||
const client = h2.connect(url, () => { | ||
const request = client.request(); | ||
request.on("data", chunk => { | ||
client.destroy(); | ||
}); | ||
request.on("end", () => { | ||
done(); | ||
}); | ||
}); | ||
|
||
client.on("error", err => { | ||
// Ignore client errors as we're forcibly destroying the connection | ||
}); | ||
}); | ||
|
||
//<#END_FILE: test-http2-compat-aborted.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,67 @@ | ||
//#FILE: test-http2-compat-errors.js | ||
//#SHA1: 3a958d2216c02d05272fbc89bd09a532419876a4 | ||
//----------------- | ||
'use strict'; | ||
|
||
const h2 = require('http2'); | ||
|
||
// Simulate crypto check | ||
const hasCrypto = true; | ||
if (!hasCrypto) { | ||
test.skip('missing crypto', () => {}); | ||
} else { | ||
let expected = null; | ||
|
||
describe('http2 compat errors', () => { | ||
let server; | ||
let url; | ||
|
||
beforeAll((done) => { | ||
server = h2.createServer((req, res) => { | ||
const resStreamErrorHandler = jest.fn(); | ||
const reqErrorHandler = jest.fn(); | ||
const resErrorHandler = jest.fn(); | ||
const reqAbortedHandler = jest.fn(); | ||
const resAbortedHandler = jest.fn(); | ||
|
||
res.stream.on('error', resStreamErrorHandler); | ||
req.on('error', reqErrorHandler); | ||
res.on('error', resErrorHandler); | ||
req.on('aborted', reqAbortedHandler); | ||
res.on('aborted', resAbortedHandler); | ||
|
||
res.write('hello'); | ||
|
||
expected = new Error('kaboom'); | ||
res.stream.destroy(expected); | ||
|
||
// Use setImmediate to allow event handlers to be called | ||
setImmediate(() => { | ||
expect(resStreamErrorHandler).toHaveBeenCalled(); | ||
expect(reqErrorHandler).not.toHaveBeenCalled(); | ||
expect(resErrorHandler).not.toHaveBeenCalled(); | ||
expect(reqAbortedHandler).toHaveBeenCalled(); | ||
expect(resAbortedHandler).not.toHaveBeenCalled(); | ||
server.close(done); | ||
}); | ||
}); | ||
|
||
server.listen(0, () => { | ||
url = `http://localhost:${server.address().port}`; | ||
done(); | ||
}); | ||
}); | ||
|
||
test('should handle errors correctly', (done) => { | ||
const client = h2.connect(url, () => { | ||
const request = client.request(); | ||
request.on('data', (chunk) => { | ||
client.destroy(); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
//<#END_FILE: test-http2-compat-errors.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
68 changes: 68 additions & 0 deletions
68
test/js/node/test/parallel/http2-compat-serverrequest.test.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,68 @@ | ||
//#FILE: test-http2-compat-serverrequest.js | ||
//#SHA1: f661c6c9249c0cdc770439f7498943fc5edbf86b | ||
//----------------- | ||
'use strict'; | ||
|
||
const h2 = require('http2'); | ||
const net = require('net'); | ||
|
||
let server; | ||
let port; | ||
|
||
beforeAll(done => { | ||
server = h2.createServer(); | ||
server.listen(0, () => { | ||
port = server.address().port; | ||
done(); | ||
}); | ||
}); | ||
|
||
afterAll(done => { | ||
server.close(done); | ||
}); | ||
|
||
test('Http2ServerRequest should expose convenience properties', done => { | ||
expect.assertions(7); | ||
|
||
server.once('request', (request, response) => { | ||
const expected = { | ||
version: '2.0', | ||
httpVersionMajor: 2, | ||
httpVersionMinor: 0 | ||
}; | ||
|
||
expect(request.httpVersion).toBe(expected.version); | ||
expect(request.httpVersionMajor).toBe(expected.httpVersionMajor); | ||
expect(request.httpVersionMinor).toBe(expected.httpVersionMinor); | ||
|
||
expect(request.socket).toBeInstanceOf(net.Socket); | ||
expect(request.connection).toBeInstanceOf(net.Socket); | ||
expect(request.socket).toBe(request.connection); | ||
|
||
response.on('finish', () => { | ||
process.nextTick(() => { | ||
expect(request.socket).toBeTruthy(); | ||
done(); | ||
}); | ||
}); | ||
response.end(); | ||
}); | ||
|
||
const url = `http://localhost:${port}`; | ||
const client = h2.connect(url, () => { | ||
const headers = { | ||
':path': '/foobar', | ||
':method': 'GET', | ||
':scheme': 'http', | ||
':authority': `localhost:${port}` | ||
}; | ||
const request = client.request(headers); | ||
request.on('end', () => { | ||
client.close(); | ||
}); | ||
request.end(); | ||
request.resume(); | ||
}); | ||
}); | ||
|
||
//<#END_FILE: test-http2-compat-serverrequest.js |
Oops, something went wrong.