|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const webpack = require('webpack'); |
| 4 | +const internalIp = require('internal-ip'); |
| 5 | +const Server = require('../lib/Server'); |
| 6 | +const createDomain = require('../lib/util/createDomain'); |
| 7 | +const config = require('./fixtures/simple-config/webpack.config'); |
| 8 | + |
| 9 | +describe('check utility funcitons', () => { |
| 10 | + let compiler; |
| 11 | + before(() => { |
| 12 | + compiler = webpack(config); |
| 13 | + }); |
| 14 | + |
| 15 | + const tests = [{ |
| 16 | + name: 'default', |
| 17 | + options: { |
| 18 | + host: 'localhost', |
| 19 | + port: 8080 |
| 20 | + }, |
| 21 | + expected: 'http://localhost:8080' |
| 22 | + }, { |
| 23 | + name: 'https', |
| 24 | + options: { |
| 25 | + host: 'localhost', |
| 26 | + port: 8080, |
| 27 | + https: true |
| 28 | + }, |
| 29 | + expected: 'https://localhost:8080', |
| 30 | + timeout: 60000 |
| 31 | + }, { |
| 32 | + name: 'override with public', |
| 33 | + options: { |
| 34 | + host: 'localhost', |
| 35 | + port: 8080, |
| 36 | + public: 'myhost.test' |
| 37 | + }, |
| 38 | + expected: 'http://myhost.test' |
| 39 | + }, { |
| 40 | + name: 'override with public (port)', |
| 41 | + options: { |
| 42 | + host: 'localhost', |
| 43 | + port: 8080, |
| 44 | + public: 'myhost.test:9090' |
| 45 | + }, |
| 46 | + expected: 'http://myhost.test:9090' |
| 47 | + }, { |
| 48 | + name: 'override with public (protocol)', |
| 49 | + options: { |
| 50 | + host: 'localhost', |
| 51 | + port: 8080, |
| 52 | + public: 'https://myhost.test' |
| 53 | + }, |
| 54 | + expected: 'https://myhost.test' |
| 55 | + }, { |
| 56 | + name: 'override with public (protocol + port)', |
| 57 | + options: { |
| 58 | + host: 'localhost', |
| 59 | + port: 8080, |
| 60 | + public: 'https://myhost.test:9090' |
| 61 | + }, |
| 62 | + expected: 'https://myhost.test:9090' |
| 63 | + }, { |
| 64 | + name: 'localIp', |
| 65 | + options: { |
| 66 | + useLocalIp: true, |
| 67 | + port: 8080 |
| 68 | + }, |
| 69 | + expected: `http://${internalIp.v4()}:8080` |
| 70 | + }]; |
| 71 | + |
| 72 | + tests.forEach((t) => { |
| 73 | + const itInstance = it(`test createDomain '${t.name}'`, (done) => { |
| 74 | + const options = t.options; |
| 75 | + const server = new Server(compiler, options); |
| 76 | + const expected = t.expected; |
| 77 | + server.listen(options.port, options.host, (err) => { |
| 78 | + if (err) { |
| 79 | + done(err); |
| 80 | + } |
| 81 | + const generatedDomain = createDomain(options, server.listeningApp); |
| 82 | + if (generatedDomain !== expected) { |
| 83 | + done(`generated domain ${generatedDomain} doesn't match expected ${expected}`); |
| 84 | + } else { |
| 85 | + done(); |
| 86 | + } |
| 87 | + server.close(); |
| 88 | + }); |
| 89 | + }); |
| 90 | + if (t.timeout) { |
| 91 | + itInstance.timeout(t.timeout); |
| 92 | + } |
| 93 | + }); |
| 94 | +}); |
0 commit comments