|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const path = require('path'); |
| 4 | +const request = require('supertest'); |
| 5 | +const semver = require('semver'); |
| 6 | +const helper = require('./helper'); |
| 7 | +const config = require('./fixtures/contentbase-config/webpack.config'); |
| 8 | + |
| 9 | +const contentBasePublic = path.join( |
| 10 | + __dirname, |
| 11 | + 'fixtures/contentbase-config/public' |
| 12 | +); |
| 13 | + |
| 14 | +describe('http2', () => { |
| 15 | + let server; |
| 16 | + let req; |
| 17 | + |
| 18 | + // HTTP/2 will only work with node versions below 10.0.0 |
| 19 | + // since spdy is broken past that point, and this test will only |
| 20 | + // work above Node 8.8.0, since it is the first version where the |
| 21 | + // built-in http2 module is exposed without need for a flag |
| 22 | + // (https://nodejs.org/en/blog/release/v8.8.0/) |
| 23 | + // if someone is testing below this Node version and breaks this, |
| 24 | + // their tests will not catch it, but CI will catch it. |
| 25 | + if ( |
| 26 | + semver.gte(process.version, '8.8.0') && |
| 27 | + semver.lt(process.version, '10.0.0') |
| 28 | + ) { |
| 29 | + /* eslint-disable global-require */ |
| 30 | + const http2 = require('http2'); |
| 31 | + /* eslint-enable global-require */ |
| 32 | + describe('http2 works with https', () => { |
| 33 | + beforeAll((done) => { |
| 34 | + server = helper.start( |
| 35 | + config, |
| 36 | + { |
| 37 | + contentBase: contentBasePublic, |
| 38 | + https: true, |
| 39 | + http2: true, |
| 40 | + }, |
| 41 | + done |
| 42 | + ); |
| 43 | + req = request(server.app); |
| 44 | + }); |
| 45 | + |
| 46 | + it('confirm http2 client can connect', (done) => { |
| 47 | + const client = http2.connect('https://localhost:8080', { |
| 48 | + rejectUnauthorized: false, |
| 49 | + }); |
| 50 | + client.on('error', (err) => console.error(err)); |
| 51 | + |
| 52 | + const http2Req = client.request({ ':path': '/' }); |
| 53 | + |
| 54 | + http2Req.on('response', (headers) => { |
| 55 | + expect(headers[':status']).toEqual(200); |
| 56 | + }); |
| 57 | + |
| 58 | + http2Req.setEncoding('utf8'); |
| 59 | + let data = ''; |
| 60 | + http2Req.on('data', (chunk) => { |
| 61 | + data += chunk; |
| 62 | + }); |
| 63 | + http2Req.on('end', () => { |
| 64 | + expect(data).toEqual(expect.stringMatching(/Heyo/)); |
| 65 | + done(); |
| 66 | + }); |
| 67 | + http2Req.end(); |
| 68 | + }); |
| 69 | + |
| 70 | + afterAll(helper.close); |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + describe('server works with http2 option, but without https option', () => { |
| 75 | + beforeAll((done) => { |
| 76 | + server = helper.start( |
| 77 | + config, |
| 78 | + { |
| 79 | + contentBase: contentBasePublic, |
| 80 | + http2: true, |
| 81 | + }, |
| 82 | + done |
| 83 | + ); |
| 84 | + req = request(server.app); |
| 85 | + }); |
| 86 | + |
| 87 | + it('Request to index', (done) => { |
| 88 | + req.get('/').expect(200, /Heyo/, done); |
| 89 | + }); |
| 90 | + |
| 91 | + afterAll(helper.close); |
| 92 | + }); |
| 93 | + |
| 94 | + describe('https without http2 disables HTTP/2', () => { |
| 95 | + beforeAll((done) => { |
| 96 | + server = helper.start( |
| 97 | + config, |
| 98 | + { |
| 99 | + contentBase: contentBasePublic, |
| 100 | + https: true, |
| 101 | + http2: false, |
| 102 | + }, |
| 103 | + done |
| 104 | + ); |
| 105 | + req = request(server.app); |
| 106 | + }); |
| 107 | + |
| 108 | + it('Request to index', (done) => { |
| 109 | + req |
| 110 | + .get('/') |
| 111 | + .expect(200, /Heyo/) |
| 112 | + .then(({ res }) => { |
| 113 | + expect(res.httpVersion).not.toEqual('2.0'); |
| 114 | + done(); |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + afterAll(helper.close); |
| 119 | + }); |
| 120 | +}); |
0 commit comments