Skip to content

Commit 39cef1b

Browse files
authored
test: add e2e tests for http2 option (#3778)
1 parent c36b50c commit 39cef1b

File tree

4 files changed

+270
-133
lines changed

4 files changed

+270
-133
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`http2 option https without http2, disables HTTP/2 should handle GET request to index route (/): HTTP version 1`] = `"http/1.1"`;
4+
5+
exports[`http2 option https without http2, disables HTTP/2 should handle GET request to index route (/): console messages 1`] = `Array []`;
6+
7+
exports[`http2 option https without http2, disables HTTP/2 should handle GET request to index route (/): page errors 1`] = `Array []`;
8+
9+
exports[`http2 option https without http2, disables HTTP/2 should handle GET request to index route (/): response status 1`] = `200`;
10+
11+
exports[`http2 option https without http2, disables HTTP/2 should handle GET request to index route (/): response text 1`] = `
12+
"Heyo.
13+
"
14+
`;
15+
16+
exports[`http2 option server works with http2 option, without https option enabled should handle GET request to index route (/): HTTP version 1`] = `"h2"`;
17+
18+
exports[`http2 option server works with http2 option, without https option enabled should handle GET request to index route (/): console messages 1`] = `Array []`;
19+
20+
exports[`http2 option server works with http2 option, without https option enabled should handle GET request to index route (/): page errors 1`] = `Array []`;
21+
22+
exports[`http2 option server works with http2 option, without https option enabled should handle GET request to index route (/): response status 1`] = `200`;
23+
24+
exports[`http2 option server works with http2 option, without https option enabled should handle GET request to index route (/): response text 1`] = `
25+
"Heyo.
26+
"
27+
`;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`http2 option https without http2, disables HTTP/2 should handle GET request to index route (/): HTTP version 1`] = `"http/1.1"`;
4+
5+
exports[`http2 option https without http2, disables HTTP/2 should handle GET request to index route (/): console messages 1`] = `Array []`;
6+
7+
exports[`http2 option https without http2, disables HTTP/2 should handle GET request to index route (/): page errors 1`] = `Array []`;
8+
9+
exports[`http2 option https without http2, disables HTTP/2 should handle GET request to index route (/): response status 1`] = `200`;
10+
11+
exports[`http2 option https without http2, disables HTTP/2 should handle GET request to index route (/): response text 1`] = `
12+
"Heyo.
13+
"
14+
`;
15+
16+
exports[`http2 option server works with http2 option, without https option enabled should handle GET request to index route (/): HTTP version 1`] = `"h2"`;
17+
18+
exports[`http2 option server works with http2 option, without https option enabled should handle GET request to index route (/): console messages 1`] = `Array []`;
19+
20+
exports[`http2 option server works with http2 option, without https option enabled should handle GET request to index route (/): page errors 1`] = `Array []`;
21+
22+
exports[`http2 option server works with http2 option, without https option enabled should handle GET request to index route (/): response status 1`] = `200`;
23+
24+
exports[`http2 option server works with http2 option, without https option enabled should handle GET request to index route (/): response text 1`] = `
25+
"Heyo.
26+
"
27+
`;

test/e2e/http2.test.js

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
"use strict";
2+
3+
const path = require("path");
4+
const http2 = require("http2");
5+
const webpack = require("webpack");
6+
const Server = require("../../lib/Server");
7+
const config = require("../fixtures/static-config/webpack.config");
8+
const runBrowser = require("../helpers/run-browser");
9+
const port = require("../ports-map")["https-option"];
10+
11+
const staticDirectory = path.resolve(
12+
__dirname,
13+
"../fixtures/static-config/public"
14+
);
15+
16+
describe("http2 option", () => {
17+
describe("http2 works with https", () => {
18+
let compiler;
19+
let server;
20+
let page;
21+
let browser;
22+
let HTTPVersion;
23+
24+
beforeEach(async () => {
25+
compiler = webpack(config);
26+
27+
server = new Server(
28+
{
29+
static: staticDirectory,
30+
https: true,
31+
http2: true,
32+
port,
33+
},
34+
compiler
35+
);
36+
37+
await server.start();
38+
39+
({ page, browser } = await runBrowser());
40+
41+
await page.goto(`https://127.0.0.1:${port}/`, {
42+
waitUntil: "networkidle0",
43+
});
44+
45+
HTTPVersion = await page.evaluate(
46+
() => performance.getEntries()[0].nextHopProtocol
47+
);
48+
});
49+
50+
afterEach(async () => {
51+
await browser.close();
52+
await server.stop();
53+
});
54+
55+
it("should confirm that http2 client can connect", (done) => {
56+
const client = http2.connect(`https://localhost:${port}`, {
57+
rejectUnauthorized: false,
58+
});
59+
60+
client.on("error", (err) => console.error(err));
61+
62+
const http2Req = client.request({ ":path": "/" });
63+
64+
http2Req.on("response", (headers) => {
65+
expect(headers[":status"]).toEqual(200);
66+
});
67+
68+
http2Req.setEncoding("utf8");
69+
70+
let data = "";
71+
72+
http2Req.on("data", (chunk) => {
73+
data += chunk;
74+
});
75+
76+
http2Req.on("end", () => {
77+
expect(data).toEqual(expect.stringMatching(/Heyo/));
78+
client.close();
79+
done();
80+
});
81+
82+
expect(HTTPVersion).toEqual("h2");
83+
84+
http2Req.end();
85+
});
86+
});
87+
88+
describe("server works with http2 option, without https option enabled", () => {
89+
let compiler;
90+
let server;
91+
let page;
92+
let browser;
93+
let pageErrors;
94+
let consoleMessages;
95+
96+
beforeEach(async () => {
97+
compiler = webpack(config);
98+
99+
server = new Server(
100+
{
101+
static: staticDirectory,
102+
http2: true,
103+
port,
104+
},
105+
compiler
106+
);
107+
108+
await server.start();
109+
110+
({ page, browser } = await runBrowser());
111+
112+
pageErrors = [];
113+
consoleMessages = [];
114+
});
115+
116+
afterEach(async () => {
117+
await browser.close();
118+
await server.stop();
119+
});
120+
121+
it("should handle GET request to index route (/)", async () => {
122+
page
123+
.on("console", (message) => {
124+
consoleMessages.push(message);
125+
})
126+
.on("pageerror", (error) => {
127+
pageErrors.push(error);
128+
});
129+
130+
const response = await page.goto(`https://127.0.0.1:${port}/`, {
131+
waitUntil: "networkidle0",
132+
});
133+
134+
const HTTPVersion = await page.evaluate(
135+
() => performance.getEntries()[0].nextHopProtocol
136+
);
137+
138+
expect(HTTPVersion).toMatchSnapshot("HTTP version");
139+
140+
expect(response.status()).toMatchSnapshot("response status");
141+
142+
expect(await response.text()).toMatchSnapshot("response text");
143+
144+
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
145+
"console messages"
146+
);
147+
148+
expect(pageErrors).toMatchSnapshot("page errors");
149+
});
150+
});
151+
152+
describe("https without http2, disables HTTP/2", () => {
153+
let compiler;
154+
let server;
155+
let page;
156+
let browser;
157+
let pageErrors;
158+
let consoleMessages;
159+
160+
beforeEach(async () => {
161+
compiler = webpack(config);
162+
163+
server = new Server(
164+
{
165+
static: staticDirectory,
166+
https: true,
167+
http2: false,
168+
port,
169+
},
170+
compiler
171+
);
172+
173+
await server.start();
174+
175+
({ page, browser } = await runBrowser());
176+
177+
pageErrors = [];
178+
consoleMessages = [];
179+
});
180+
181+
afterEach(async () => {
182+
await browser.close();
183+
await server.stop();
184+
});
185+
186+
it("should handle GET request to index route (/)", async () => {
187+
page
188+
.on("console", (message) => {
189+
consoleMessages.push(message);
190+
})
191+
.on("pageerror", (error) => {
192+
pageErrors.push(error);
193+
});
194+
195+
const response = await page.goto(`https://127.0.0.1:${port}/`, {
196+
waitUntil: "networkidle0",
197+
});
198+
199+
const HTTPVersion = await page.evaluate(
200+
() => performance.getEntries()[0].nextHopProtocol
201+
);
202+
203+
expect(HTTPVersion).toMatchSnapshot("HTTP version");
204+
205+
expect(response.status()).toMatchSnapshot("response status");
206+
207+
expect(await response.text()).toMatchSnapshot("response text");
208+
209+
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
210+
"console messages"
211+
);
212+
213+
expect(pageErrors).toMatchSnapshot("page errors");
214+
});
215+
});
216+
});

0 commit comments

Comments
 (0)