Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add e2e tests for proxy option
Browse files Browse the repository at this point in the history
snitin315 committed Aug 29, 2021
1 parent 8fce077 commit d17bab7
Showing 2 changed files with 439 additions and 0 deletions.
56 changes: 56 additions & 0 deletions test/e2e/__snapshots__/proxy.test.js.snap.webpack5
Original file line number Diff line number Diff line change
@@ -277,3 +277,59 @@ exports[`proxy option should supports http methods errors: page errors 1`] = `Ar
exports[`proxy option should supports http methods errors: response status 1`] = `500`;

exports[`proxy option should supports http methods errors: response text 1`] = `"error from proxy"`;

exports[`proxy option should work and respect \`logProvider\` and \`logLevel\` options target respects a proxy option when a request path is matched: console messages 1`] = `
Array [
"Failed to load resource: the server responded with a status of 504 (Gateway Timeout)",
]
`;

exports[`proxy option should work and respect \`logProvider\` and \`logLevel\` options target respects a proxy option when a request path is matched: page errors 1`] = `Array []`;

exports[`proxy option should work and respect \`logProvider\` and \`logLevel\` options target respects a proxy option when a request path is matched: response status 1`] = `504`;

exports[`proxy option should work and respect \`logProvider\` and \`logLevel\` options target respects a proxy option when a request path is matched: response text 1`] = `"Error occured while trying to proxy: localhost:8123/my-path"`;

exports[`proxy option should work and respect \`logProvider\` and \`logLevel\` options with \`silent\` value target respects a proxy option when a request path is matched: console messages 1`] = `
Array [
"Failed to load resource: the server responded with a status of 504 (Gateway Timeout)",
]
`;

exports[`proxy option should work and respect \`logProvider\` and \`logLevel\` options with \`silent\` value target respects a proxy option when a request path is matched: page errors 1`] = `Array []`;

exports[`proxy option should work and respect \`logProvider\` and \`logLevel\` options with \`silent\` value target respects a proxy option when a request path is matched: response status 1`] = `504`;

exports[`proxy option should work and respect \`logProvider\` and \`logLevel\` options with \`silent\` value target respects a proxy option when a request path is matched: response text 1`] = `"Error occured while trying to proxy: localhost:8123/my-path"`;

exports[`proxy option should work and respect the \`infrastructureLogging.level\` option target respects a proxy option when a request path is matched: console messages 1`] = `
Array [
"Failed to load resource: the server responded with a status of 504 (Gateway Timeout)",
]
`;

exports[`proxy option should work and respect the \`infrastructureLogging.level\` option target respects a proxy option when a request path is matched: page errors 1`] = `Array []`;

exports[`proxy option should work and respect the \`infrastructureLogging.level\` option target respects a proxy option when a request path is matched: response status 1`] = `504`;

exports[`proxy option should work and respect the \`infrastructureLogging.level\` option target respects a proxy option when a request path is matched: response text 1`] = `"Error occured while trying to proxy: localhost:8123/my-path"`;

exports[`proxy option should work and respect the \`infrastructureLogging.level\` option with \`none\` value target respects a proxy option when a request path is matched: console messages 1`] = `
Array [
"Failed to load resource: the server responded with a status of 504 (Gateway Timeout)",
]
`;

exports[`proxy option should work and respect the \`infrastructureLogging.level\` option with \`none\` value target respects a proxy option when a request path is matched: page errors 1`] = `Array []`;

exports[`proxy option should work and respect the \`infrastructureLogging.level\` option with \`none\` value target respects a proxy option when a request path is matched: response status 1`] = `504`;

exports[`proxy option should work and respect the \`infrastructureLogging.level\` option with \`none\` value target respects a proxy option when a request path is matched: response text 1`] = `"Error occured while trying to proxy: localhost:8123/my-path"`;

exports[`proxy option should work in multi compiler mode respects a proxy option: console messages 1`] = `Array []`;

exports[`proxy option should work in multi compiler mode respects a proxy option: page errors 1`] = `Array []`;

exports[`proxy option should work in multi compiler mode respects a proxy option: response status 1`] = `200`;

exports[`proxy option should work in multi compiler mode respects a proxy option: response text 1`] = `"from proxy1"`;
383 changes: 383 additions & 0 deletions test/e2e/proxy.test.js
Original file line number Diff line number Diff line change
@@ -1351,4 +1351,387 @@ describe("proxy option", () => {
expect(pageErrors).toMatchSnapshot("page errors");
});
});

describe("should work in multi compiler mode", () => {
let compiler;
let server;
let page;
let browser;
let pageErrors;
let consoleMessages;

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

server = new Server(
{
proxy: {
"*": {
context: () => true,
target: `http://localhost:${port1}`,
},
},
port: port3,
},
compiler
);

await server.start();

await listenProxyServers();

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

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

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

it("respects a proxy option", async () => {
page
.on("console", (message) => {
consoleMessages.push(message);
})
.on("pageerror", (error) => {
pageErrors.push(error);
});

const response = await page.goto(`http://localhost:${port3}/proxy1`, {
waitUntil: "networkidle0",
});

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

expect(await response.text()).toMatchSnapshot("response text");

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

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

describe("should work and respect `logProvider` and `logLevel` options", () => {
let compiler;
let server;
let page;
let browser;
let pageErrors;
let consoleMessages;
let customLogProvider;

beforeEach(async () => {
customLogProvider = {
log: jest.fn(),
debug: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
};
compiler = webpack(config);

server = new Server(
{
proxy: {
"/my-path": {
target: "http://unknown:1234",
logProvider: () => customLogProvider,
logLevel: "error",
},
},
port: port3,
},
compiler
);

await server.start();

await listenProxyServers();

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

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

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

describe("target", () => {
it("respects a proxy option when a request path is matched", async () => {
page
.on("console", (message) => {
consoleMessages.push(message);
})
.on("pageerror", (error) => {
pageErrors.push(error);
});

const response = await page.goto(`http://localhost:${port3}/my-path`, {
waitUntil: "networkidle0",
});

expect(customLogProvider.error).toHaveBeenCalledTimes(1);

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

expect(await response.text()).toMatchSnapshot("response text");

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

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

describe("should work and respect `logProvider` and `logLevel` options with `silent` value", () => {
let compiler;
let server;
let page;
let browser;
let pageErrors;
let consoleMessages;
let customLogProvider;

beforeEach(async () => {
customLogProvider = {
log: jest.fn(),
debug: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
};
compiler = webpack(config);

server = new Server(
{
proxy: {
"/my-path": {
target: "http://unknown:1234",
logProvider: () => customLogProvider,
logLevel: "silent",
},
},
port: port3,
},
compiler
);

await server.start();

await listenProxyServers();

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

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

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

describe("target", () => {
it("respects a proxy option when a request path is matched", async () => {
page
.on("console", (message) => {
consoleMessages.push(message);
})
.on("pageerror", (error) => {
pageErrors.push(error);
});

const response = await page.goto(`http://localhost:${port3}/my-path`, {
waitUntil: "networkidle0",
});

expect(customLogProvider.error).toHaveBeenCalledTimes(0);

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

expect(await response.text()).toMatchSnapshot("response text");

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

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

describe("should work and respect the `infrastructureLogging.level` option", () => {
let compiler;
let server;
let page;
let browser;
let pageErrors;
let consoleMessages;
let customLogProvider;

beforeEach(async () => {
customLogProvider = {
log: jest.fn(),
debug: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
};

compiler = webpack({
...config,
infrastructureLogging: { level: "error" },
});

server = new Server(
{
proxy: {
"/my-path": {
target: "http://unknown:1234",
logProvider: () => customLogProvider,
},
},
port: port3,
},
compiler
);

await server.start();

await listenProxyServers();

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

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

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

describe("target", () => {
it("respects a proxy option when a request path is matched", async () => {
page
.on("console", (message) => {
consoleMessages.push(message);
})
.on("pageerror", (error) => {
pageErrors.push(error);
});

const response = await page.goto(`http://localhost:${port3}/my-path`, {
waitUntil: "networkidle0",
});

expect(customLogProvider.error).toHaveBeenCalledTimes(1);

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

expect(await response.text()).toMatchSnapshot("response text");

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

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

describe("should work and respect the `infrastructureLogging.level` option with `none` value", () => {
let compiler;
let server;
let page;
let browser;
let pageErrors;
let consoleMessages;
let customLogProvider;

beforeEach(async () => {
customLogProvider = {
log: jest.fn(),
debug: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
};

compiler = webpack({
...config,
infrastructureLogging: { level: "none" },
});

server = new Server(
{
proxy: {
"/my-path": {
target: "http://unknown:1234",
logProvider: () => customLogProvider,
},
},
port: port3,
},
compiler
);

await server.start();

await listenProxyServers();

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

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

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

describe("target", () => {
it("respects a proxy option when a request path is matched", async () => {
page
.on("console", (message) => {
consoleMessages.push(message);
})
.on("pageerror", (error) => {
pageErrors.push(error);
});

const response = await page.goto(`http://localhost:${port3}/my-path`, {
waitUntil: "networkidle0",
});

expect(customLogProvider.error).toHaveBeenCalledTimes(0);

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

expect(await response.text()).toMatchSnapshot("response text");

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

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

0 comments on commit d17bab7

Please sign in to comment.