|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/* eslint-disable |
| 4 | + class-methods-use-this |
| 5 | +*/ |
| 6 | +const request = require('supertest'); |
| 7 | +const sockjs = require('sockjs'); |
| 8 | +const SockJSServer = require('../lib/servers/SockJSServer'); |
| 9 | +const config = require('./fixtures/simple-config/webpack.config'); |
| 10 | +const testServer = require('./helpers/test-server'); |
| 11 | +const BaseServer = require('./../lib/servers/BaseServer'); |
| 12 | + |
| 13 | +describe('serverMode', () => { |
| 14 | + let server; |
| 15 | + let req; |
| 16 | + |
| 17 | + afterEach((done) => { |
| 18 | + testServer.close(done); |
| 19 | + req = null; |
| 20 | + server = null; |
| 21 | + }); |
| 22 | + describe("supplying 'sockjs' as a string", () => { |
| 23 | + beforeEach((done) => { |
| 24 | + server = testServer.start( |
| 25 | + config, |
| 26 | + { |
| 27 | + serverMode: 'sockjs', |
| 28 | + }, |
| 29 | + done |
| 30 | + ); |
| 31 | + req = request('http://localhost:8080'); |
| 32 | + }); |
| 33 | + |
| 34 | + it('sockjs path responds with a 200', (done) => { |
| 35 | + req.get('/sockjs-node').expect(200, done); |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + describe('supplying path to sockjs implementation', () => { |
| 40 | + beforeEach((done) => { |
| 41 | + server = testServer.start( |
| 42 | + config, |
| 43 | + { |
| 44 | + serverMode: require.resolve('../lib/servers/SockJSServer'), |
| 45 | + }, |
| 46 | + done |
| 47 | + ); |
| 48 | + req = request('http://localhost:8080'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('sockjs path responds with a 200', (done) => { |
| 52 | + req.get('/sockjs-node').expect(200, done); |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + describe('supplying sockjs implementation class', () => { |
| 57 | + beforeEach((done) => { |
| 58 | + server = testServer.start( |
| 59 | + config, |
| 60 | + { |
| 61 | + serverMode: SockJSServer, |
| 62 | + }, |
| 63 | + done |
| 64 | + ); |
| 65 | + req = request('http://localhost:8080'); |
| 66 | + }); |
| 67 | + |
| 68 | + it('sockjs path responds with a 200', (done) => { |
| 69 | + req.get('/sockjs-node').expect(200, done); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + describe('custom sockjs implementation', () => { |
| 74 | + it('uses supplied server implementation', (done) => { |
| 75 | + server = testServer.start( |
| 76 | + config, |
| 77 | + { |
| 78 | + sockPath: '/foo/test/bar/', |
| 79 | + serverMode: class MySockJSServer extends BaseServer { |
| 80 | + constructor(serv) { |
| 81 | + super(serv); |
| 82 | + this.socket = sockjs.createServer({ |
| 83 | + // Use provided up-to-date sockjs-client |
| 84 | + sockjs_url: '/__webpack_dev_server__/sockjs.bundle.js', |
| 85 | + // Limit useless logs |
| 86 | + log: (severity, line) => { |
| 87 | + if (severity === 'error') { |
| 88 | + this.server.log.error(line); |
| 89 | + } else { |
| 90 | + this.server.log.debug(line); |
| 91 | + } |
| 92 | + }, |
| 93 | + }); |
| 94 | + |
| 95 | + this.socket.installHandlers(this.server.listeningApp, { |
| 96 | + prefix: this.server.sockPath, |
| 97 | + }); |
| 98 | + |
| 99 | + expect(server.options.sockPath).toEqual('/foo/test/bar/'); |
| 100 | + } |
| 101 | + |
| 102 | + send(connection, message) { |
| 103 | + connection.write(message); |
| 104 | + } |
| 105 | + |
| 106 | + close(connection) { |
| 107 | + connection.close(); |
| 108 | + } |
| 109 | + |
| 110 | + onConnection(f) { |
| 111 | + this.socket.on('connection', f); |
| 112 | + } |
| 113 | + }, |
| 114 | + }, |
| 115 | + done |
| 116 | + ); |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + describe('supplying nonexistent path', () => { |
| 121 | + it('should throw an error', () => { |
| 122 | + expect(() => { |
| 123 | + server = testServer.start( |
| 124 | + config, |
| 125 | + { |
| 126 | + serverMode: '/bad/path/to/implementation', |
| 127 | + }, |
| 128 | + () => {} |
| 129 | + ); |
| 130 | + }).toThrow(/serverMode must be a string/); |
| 131 | + }); |
| 132 | + }); |
| 133 | +}); |
0 commit comments