Skip to content

Commit

Permalink
get tests working
Browse files Browse the repository at this point in the history
  • Loading branch information
dhensby committed Oct 11, 2022
1 parent 4435c47 commit 41b638d
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions test/httpbis/httpbis.int.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as http from 'http';
import * as http2 from 'http2';
import { Server } from 'net';
import { Server, Socket } from 'net';
import { expect } from 'chai';
import {
createVerifier,
Expand Down Expand Up @@ -51,7 +51,11 @@ function createHttpServer(config: ServerConfig): TestServer {

function createHttp2Server(config: ServerConfig): TestServer {
const requests: Request[] = [];
const connections: Socket[] = [];
const server = http2.createServer();
server.on('connection', (conn) => {
connections.push(conn);
});
server.on('stream', (stream, headers) => {
const domain = headers[':authority'] ?? 'localhost';
const request: Request = {
Expand All @@ -67,9 +71,28 @@ function createHttp2Server(config: ServerConfig): TestServer {
server.once('listening', () => resolve());
server.listen(config.port);
}),
stop: () => new Promise<void>((resolve) => server.close(() => resolve())),
stop: () => new Promise<void>((resolve) => {
Promise.all(connections.map((conn) => new Promise<void>((done) => {
if (conn.destroyed) {
done();
} else {
conn.destroy();
conn.on('close', done);
}
}))).then(() => server.close(() => resolve()));
}),
requests,
clear: () => requests.splice(0),
clear: () => new Promise<void>((resolve) => {
requests.splice(0);
Promise.all(connections.map((conn) => new Promise<void>((closed) => {
if (conn.destroyed) {
closed();
} else {
conn.destroy();
conn.on('close', closed);
}
}))).then(() => resolve());
}),
};
}

Expand Down Expand Up @@ -119,17 +142,15 @@ function makeHttp2Request(request: Request & { body?: string; }, port?: number):
headers = h as Record<string, string | string[]>;
});
req.on('error', (e) => {
reject(e);
client.close();
client.close(() => reject(e));
});
const chunks: Buffer[] = [];
req.on('data', (chunk) => chunks.push(chunk));
req.on('end', () => {
client.close();
resolve({
client.close(() => resolve({
headers,
body: Buffer.concat(chunks),
});
}));
});
});
}
Expand Down Expand Up @@ -443,7 +464,9 @@ describe('httpbis', () => {
'signature': 'sig-b24=:MEYCIQDXrmWrcxKWLQQm0zlwbFr5/KAlB9oHkfMpNRVCuGVHjQIhAKtljVKRuRoWv5dCKuc+GgP3eqLAq+Eg0d3olyR67BYK:',
});
stream.end('{"message": "good dog"}');
stream.close();
stream.close(undefined, () => {
console.log('closed');
});
});
return server.start();
});
Expand Down

0 comments on commit 41b638d

Please sign in to comment.