|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +constwebpack=require("webpack"); |
| 4 | +constServer=require("../../lib/Server"); |
| 5 | +constconfig=require("../fixtures/client-config/webpack.config"); |
| 6 | +construnBrowser=require("../helpers/run-browser"); |
| 7 | +const[port1,port2]=require("../ports-map")["cross-origin-request"]; |
| 8 | + |
| 9 | +describe("cross-origin requests",()=>{ |
| 10 | +constdevServerPort=port1; |
| 11 | +consthtmlServerPort=port2; |
| 12 | +consthtmlServerHost="127.0.0.1"; |
| 13 | + |
| 14 | +it("should return 403 for cross-origin no-cors non-module script tag requests",async()=>{ |
| 15 | +constcompiler=webpack(config); |
| 16 | +constdevServerOptions={ |
| 17 | +port: devServerPort, |
| 18 | +allowedHosts: "auto", |
| 19 | +}; |
| 20 | +constserver=newServer(devServerOptions,compiler); |
| 21 | + |
| 22 | +awaitserver.start(); |
| 23 | + |
| 24 | +// Start a separate server for serving the HTML file |
| 25 | +consthttp=require("http"); |
| 26 | +consthtmlServer=http.createServer((req,res)=>{ |
| 27 | +res.writeHead(200,{"Content-Type": "text/html"}); |
| 28 | +res.end(` |
| 29 | + <html> |
| 30 | + <head> |
| 31 | + <script src="http://localhost:${devServerPort}/main.js"></script> |
| 32 | + </head> |
| 33 | + <body></body> |
| 34 | + </html> |
| 35 | + `); |
| 36 | +}); |
| 37 | +htmlServer.listen(htmlServerPort,htmlServerHost); |
| 38 | + |
| 39 | +const{ page, browser }=awaitrunBrowser(); |
| 40 | +try{ |
| 41 | +constpageErrors=[]; |
| 42 | + |
| 43 | +page.on("pageerror",(error)=>{ |
| 44 | +pageErrors.push(error); |
| 45 | +}); |
| 46 | + |
| 47 | +constscriptTagRequest=page.waitForResponse( |
| 48 | +`http://localhost:${devServerPort}/main.js` |
| 49 | +); |
| 50 | + |
| 51 | +awaitpage.goto(`http://${htmlServerHost}:${htmlServerPort}`); |
| 52 | + |
| 53 | +constresponse=awaitscriptTagRequest; |
| 54 | + |
| 55 | +expect(response.status()).toBe(403); |
| 56 | +}catch(error){ |
| 57 | +throwerror; |
| 58 | +}finally{ |
| 59 | +awaitbrowser.close(); |
| 60 | +awaitserver.stop(); |
| 61 | +htmlServer.close(); |
| 62 | +} |
| 63 | +}); |
| 64 | + |
| 65 | +it("should return 200 for cross-origin cors non-module script tag requests",async()=>{ |
| 66 | +constcompiler=webpack(config); |
| 67 | +constdevServerOptions={ |
| 68 | +port: devServerPort, |
| 69 | +allowedHosts: "auto", |
| 70 | +headers: { |
| 71 | +"Access-Control-Allow-Origin": "*", |
| 72 | +}, |
| 73 | +}; |
| 74 | +constserver=newServer(devServerOptions,compiler); |
| 75 | + |
| 76 | +awaitserver.start(); |
| 77 | + |
| 78 | +// Start a separate server for serving the HTML file |
| 79 | +consthttp=require("http"); |
| 80 | +consthtmlServer=http.createServer((req,res)=>{ |
| 81 | +res.writeHead(200,{"Content-Type": "text/html"}); |
| 82 | +res.end(` |
| 83 | + <html> |
| 84 | + <head> |
| 85 | + <script src="http://localhost:${devServerPort}/main.js" crossorigin></script> |
| 86 | + </head> |
| 87 | + <body></body> |
| 88 | + </html> |
| 89 | + `); |
| 90 | +}); |
| 91 | +htmlServer.listen(htmlServerPort,htmlServerHost); |
| 92 | + |
| 93 | +const{ page, browser }=awaitrunBrowser(); |
| 94 | +try{ |
| 95 | +constpageErrors=[]; |
| 96 | + |
| 97 | +page.on("pageerror",(error)=>{ |
| 98 | +pageErrors.push(error); |
| 99 | +}); |
| 100 | + |
| 101 | +constscriptTagRequest=page.waitForResponse( |
| 102 | +`http://localhost:${devServerPort}/main.js` |
| 103 | +); |
| 104 | + |
| 105 | +awaitpage.goto(`http://${htmlServerHost}:${htmlServerPort}`); |
| 106 | + |
| 107 | +constresponse=awaitscriptTagRequest; |
| 108 | + |
| 109 | +expect(response.status()).toBe(200); |
| 110 | +}catch(error){ |
| 111 | +throwerror; |
| 112 | +}finally{ |
| 113 | +awaitbrowser.close(); |
| 114 | +awaitserver.stop(); |
| 115 | +htmlServer.close(); |
| 116 | +} |
| 117 | +}); |
| 118 | +}); |
0 commit comments