|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | 3 | const https = require('https'); |
| 4 | +let http2; |
| 5 | +try { |
| 6 | + http2 = require('http2'); // eslint-disable-line global-require |
| 7 | +} catch (_) { |
| 8 | + // eslint-disable-line no-empty |
| 9 | +} |
4 | 10 | const fs = require('fs'); |
5 | 11 | const path = require('path'); |
6 | 12 | const should = require('should'); |
@@ -1359,3 +1365,62 @@ describe('request.get(url).query(vals) works as expected', function () { |
1359 | 1365 | }); |
1360 | 1366 | }); |
1361 | 1367 | }); |
| 1368 | + |
| 1369 | +const describeHttp2 = (http2) ? describe : describe.skip; |
| 1370 | +describeHttp2('http2', function() { |
| 1371 | + // eslint-disable-next-line global-require |
| 1372 | + const proxyquire = require('proxyquire'); |
| 1373 | + |
| 1374 | + const tests = [ |
| 1375 | + { |
| 1376 | + title: 'request(app)', |
| 1377 | + api: request, |
| 1378 | + mockApi: proxyquire('../index.js', { http2: null }) |
| 1379 | + }, |
| 1380 | + { |
| 1381 | + title: 'request.agent(app)', |
| 1382 | + api: request.agent, |
| 1383 | + mockApi: proxyquire('../lib/agent.js', { http2: null }) |
| 1384 | + } |
| 1385 | + ]; |
| 1386 | + |
| 1387 | + tests.forEach(({ title, api, mockApi }) => { |
| 1388 | + describe(title, function () { |
| 1389 | + const app = function(req, res) { |
| 1390 | + res.end('hey'); |
| 1391 | + }; |
| 1392 | + |
| 1393 | + it('should fire up the app on an ephemeral port', function (done) { |
| 1394 | + api(app, { http2: true }) |
| 1395 | + .get('/') |
| 1396 | + .end(function (err, res) { |
| 1397 | + res.status.should.equal(200); |
| 1398 | + res.text.should.equal('hey'); |
| 1399 | + done(); |
| 1400 | + }); |
| 1401 | + }); |
| 1402 | + |
| 1403 | + it('should work with an active server', function (done) { |
| 1404 | + const server = http2.createServer(app); |
| 1405 | + |
| 1406 | + server.listen(function () { |
| 1407 | + api(server) |
| 1408 | + .get('/') |
| 1409 | + .http2() |
| 1410 | + .end(function (err, res) { |
| 1411 | + res.status.should.equal(200); |
| 1412 | + res.text.should.equal('hey'); |
| 1413 | + // close the external server explictly |
| 1414 | + server.close(done); |
| 1415 | + }); |
| 1416 | + }); |
| 1417 | + }); |
| 1418 | + |
| 1419 | + it('should throw error if http2 is not supported', function() { |
| 1420 | + (function() { |
| 1421 | + mockApi(app, { http2: true }); |
| 1422 | + }).should.throw('supertest: this version of Node.js does not support http2'); |
| 1423 | + }); |
| 1424 | + }); |
| 1425 | + }); |
| 1426 | +}); |
0 commit comments