Skip to content

Commit

Permalink
test(serverreplay): add test for simple request
Browse files Browse the repository at this point in the history
  • Loading branch information
bobvanderlinden committed May 24, 2020
1 parent 6139a52 commit d616903
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/har.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -83,4 +112,6 @@ module.exports = {
createHar,
createEntry,
createErrorHarResponse,
createRequest,
createResponse,
};
106 changes: 106 additions & 0 deletions test/serverreplay.test.js
Original file line number Diff line number Diff line change
@@ -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<String>,
"time": -1,
"timings": Object {
"receive": -1,
"send": -1,
"wait": -1,
},
},
],
"version": "1.2",
},
}
`
);
});
});

0 comments on commit d616903

Please sign in to comment.