Skip to content

Commit d89a64d

Browse files
committed
test: add e2e tests for proxy option
1 parent ddea063 commit d89a64d

File tree

2 files changed

+341
-1
lines changed

2 files changed

+341
-1
lines changed

test/e2e/__snapshots__/proxy.test.js.snap.webpack5

+56
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,59 @@ exports[`proxy option should sharing a proxy option respects proxy2 option: page
221221
exports[`proxy option should sharing a proxy option respects proxy2 option: response status 1`] = `200`;
222222

223223
exports[`proxy option should sharing a proxy option respects proxy2 option: response text 1`] = `"from proxy"`;
224+
225+
exports[`proxy option should supports http methods DELETE method: console messages 1`] = `Array []`;
226+
227+
exports[`proxy option should supports http methods DELETE method: page errors 1`] = `Array []`;
228+
229+
exports[`proxy option should supports http methods DELETE method: response status 1`] = `200`;
230+
231+
exports[`proxy option should supports http methods DELETE method: response text 1`] = `"DELETE method from proxy"`;
232+
233+
exports[`proxy option should supports http methods GET method: console messages 1`] = `Array []`;
234+
235+
exports[`proxy option should supports http methods GET method: page errors 1`] = `Array []`;
236+
237+
exports[`proxy option should supports http methods GET method: response status 1`] = `200`;
238+
239+
exports[`proxy option should supports http methods GET method: response text 1`] = `"GET method from proxy"`;
240+
241+
exports[`proxy option should supports http methods HEAD method: console messages 1`] = `Array []`;
242+
243+
exports[`proxy option should supports http methods HEAD method: page errors 1`] = `Array []`;
244+
245+
exports[`proxy option should supports http methods HEAD method: response status 1`] = `200`;
246+
247+
exports[`proxy option should supports http methods HEAD method: response text 1`] = `""`;
248+
249+
exports[`proxy option should supports http methods POST method (application/json): console messages 1`] = `Array []`;
250+
251+
exports[`proxy option should supports http methods POST method (application/json): page errors 1`] = `Array []`;
252+
253+
exports[`proxy option should supports http methods POST method (application/json): response headers content-type 1`] = `"application/json; charset=utf-8"`;
254+
255+
exports[`proxy option should supports http methods POST method (application/json): response status 1`] = `200`;
256+
257+
exports[`proxy option should supports http methods POST method (application/json): response text 1`] = `"{\\"answer\\":\\"POST method from proxy (id: 1)\\"}"`;
258+
259+
exports[`proxy option should supports http methods POST method (application/x-www-form-urlencoded): console messages 1`] = `Array []`;
260+
261+
exports[`proxy option should supports http methods POST method (application/x-www-form-urlencoded): page errors 1`] = `Array []`;
262+
263+
exports[`proxy option should supports http methods POST method (application/x-www-form-urlencoded): response headers content-type 1`] = `"text/html; charset=utf-8"`;
264+
265+
exports[`proxy option should supports http methods POST method (application/x-www-form-urlencoded): response status 1`] = `200`;
266+
267+
exports[`proxy option should supports http methods POST method (application/x-www-form-urlencoded): response text 1`] = `"POST method from proxy (id: 1)"`;
268+
269+
exports[`proxy option should supports http methods errors: console messages 1`] = `
270+
Array [
271+
"Failed to load resource: the server responded with a status of 500 (Internal Server Error)",
272+
]
273+
`;
274+
275+
exports[`proxy option should supports http methods errors: page errors 1`] = `Array []`;
276+
277+
exports[`proxy option should supports http methods errors: response status 1`] = `500`;
278+
279+
exports[`proxy option should supports http methods errors: response text 1`] = `"error from proxy"`;

test/e2e/proxy.test.js

+285-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const path = require("path");
44
const express = require("express");
5-
// const bodyParser = require("body-parser");
5+
const bodyParser = require("body-parser");
66
const WebSocket = require("ws");
77
const webpack = require("webpack");
88
const runBrowser = require("../helpers/run-browser");
@@ -1067,4 +1067,288 @@ describe("proxy option", () => {
10671067
});
10681068
});
10691069
});
1070+
1071+
describe("should supports http methods", () => {
1072+
let compiler;
1073+
let server;
1074+
let page;
1075+
let browser;
1076+
let pageErrors;
1077+
let consoleMessages;
1078+
let listener;
1079+
1080+
const proxyTarget = {
1081+
target: `http://localhost:${port1}`,
1082+
};
1083+
1084+
beforeEach(async () => {
1085+
compiler = webpack(config);
1086+
1087+
server = new Server(
1088+
{
1089+
proxy: {
1090+
"**": proxyTarget,
1091+
},
1092+
port: port3,
1093+
},
1094+
compiler
1095+
);
1096+
1097+
await server.start();
1098+
1099+
const proxy = express();
1100+
1101+
// Parse application/x-www-form-urlencoded
1102+
proxy.use(bodyParser.urlencoded({ extended: false }));
1103+
1104+
// Parse application/json
1105+
proxy.use(bodyParser.json());
1106+
1107+
// This forces Express to try to decode URLs, which is needed for the test
1108+
// associated with the middleware below.
1109+
proxy.all("*", (_req, res, next) => {
1110+
next();
1111+
});
1112+
// We must define all 4 params in order for this to be detected as an
1113+
// error handling middleware.
1114+
// eslint-disable-next-line no-unused-vars
1115+
proxy.use((error, proxyReq, res, next) => {
1116+
res.status(500);
1117+
res.send("error from proxy");
1118+
});
1119+
1120+
proxy.get("/get", (proxyReq, res) => {
1121+
res.send("GET method from proxy");
1122+
});
1123+
1124+
proxy.head("/head", (proxyReq, res) => {
1125+
res.send("HEAD method from proxy");
1126+
});
1127+
1128+
proxy.post("/post-x-www-form-urlencoded", (proxyReq, res) => {
1129+
const id = proxyReq.body.id;
1130+
1131+
res.status(200).send(`POST method from proxy (id: ${id})`);
1132+
});
1133+
1134+
proxy.post("/post-application-json", (proxyReq, res) => {
1135+
const id = proxyReq.body.id;
1136+
1137+
res.status(200).send({ answer: `POST method from proxy (id: ${id})` });
1138+
});
1139+
1140+
proxy.delete("/delete", (proxyReq, res) => {
1141+
res.send("DELETE method from proxy");
1142+
});
1143+
1144+
listener = proxy.listen(port1);
1145+
1146+
({ page, browser } = await runBrowser());
1147+
1148+
pageErrors = [];
1149+
consoleMessages = [];
1150+
});
1151+
1152+
afterEach(async () => {
1153+
await browser.close();
1154+
await server.stop();
1155+
await new Promise((resolve) => {
1156+
listener.close(() => {
1157+
resolve();
1158+
});
1159+
});
1160+
});
1161+
1162+
it("errors", async () => {
1163+
page
1164+
.on("console", (message) => {
1165+
consoleMessages.push(message);
1166+
})
1167+
.on("pageerror", (error) => {
1168+
pageErrors.push(error);
1169+
});
1170+
1171+
const response = await page.goto(`http://localhost:${port3}/%`, {
1172+
waitUntil: "networkidle0",
1173+
});
1174+
1175+
expect(response.status()).toMatchSnapshot("response status");
1176+
1177+
expect(await response.text()).toMatchSnapshot("response text");
1178+
1179+
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
1180+
"console messages"
1181+
);
1182+
1183+
expect(pageErrors).toMatchSnapshot("page errors");
1184+
});
1185+
1186+
it("GET method", async () => {
1187+
page
1188+
.on("console", (message) => {
1189+
consoleMessages.push(message);
1190+
})
1191+
.on("pageerror", (error) => {
1192+
pageErrors.push(error);
1193+
});
1194+
1195+
const response = await page.goto(`http://localhost:${port3}/get`, {
1196+
waitUntil: "networkidle0",
1197+
});
1198+
1199+
expect(response.status()).toMatchSnapshot("response status");
1200+
1201+
expect(await response.text()).toMatchSnapshot("response text");
1202+
1203+
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
1204+
"console messages"
1205+
);
1206+
1207+
expect(pageErrors).toMatchSnapshot("page errors");
1208+
});
1209+
1210+
it("HEAD method", async () => {
1211+
await page.setRequestInterception(true);
1212+
1213+
page
1214+
.on("console", (message) => {
1215+
consoleMessages.push(message);
1216+
})
1217+
.on("pageerror", (error) => {
1218+
pageErrors.push(error);
1219+
})
1220+
.on("request", (interceptedRequest) => {
1221+
interceptedRequest.continue({ method: "HEAD" });
1222+
});
1223+
1224+
const response = await page.goto(`http://localhost:${port3}/head`, {
1225+
waitUntil: "networkidle0",
1226+
});
1227+
1228+
expect(response.status()).toMatchSnapshot("response status");
1229+
1230+
expect(await response.text()).toMatchSnapshot("response text");
1231+
1232+
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
1233+
"console messages"
1234+
);
1235+
1236+
expect(pageErrors).toMatchSnapshot("page errors");
1237+
});
1238+
1239+
it("POST method (application/x-www-form-urlencoded)", async () => {
1240+
await page.setRequestInterception(true);
1241+
1242+
page
1243+
.on("console", (message) => {
1244+
consoleMessages.push(message);
1245+
})
1246+
.on("pageerror", (error) => {
1247+
pageErrors.push(error);
1248+
})
1249+
.on("request", (interceptedRequest) => {
1250+
interceptedRequest.continue({
1251+
method: "POST",
1252+
postData: "id=1",
1253+
headers: {
1254+
...interceptedRequest.headers(),
1255+
"Content-Type": "application/x-www-form-urlencoded",
1256+
},
1257+
});
1258+
});
1259+
1260+
const response = await page.goto(
1261+
`http://localhost:${port3}/post-x-www-form-urlencoded`,
1262+
{
1263+
waitUntil: "networkidle0",
1264+
}
1265+
);
1266+
1267+
expect(response.status()).toMatchSnapshot("response status");
1268+
1269+
expect(await response.text()).toMatchSnapshot("response text");
1270+
1271+
expect(response.headers()["content-type"]).toMatchSnapshot(
1272+
"response headers content-type"
1273+
);
1274+
1275+
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
1276+
"console messages"
1277+
);
1278+
1279+
expect(pageErrors).toMatchSnapshot("page errors");
1280+
});
1281+
1282+
it("POST method (application/json)", async () => {
1283+
await page.setRequestInterception(true);
1284+
1285+
page
1286+
.on("console", (message) => {
1287+
consoleMessages.push(message);
1288+
})
1289+
.on("pageerror", (error) => {
1290+
pageErrors.push(error);
1291+
})
1292+
.on("request", (interceptedRequest) => {
1293+
interceptedRequest.continue({
1294+
method: "POST",
1295+
postData: JSON.stringify({ id: "1" }),
1296+
headers: {
1297+
...interceptedRequest.headers(),
1298+
"Content-Type": "application/json",
1299+
},
1300+
});
1301+
});
1302+
1303+
const response = await page.goto(
1304+
`http://localhost:${port3}/post-application-json`,
1305+
{
1306+
waitUntil: "networkidle0",
1307+
}
1308+
);
1309+
1310+
expect(response.status()).toMatchSnapshot("response status");
1311+
1312+
expect(await response.text()).toMatchSnapshot("response text");
1313+
1314+
expect(response.headers()["content-type"]).toMatchSnapshot(
1315+
"response headers content-type"
1316+
);
1317+
1318+
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
1319+
"console messages"
1320+
);
1321+
1322+
expect(pageErrors).toMatchSnapshot("page errors");
1323+
});
1324+
1325+
it("DELETE method", async () => {
1326+
await page.setRequestInterception(true);
1327+
1328+
page
1329+
.on("console", (message) => {
1330+
consoleMessages.push(message);
1331+
})
1332+
.on("pageerror", (error) => {
1333+
pageErrors.push(error);
1334+
})
1335+
.on("request", (interceptedRequest) => {
1336+
interceptedRequest.continue({ method: "DELETE" });
1337+
});
1338+
1339+
const response = await page.goto(`http://localhost:${port3}/delete`, {
1340+
waitUntil: "networkidle0",
1341+
});
1342+
1343+
expect(response.status()).toMatchSnapshot("response status");
1344+
1345+
expect(await response.text()).toMatchSnapshot("response text");
1346+
1347+
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
1348+
"console messages"
1349+
);
1350+
1351+
expect(pageErrors).toMatchSnapshot("page errors");
1352+
});
1353+
});
10701354
});

0 commit comments

Comments
 (0)