Skip to content

Commit

Permalink
test: simplify tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bobvanderlinden committed May 24, 2020
1 parent b90e8b5 commit d99d8e9
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 73 deletions.
30 changes: 29 additions & 1 deletion test/http.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const http = require("http");
const { readStreamText } = require("../src/node-conversion");

describe("http", () => {
it("when hosting server on port 80 we can respond with data", () => {
Expand Down Expand Up @@ -29,7 +30,7 @@ describe("http", () => {
});
});

it("test2", () => {
it("when hosting 2 servers we can send and receive requests individually", () => {
return new Promise((cb) => {
expect.assertions(2);
http
Expand Down Expand Up @@ -78,4 +79,31 @@ describe("http", () => {
.end();
});
});

it("when hosting a server we send post data", () => {
return new Promise((done) => {
expect.assertions(2);
http
.createServer(async (req, res) => {
const body = await readStreamText(req);
expect(body).toBe("this is post data");
res.end();
})
.listen(80, "localhost", 0, () => {});
const request = http.request(
{
host: "localhost",
port: 80,
url: "/",
},
async (response) => {
const responseBodyText = await readStreamText(response);
expect(responseBodyText).toBe("");
done();
}
);
request.write("this is post data");
request.end();
});
});
});
211 changes: 139 additions & 72 deletions test/proxy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,73 +4,65 @@ const AbortController = require("abort-controller");
const http = require("http");
const { readStreamText } = require("../src/node-conversion");
const { runServer } = require("../src/server");
const { Readable } = require("stream");

async function testProxy({
requestHandler,
block,
withStartedDateTime = false,
}) {
const abortController = new AbortController();

async function request(options) {
return await doRequest({
protocol: http,
hostnam: "127.0.0.1",
port: 8080,
url: "/",
...options,
});
}

try {
const server = http.createServer((req, res) => {
res.sendDate = false;
return requestHandler(req, res);
});
const DEFAULT_HOSTNAME = "localhost";
const DEFAULT_LISTEN_PORT = 80;
const DEFAULT_PROXY_PORT = 81;
const DEFAULT_HANDLER = (req, res) => res.end();

runServer({
server,
port: 8081,
hostname: "127.0.0.1",
signal: abortController.signal,
});
async function startServer(
{ hostname = DEFAULT_HOSTNAME, port = DEFAULT_PROXY_PORT },
handler = DEFAULT_HANDLER
) {
const server = http.createServer((req, res) => {
res.sendDate = false;
return handler(req, res);
});
await new Promise((resolve) => server.listen(port, hostname, 1, resolve));
return server;
}

let result;
const runningProxy = run({
listenHost: "127.0.0.1",
listenPort: 8080,
connectHost: "127.0.0.1",
connectPort: 8081,
signal: abortController.signal,
}).then((_result) => {
result = _result;
});
async function request(options) {
const response = await doRequest({
protocol: http,
hostname: DEFAULT_HOSTNAME,
port: DEFAULT_LISTEN_PORT,
url: "/",
...options,
});
response.body = await readStreamText(response);
return response;
}

await block(request);
abortController.abort();
await runningProxy;
async function runProxy(block) {
let result;
const abortController = new AbortController();
const runningProxy = run({
listenHost: DEFAULT_HOSTNAME,
listenPort: DEFAULT_LISTEN_PORT,
connectHost: DEFAULT_HOSTNAME,
connectPort: DEFAULT_PROXY_PORT,
signal: abortController.signal,
}).then((_result) => {
result = _result;
});

if (!withStartedDateTime) {
for (const entry of result.log.entries) {
delete entry.startedDateTime;
}
}
await block();
abortController.abort();
await runningProxy;

return result;
} catch (e) {
abortController.abort();
throw e;
for (const entry of result.log.entries) {
delete entry.startedDateTime;
}

return result;
}

describe("proxy", () => {
it("no request", async () => {
const result = await testProxy({
requestHandler: (req, res) => {
res.end();
},
block: () => {},
});
it("returns a correct HAR when there is no request", async () => {
await startServer({});
const result = await runProxy(() => {});
expect(result).toMatchInlineSnapshot(`
Object {
"log": Object {
Expand All @@ -85,19 +77,11 @@ describe("proxy", () => {
`);
});

it("empty request", async () => {
const result = await testProxy({
requestHandler: (req, res) => {
res.end();
},
block: async (request) => {
const response = await request({
url: "/",
});
await readStreamText(response);
},
it("returns a correct HAR when there is a single GET request", async () => {
await startServer({});
const result = await runProxy(async () => {
await request({});
});

expect(result).toMatchInlineSnapshot(`
Object {
"log": Object {
Expand All @@ -113,7 +97,7 @@ describe("proxy", () => {
"headers": Array [
Object {
"name": "Host",
"value": "localhost:8080",
"value": "localhost",
},
Object {
"name": "Connection",
Expand All @@ -123,12 +107,95 @@ describe("proxy", () => {
"headersSize": -1,
"httpVersion": "1.1",
"method": "GET",
"postData": Object {
"postData": undefined,
"queryString": Array [],
"url": "http://localhost/",
},
"response": Object {
"bodySize": 0,
"content": Object {
"mimeType": undefined,
"size": 0,
"text": "",
},
"cookies": Object {},
"headers": Array [
Object {
"name": "Connection",
"value": "close",
},
Object {
"name": "Content-Length",
"value": "0",
},
],
"headersSize": -1,
"httpVersion": "HTTP/1.1",
"redirectURL": "",
"status": 200,
"statusText": "OK",
},
"time": -1,
"timings": Object {
"receive": -1,
"send": -1,
"wait": -1,
},
},
],
"version": "1.2",
},
}
`);
});
it("returns a correct HAR when there is a single POST request", async () => {
await startServer({});
const result = await runProxy(async () => {
await request({
method: "POST",
headers: {
"Content-Type": "text/plain",
},
body: Readable.from(["this is the request body"]),
});
});

expect(result).toMatchInlineSnapshot(`
Object {
"log": Object {
"creator": Object {
"name": "harhar",
"version": "",
},
"entries": Array [
Object {
"cache": Object {},
"request": Object {
"bodySize": -1,
"headers": Array [
Object {
"name": "Host",
"value": "localhost",
},
Object {
"name": "Connection",
"value": "close",
},
Object {
"name": "Content-Type",
"value": "text/plain",
},
],
"headersSize": -1,
"httpVersion": "1.1",
"method": "POST",
"postData": Object {
"encoding": undefined,
"mimeType": "text/plain",
"text": "this is the request body",
},
"queryString": Array [],
"url": "http://localhost:8080/",
"url": "http://localhost/",
},
"response": Object {
"bodySize": 0,
Expand Down

0 comments on commit d99d8e9

Please sign in to comment.