1// Copyright Joyent, Inc. and other Node contributors. 2// 3// Permission is hereby granted, free of charge, to any person obtaining a 4// copy of this software and associated documentation files (the 5// "Software"), to deal in the Software without restriction, including 6// without limitation the rights to use, copy, modify, merge, publish, 7// distribute, sublicense, and/or sell copies of the Software, and to permit 8// persons to whom the Software is furnished to do so, subject to the 9// following conditions: 10// 11// The above copyright notice and this permission notice shall be included 12// in all copies or substantial portions of the Software. 13// 14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20// USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22'use strict'; 23const common = require('../common'); 24const assert = require('assert'); 25const http = require('http'); 26const net = require('net'); 27const util = require('util'); 28 29// First, we test an HTTP/1.0 request. 30function testHttp10(port, callback) { 31 const c = net.createConnection(port); 32 33 c.setEncoding('utf8'); 34 35 c.on('connect', () => { 36 c.write('GET / HTTP/1.0\r\n\r\n'); 37 }); 38 39 let res_buffer = ''; 40 c.on('data', (chunk) => { 41 res_buffer += chunk; 42 }); 43 44 c.on('end', function() { 45 c.end(); 46 assert.ok( 47 !/x-foo/.test(res_buffer), 48 `No trailer in HTTP/1.0 response. Response buffer: ${res_buffer}` 49 ); 50 callback(); 51 }); 52} 53 54// Now, we test an HTTP/1.1 request. 55function testHttp11(port, callback) { 56 const c = net.createConnection(port); 57 58 c.setEncoding('utf8'); 59 60 let tid; 61 c.on('connect', function() { 62 c.write('GET / HTTP/1.1\r\n\r\n'); 63 tid = setTimeout(common.mustNotCall(), 2000, 'Couldn\'t find last chunk.'); 64 }); 65 66 let res_buffer = ''; 67 c.on('data', function(chunk) { 68 res_buffer += chunk; 69 if (/0\r\n/.test(res_buffer)) { // got the end. 70 clearTimeout(tid); 71 assert.ok( 72 /0\r\nx-foo: bar\r\n\r\n$/.test(res_buffer), 73 `No trailer in HTTP/1.1 response. Response buffer: ${res_buffer}` 74 ); 75 callback(); 76 } 77 }); 78} 79 80// Now, see if the client sees the trailers. 81function testClientTrailers(port, callback) { 82 http.get({ port, path: '/hello', headers: {} }, (res) => { 83 res.on('end', function() { 84 assert.ok('x-foo' in res.trailers, 85 `${util.inspect(res.trailers)} misses the 'x-foo' property`); 86 callback(); 87 }); 88 res.resume(); 89 }); 90} 91 92const server = http.createServer((req, res) => { 93 res.writeHead(200, [['content-type', 'text/plain']]); 94 res.addTrailers({ 'x-foo': 'bar' }); 95 res.end('stuff\n'); 96}); 97server.listen(0, () => { 98 Promise.all([testHttp10, testHttp11, testClientTrailers] 99 .map(util.promisify) 100 .map((f) => f(server.address().port))) 101 .then(() => server.close()); 102}); 103