|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const path = require('path'); |
| 4 | +const express = require('express'); |
| 5 | +const httpProxy = require('http-proxy-middleware'); |
| 6 | +const request = require('supertest'); |
| 7 | +const runDevServer = require('./helpers/run-webpack-dev-server'); |
| 8 | +const runBrowser = require('./helpers/run-browser'); |
| 9 | + |
| 10 | +const configPath = path.resolve( |
| 11 | + __dirname, |
| 12 | + './fixtures/client-config/webpack.config.js' |
| 13 | +); |
| 14 | + |
| 15 | +function startProxy(port) { |
| 16 | + const proxy = express(); |
| 17 | + proxy.use( |
| 18 | + '/', |
| 19 | + httpProxy({ |
| 20 | + target: 'http://0.0.0.0:8080', |
| 21 | + ws: true, |
| 22 | + changeOrigin: true, |
| 23 | + }) |
| 24 | + ); |
| 25 | + return proxy.listen(port); |
| 26 | +} |
| 27 | + |
| 28 | +describe('Client code', () => { |
| 29 | + let proxy; |
| 30 | + let server; |
| 31 | + |
| 32 | + beforeAll((done) => { |
| 33 | + server = runDevServer('--host 0.0.0.0 --port 8080', configPath); |
| 34 | + proxy = startProxy(9090); |
| 35 | + setTimeout(done, 5000); |
| 36 | + }); |
| 37 | + |
| 38 | + afterAll((done) => { |
| 39 | + proxy.close(); |
| 40 | + server.close(); |
| 41 | + done(); |
| 42 | + }); |
| 43 | + |
| 44 | + describe('behind a proxy', () => { |
| 45 | + jest.setTimeout(20000); |
| 46 | + |
| 47 | + it('responds with a 200', (done) => { |
| 48 | + const req = request('http://localhost:9090'); |
| 49 | + req.get('/sockjs-node').expect(200, 'Welcome to SockJS!\n', done); |
| 50 | + }); |
| 51 | + |
| 52 | + it('requests websocket through the proxy with proper port number', (done) => { |
| 53 | + runBrowser().then(({ page, browser }) => { |
| 54 | + page |
| 55 | + .waitForRequest((requestObj) => requestObj.url().match(/sockjs-node/)) |
| 56 | + .then((requestObj) => { |
| 57 | + expect(requestObj.url()).toMatch( |
| 58 | + /^http:\/\/localhost:9090\/sockjs-node/ |
| 59 | + ); |
| 60 | + browser.close(); |
| 61 | + done(); |
| 62 | + }); |
| 63 | + page.goto('http://localhost:9090/main'); |
| 64 | + }); |
| 65 | + }); |
| 66 | + }); |
| 67 | +}); |
0 commit comments