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