|
| 1 | +'use strict' |
| 2 | + |
| 3 | +var server = require('./server') |
| 4 | +var request = require('../index') |
| 5 | +var tape = require('tape') |
| 6 | +var destroyable = require('server-destroy') |
| 7 | + |
| 8 | +var s = server.createHttp2Server() |
| 9 | +destroyable(s) |
| 10 | + |
| 11 | +s.on('/redirect/from', function (req, res) { |
| 12 | + res.writeHead(301, { |
| 13 | + location: '/redirect/to' |
| 14 | + }) |
| 15 | + res.end() |
| 16 | +}) |
| 17 | + |
| 18 | +s.on('/redirect/to', function (req, res) { |
| 19 | + res.end('ok') |
| 20 | +}) |
| 21 | + |
| 22 | +s.on('/headers.json', function (req, res) { |
| 23 | + res.writeHead(200, { |
| 24 | + 'Content-Type': 'application/json' |
| 25 | + }) |
| 26 | + |
| 27 | + res.end(JSON.stringify(req.headers)) |
| 28 | +}) |
| 29 | + |
| 30 | +tape('setup', function (t) { |
| 31 | + s.listen(0, function () { |
| 32 | + tape('cleanup', function (t) { |
| 33 | + s.destroy(function () { |
| 34 | + t.end() |
| 35 | + }) |
| 36 | + }) |
| 37 | + t.end() |
| 38 | + }) |
| 39 | +}) |
| 40 | + |
| 41 | +tape('undefined headers', function (t) { |
| 42 | + request({ |
| 43 | + url: s.url + '/headers.json', |
| 44 | + headers: { |
| 45 | + 'X-TEST-1': 'test1', |
| 46 | + 'X-TEST-2': undefined |
| 47 | + }, |
| 48 | + json: true, |
| 49 | + strictSSL: false, |
| 50 | + protocolVersion: 'http2' |
| 51 | + }, function (err, res, body) { |
| 52 | + t.equal(err, null) |
| 53 | + t.equal(body['x-test-1'], 'test1') |
| 54 | + t.equal(typeof body['x-test-2'], 'undefined') |
| 55 | + t.end() |
| 56 | + }) |
| 57 | +}) |
| 58 | + |
| 59 | +tape('preserve port in authority header if non-standard port', function (t) { |
| 60 | + request({ |
| 61 | + url: s.url + '/headers.json', |
| 62 | + strictSSL: false, |
| 63 | + protocolVersion: 'http2' |
| 64 | + }, function (err, res, body, debug) { |
| 65 | + t.equal(err, null) |
| 66 | + console.log() |
| 67 | + t.equal(debug[0].request.headers.find(({key}) => key === ':authority').value, 'localhost:' + s.port) |
| 68 | + t.end() |
| 69 | + }) |
| 70 | +}) |
| 71 | + |
| 72 | +tape('strip port in authority header if explicit standard port (:443) & protocol (HTTPS)', function (t) { |
| 73 | + request({ |
| 74 | + url: 'https://localhost:443/headers.json', |
| 75 | + strictSSL: false, |
| 76 | + protocolVersion: 'http2' |
| 77 | + }, function (_err, res, body, debug) { |
| 78 | + t.equal(debug[0].request.headers.find(({key}) => key === ':authority').value, 'localhost') |
| 79 | + t.end() |
| 80 | + }) |
| 81 | +}) |
| 82 | + |
| 83 | +tape('strip port in authority header if implicit standard port & protocol (HTTPS)', function (t) { |
| 84 | + request({ |
| 85 | + url: 'https://localhost/headers.json', |
| 86 | + strictSSL: false, |
| 87 | + protocolVersion: 'http2' |
| 88 | + }, function (_err, res, body, debug) { |
| 89 | + t.equal(debug[0].request.headers.find(({key}) => key === ':authority').value, 'localhost') |
| 90 | + t.end() |
| 91 | + }) |
| 92 | +}) |
0 commit comments