Skip to content

Commit

Permalink
test: more
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Sep 15, 2021
1 parent f8bf3ff commit 6b6ace1
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 8 deletions.
26 changes: 18 additions & 8 deletions lib/Server.js
Expand Up @@ -491,17 +491,27 @@ class Server {
}

// normalize `headers`
if (
typeof options.headers !== "undefined" &&
Array.isArray(options.headers)
) {
if (typeof options.headers !== "undefined") {
const allHeaders = {};

options.headers.forEach((header) => {
allHeaders[header.key] = header.value;
});
if (Array.isArray(options.headers)) {
options.headers.forEach((header) => {
allHeaders[header.key] = header.value;
});
options.headers = allHeaders;
}

options.headers = allHeaders;
if (typeof options.headers === "function") {
const returnedHeaders = options.headers();

if (Array.isArray(returnedHeaders)) {
returnedHeaders.forEach((header) => {
allHeaders[header.key] = header.value;
});

options.headers = allHeaders;
}
}
}

if (typeof options.historyApiFallback === "undefined") {
Expand Down
10 changes: 10 additions & 0 deletions test/e2e/__snapshots__/headers.test.js.snap.webpack4
@@ -1,5 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`headers option as a function returning an array should handle GET request with headers: console messages 1`] = `Array []`;

exports[`headers option as a function returning an array should handle GET request with headers: page errors 1`] = `Array []`;

exports[`headers option as a function returning an array should handle GET request with headers: response headers x-bar 1`] = `"value2"`;

exports[`headers option as a function returning an array should handle GET request with headers: response headers x-foo 1`] = `"value1"`;

exports[`headers option as a function returning an array should handle GET request with headers: response status 1`] = `200`;

exports[`headers option as a function should handle GET request with headers as a function: console messages 1`] = `Array []`;

exports[`headers option as a function should handle GET request with headers as a function: page errors 1`] = `Array []`;
Expand Down
10 changes: 10 additions & 0 deletions test/e2e/__snapshots__/headers.test.js.snap.webpack5
@@ -1,5 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`headers option as a function returning an array should handle GET request with headers: console messages 1`] = `Array []`;

exports[`headers option as a function returning an array should handle GET request with headers: page errors 1`] = `Array []`;

exports[`headers option as a function returning an array should handle GET request with headers: response headers x-bar 1`] = `"value2"`;

exports[`headers option as a function returning an array should handle GET request with headers: response headers x-foo 1`] = `"value1"`;

exports[`headers option as a function returning an array should handle GET request with headers: response status 1`] = `200`;

exports[`headers option as a function should handle GET request with headers as a function: console messages 1`] = `Array []`;

exports[`headers option as a function should handle GET request with headers as a function: page errors 1`] = `Array []`;
Expand Down
72 changes: 72 additions & 0 deletions test/e2e/headers.test.js
Expand Up @@ -258,6 +258,78 @@ describe("headers option", () => {
});
});

describe("as a function returning an array", () => {
let compiler;
let server;
let page;
let browser;
let pageErrors;
let consoleMessages;

beforeEach(async () => {
compiler = webpack(config);

server = new Server(
{
headers: () => [
{
key: "X-Foo",
value: "value1",
},
{
key: "X-Bar",
value: "value2",
},
],
port,
},
compiler
);

await server.start();

({ page, browser } = await runBrowser());

pageErrors = [];
consoleMessages = [];
});

afterEach(async () => {
await browser.close();
await server.stop();
});

it("should handle GET request with headers", async () => {
page
.on("console", (message) => {
consoleMessages.push(message);
})
.on("pageerror", (error) => {
pageErrors.push(error);
});

const response = await page.goto(`http://127.0.0.1:${port}/main.js`, {
waitUntil: "networkidle0",
});

expect(response.headers()["x-foo"]).toMatchSnapshot(
"response headers x-foo"
);

expect(response.headers()["x-bar"]).toMatchSnapshot(
"response headers x-bar"
);

expect(response.status()).toMatchSnapshot("response status");

expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
"console messages"
);

expect(pageErrors).toMatchSnapshot("page errors");
});
});

describe("dev middleware headers take precedence for dev middleware output files", () => {
let compiler;
let server;
Expand Down

0 comments on commit 6b6ace1

Please sign in to comment.