From d6169033598e689a804e5219ce7fc46d94bcd400 Mon Sep 17 00:00:00 2001 From: Bob van der Linden Date: Sun, 24 May 2020 23:31:49 +0200 Subject: [PATCH] test(serverreplay): add test for simple request --- src/har.js | 31 +++++++++++ test/serverreplay.test.js | 106 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 test/serverreplay.test.js diff --git a/src/har.js b/src/har.js index b664512..67c2dc7 100644 --- a/src/har.js +++ b/src/har.js @@ -58,6 +58,35 @@ function createEntry({ }; } +function createRequest({ url, ...request }) { + return { + url, + headers: [], + queryString: [], + headersSize: -1, + bodySize: -1, + ...request, + }; +} + +function createResponse(response) { + return { + status: 200, + statusText: "OK", + httpVersion: "1.1", + headers: [], + cookies: [], + content: { + size: 0, + mimeType: "x-unknown", + }, + redirectURL: "", + headersSize: -1, + bodySize: -1, + ...response, + }; +} + function createErrorHarResponse(errorMessage) { return { status: 0, @@ -83,4 +112,6 @@ module.exports = { createHar, createEntry, createErrorHarResponse, + createRequest, + createResponse, }; diff --git a/test/serverreplay.test.js b/test/serverreplay.test.js new file mode 100644 index 0000000..db4728a --- /dev/null +++ b/test/serverreplay.test.js @@ -0,0 +1,106 @@ +const { run } = require("../src/serverreplay"); +const { + createHar, + createEntry, + createRequest, + createResponse, +} = require("../src/har"); +const { request } = require("./support/http"); +const { AbortController } = require("abort-controller"); + +describe("serverreplay", () => { + it("returns a correct HAR when making matching simple request", async () => { + const harInput = createHar({ + entries: [ + createEntry({ + request: createRequest({ + method: "GET", + url: "http://localhost/", + httpVersion: "1.1", + headers: [ + { name: "Host", value: "localhost" }, + { name: "Connection", value: "close" }, + ], + }), + response: createResponse({ + status: 200, + statusText: "OK", + }), + }), + ], + }); + const abortController = new AbortController(); + let result; + const promise = run({ + port: 80, + harInput, + abortController, + }).then((_result) => { + result = _result; + }); + await request(); + abortController.abort(); + await promise; + expect(result).toMatchInlineSnapshot( + { + log: { entries: [{ startedDateTime: expect.any(String) }] }, + }, + ` + 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", + }, + ], + "headersSize": -1, + "httpVersion": "1.1", + "method": "GET", + "postData": undefined, + "queryString": Array [], + "url": "http://localhost/", + }, + "response": Object { + "bodySize": -1, + "content": Object { + "mimeType": "x-unknown", + "size": 0, + }, + "cookies": Array [], + "headers": Array [], + "headersSize": -1, + "httpVersion": "1.1", + "redirectURL": "", + "status": 200, + "statusText": "OK", + }, + "startedDateTime": Any, + "time": -1, + "timings": Object { + "receive": -1, + "send": -1, + "wait": -1, + }, + }, + ], + "version": "1.2", + }, + } + ` + ); + }); +});