|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const webpack = require("webpack"); |
| 4 | +const Server = require("../../lib/Server"); |
| 5 | +const config = require("../fixtures/client-config/webpack.config"); |
| 6 | +const runBrowser = require("../helpers/run-browser"); |
| 7 | +const port = require("../ports-map")["magic-html-option"]; |
| 8 | + |
| 9 | +describe("magicHtml option", () => { |
| 10 | + describe("enabled", () => { |
| 11 | + let compiler; |
| 12 | + let server; |
| 13 | + let page; |
| 14 | + let browser; |
| 15 | + let pageErrors; |
| 16 | + let consoleMessages; |
| 17 | + |
| 18 | + beforeEach(async () => { |
| 19 | + compiler = webpack(config); |
| 20 | + server = new Server({ port, magicHtml: true }, compiler); |
| 21 | + |
| 22 | + await server.start(); |
| 23 | + |
| 24 | + ({ page, browser } = await runBrowser()); |
| 25 | + |
| 26 | + pageErrors = []; |
| 27 | + consoleMessages = []; |
| 28 | + }); |
| 29 | + |
| 30 | + afterEach(async () => { |
| 31 | + await browser.close(); |
| 32 | + await server.stop(); |
| 33 | + }); |
| 34 | + |
| 35 | + it("should handle GET request to magic async html", async () => { |
| 36 | + page |
| 37 | + .on("console", (message) => { |
| 38 | + consoleMessages.push(message); |
| 39 | + }) |
| 40 | + .on("pageerror", (error) => { |
| 41 | + pageErrors.push(error); |
| 42 | + }); |
| 43 | + |
| 44 | + const response = await page.goto(`http://127.0.0.1:${port}/main`, { |
| 45 | + waitUntil: "networkidle0", |
| 46 | + }); |
| 47 | + |
| 48 | + expect(response.headers()["content-type"]).toMatchSnapshot( |
| 49 | + "response headers content-type" |
| 50 | + ); |
| 51 | + |
| 52 | + expect(response.status()).toMatchSnapshot("response status"); |
| 53 | + |
| 54 | + expect(consoleMessages.map((message) => message.text())).toMatchSnapshot( |
| 55 | + "console messages" |
| 56 | + ); |
| 57 | + }); |
| 58 | + |
| 59 | + it("should handle HEAD request to magic async html", async () => { |
| 60 | + await page.setRequestInterception(true); |
| 61 | + |
| 62 | + page |
| 63 | + .on("console", (message) => { |
| 64 | + consoleMessages.push(message); |
| 65 | + }) |
| 66 | + .on("pageerror", (error) => { |
| 67 | + pageErrors.push(error); |
| 68 | + }) |
| 69 | + .on("request", (interceptedRequest) => { |
| 70 | + interceptedRequest.continue({ method: "HEAD" }); |
| 71 | + }); |
| 72 | + |
| 73 | + const response = await page.goto(`http://127.0.0.1:${port}/main`, { |
| 74 | + waitUntil: "networkidle0", |
| 75 | + }); |
| 76 | + |
| 77 | + expect(response.headers()["content-type"]).toMatchSnapshot( |
| 78 | + "response headers content-type" |
| 79 | + ); |
| 80 | + |
| 81 | + expect(response.status()).toMatchSnapshot("response status"); |
| 82 | + |
| 83 | + expect(consoleMessages.map((message) => message.text())).toMatchSnapshot( |
| 84 | + "console messages" |
| 85 | + ); |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + describe("disabled", () => { |
| 90 | + let compiler; |
| 91 | + let server; |
| 92 | + let page; |
| 93 | + let browser; |
| 94 | + let pageErrors; |
| 95 | + let consoleMessages; |
| 96 | + |
| 97 | + beforeEach(async () => { |
| 98 | + compiler = webpack(config); |
| 99 | + server = new Server({ port, magicHtml: false }, compiler); |
| 100 | + |
| 101 | + await server.start(); |
| 102 | + |
| 103 | + ({ page, browser } = await runBrowser()); |
| 104 | + |
| 105 | + pageErrors = []; |
| 106 | + consoleMessages = []; |
| 107 | + }); |
| 108 | + |
| 109 | + afterEach(async () => { |
| 110 | + await browser.close(); |
| 111 | + await server.stop(); |
| 112 | + }); |
| 113 | + |
| 114 | + it("should not handle GET request to magic async html", async () => { |
| 115 | + page |
| 116 | + .on("console", (message) => { |
| 117 | + consoleMessages.push(message); |
| 118 | + }) |
| 119 | + .on("pageerror", (error) => { |
| 120 | + pageErrors.push(error); |
| 121 | + }); |
| 122 | + |
| 123 | + const response = await page.goto(`http://127.0.0.1:${port}/main`, { |
| 124 | + waitUntil: "networkidle0", |
| 125 | + }); |
| 126 | + |
| 127 | + expect(response.headers()["content-type"]).toMatchSnapshot( |
| 128 | + "response headers content-type" |
| 129 | + ); |
| 130 | + |
| 131 | + expect(response.status()).toMatchSnapshot("response status"); |
| 132 | + |
| 133 | + expect(consoleMessages.map((message) => message.text())).toMatchSnapshot( |
| 134 | + "console messages" |
| 135 | + ); |
| 136 | + }); |
| 137 | + |
| 138 | + it("should not handle HEAD request to magic async html", async () => { |
| 139 | + await page.setRequestInterception(true); |
| 140 | + |
| 141 | + page |
| 142 | + .on("console", (message) => { |
| 143 | + consoleMessages.push(message); |
| 144 | + }) |
| 145 | + .on("pageerror", (error) => { |
| 146 | + pageErrors.push(error); |
| 147 | + }) |
| 148 | + .on("request", (interceptedRequest) => { |
| 149 | + interceptedRequest.continue({ method: "HEAD" }); |
| 150 | + }); |
| 151 | + |
| 152 | + const response = await page.goto(`http://127.0.0.1:${port}/main`, { |
| 153 | + waitUntil: "networkidle0", |
| 154 | + }); |
| 155 | + |
| 156 | + expect(response.headers()["content-type"]).toMatchSnapshot( |
| 157 | + "response headers content-type" |
| 158 | + ); |
| 159 | + |
| 160 | + expect(response.status()).toMatchSnapshot("response status"); |
| 161 | + |
| 162 | + expect(consoleMessages.map((message) => message.text())).toMatchSnapshot( |
| 163 | + "console messages" |
| 164 | + ); |
| 165 | + }); |
| 166 | + }); |
| 167 | +}); |
0 commit comments