Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cirospaciari committed Oct 14, 2024
1 parent e159c32 commit ca7dd0e
Show file tree
Hide file tree
Showing 30 changed files with 1,157 additions and 370 deletions.
44 changes: 44 additions & 0 deletions test/js/node/test/parallel/http2-byteswritten-server.test.js
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
55 changes: 55 additions & 0 deletions test/js/node/test/parallel/http2-compat-aborted.test.js
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
67 changes: 67 additions & 0 deletions test/js/node/test/parallel/http2-compat-errors.test.js
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
62 changes: 31 additions & 31 deletions test/js/node/test/parallel/http2-compat-expect-handling.test.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
//#FILE: test-http2-compat-expect-handling.js
//#SHA1: 015a7b40547c969f4d631e7e743f5293d9e8f843
//-----------------
'use strict';
"use strict";

const http2 = require('http2');
const http2 = require("http2");

const hasCrypto = (() => {
try {
require('crypto');
require("crypto");
return true;
} catch (err) {
return false;
}
})();

const expectValue = 'meoww';
const expectValue = "meoww";

describe('HTTP/2 Expect Header Handling', () => {
describe("HTTP/2 Expect Header Handling", () => {
let server;
let port;

beforeAll((done) => {
beforeAll(done => {
server = http2.createServer();
server.listen(0, () => {
port = server.address().port;
done();
});
});

afterAll((done) => {
server.close(done);
afterAll(() => {
server.close();
});

test('server should not call request handler', () => {
test("server should not call request handler", () => {
const requestHandler = jest.fn();
server.on('request', requestHandler);
server.on("request", requestHandler);

return new Promise((resolve) => {
server.once('checkExpectation', (req, res) => {
return new Promise(resolve => {
server.once("checkExpectation", (req, res) => {
expect(req.headers.expect).toBe(expectValue);
res.statusCode = 417;
res.end();
Expand All @@ -47,41 +47,41 @@ describe('HTTP/2 Expect Header Handling', () => {

const client = http2.connect(`http://localhost:${port}`);
const req = client.request({
':path': '/',
':method': 'GET',
':scheme': 'http',
':authority': `localhost:${port}`,
'expect': expectValue
":path": "/",
":method": "GET",
":scheme": "http",
":authority": `localhost:${port}`,
"expect": expectValue,
});

req.on('response', (headers) => {
expect(headers[':status']).toBe(417);
req.on("response", headers => {
expect(headers[":status"]).toBe(417);
req.resume();
});

req.on('end', () => {
req.on("end", () => {
client.close();
});
});
});

test('client should receive 417 status', () => {
return new Promise((resolve) => {
test("client should receive 417 status", () => {
return new Promise(resolve => {
const client = http2.connect(`http://localhost:${port}`);
const req = client.request({
':path': '/',
':method': 'GET',
':scheme': 'http',
':authority': `localhost:${port}`,
'expect': expectValue
":path": "/",
":method": "GET",
":scheme": "http",
":authority": `localhost:${port}`,
"expect": expectValue,
});

req.on('response', (headers) => {
expect(headers[':status']).toBe(417);
req.on("response", headers => {
expect(headers[":status"]).toBe(417);
req.resume();
});

req.on('end', () => {
req.on("end", () => {
client.close();
resolve();
});
Expand All @@ -90,7 +90,7 @@ describe('HTTP/2 Expect Header Handling', () => {
});

if (!hasCrypto) {
test.skip('skipping HTTP/2 tests due to missing crypto support', () => {});
test.skip("skipping HTTP/2 tests due to missing crypto support", () => {});
}

//<#END_FILE: test-http2-compat-expect-handling.js
68 changes: 68 additions & 0 deletions test/js/node/test/parallel/http2-compat-serverrequest.test.js
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
Loading

0 comments on commit ca7dd0e

Please sign in to comment.