|
| 1 | +/* |
| 2 | + * Copyright (c) 2017, Trent Mick. |
| 3 | + * |
| 4 | + * Test the bunyan CLI's handling of the "client_req" field. |
| 5 | + * "client_req" is a common-ish Bunyan log field from restify-clients. See: |
| 6 | + * // JSSTYLED |
| 7 | + * https://github.com/restify/clients/blob/85374f87db9f4469de2605b6b15632b317cc12be/lib/helpers/bunyan.js#L213 |
| 8 | + */ |
| 9 | + |
| 10 | +var exec = require('child_process').exec; |
| 11 | +var fs = require('fs'); |
| 12 | +var path = require('path'); |
| 13 | +var _ = require('util').format; |
| 14 | +var vasync = require('vasync'); |
| 15 | + |
| 16 | +// node-tap API |
| 17 | +if (require.cache[__dirname + '/tap4nodeunit.js']) |
| 18 | + delete require.cache[__dirname + '/tap4nodeunit.js']; |
| 19 | +var tap4nodeunit = require('./tap4nodeunit.js'); |
| 20 | +var after = tap4nodeunit.after; |
| 21 | +var before = tap4nodeunit.before; |
| 22 | +var test = tap4nodeunit.test; |
| 23 | + |
| 24 | + |
| 25 | +// ---- globals |
| 26 | + |
| 27 | +var BUNYAN = path.resolve(__dirname, '../bin/bunyan'); |
| 28 | + |
| 29 | + |
| 30 | +// ---- tests |
| 31 | + |
| 32 | +// The bunyan CLI shouldn't add a "Host:" line if client_req.headers has one. |
| 33 | +// See <https://github.com/trentm/node-bunyan/issues/504>. |
| 34 | +test('client_req renders without extra Host header', function (t) { |
| 35 | + exec(_('%s %s/corpus/issue504-client-req-with-host.log', BUNYAN, __dirname), |
| 36 | + function (err, stdout, stderr) { |
| 37 | + t.ifError(err) |
| 38 | + t.equal(stdout, [ |
| 39 | + // JSSTYLED |
| 40 | + '[2017-05-12T23:59:15.877Z] TRACE: minfo/66266 on sharptooth.local: request sent (client_req.address=127.0.0.1)', |
| 41 | + ' HEAD /dap/stor HTTP/1.1', |
| 42 | + ' accept: application/json, */*', |
| 43 | + ' host: foo.example.com', |
| 44 | + ' date: Fri, 12 May 2017 23:59:15 GMT', |
| 45 | + '' |
| 46 | + ].join('\n')); |
| 47 | + t.end(); |
| 48 | + }); |
| 49 | +}); |
| 50 | + |
| 51 | +test('client_req added Host header elides port 80', function (t) { |
| 52 | + exec(_('%s %s/corpus/issue504-client-req-with-port80.log', |
| 53 | + BUNYAN, __dirname), |
| 54 | + function (err, stdout, stderr) { |
| 55 | + t.ifError(err) |
| 56 | + t.equal(stdout, [ |
| 57 | + // JSSTYLED |
| 58 | + '[2017-05-12T23:59:15.877Z] TRACE: minfo/66266 on sharptooth.local: request sent', |
| 59 | + ' HEAD /dap/stor HTTP/1.1', |
| 60 | + ' Host: 127.0.0.1', |
| 61 | + ' accept: application/json, */*', |
| 62 | + ' date: Fri, 12 May 2017 23:59:15 GMT', |
| 63 | + '' |
| 64 | + ].join('\n')); |
| 65 | + t.end(); |
| 66 | + }); |
| 67 | +}); |
| 68 | + |
| 69 | +test('client_req added Host header elides port 443', function (t) { |
| 70 | + exec(_('%s %s/corpus/issue504-client-req-with-port443.log', |
| 71 | + BUNYAN, __dirname), |
| 72 | + function (err, stdout, stderr) { |
| 73 | + t.ifError(err) |
| 74 | + t.equal(stdout, [ |
| 75 | + // JSSTYLED |
| 76 | + '[2017-05-12T23:59:15.877Z] TRACE: minfo/66266 on sharptooth.local: request sent', |
| 77 | + ' HEAD /dap/stor HTTP/1.1', |
| 78 | + ' Host: 127.0.0.1', |
| 79 | + ' accept: application/json, */*', |
| 80 | + ' date: Fri, 12 May 2017 23:59:15 GMT', |
| 81 | + '' |
| 82 | + ].join('\n')); |
| 83 | + t.end(); |
| 84 | + }); |
| 85 | +}); |
| 86 | + |
| 87 | +test('client_req added Host header, non-default port', function (t) { |
| 88 | + exec(_('%s %s/corpus/issue504-client-req-with-port8080.log', |
| 89 | + BUNYAN, __dirname), |
| 90 | + function (err, stdout, stderr) { |
| 91 | + t.ifError(err) |
| 92 | + t.equal(stdout, [ |
| 93 | + // JSSTYLED |
| 94 | + '[2017-05-12T23:59:15.877Z] TRACE: minfo/66266 on sharptooth.local: request sent', |
| 95 | + ' HEAD /dap/stor HTTP/1.1', |
| 96 | + ' Host: 127.0.0.1:8080', |
| 97 | + ' accept: application/json, */*', |
| 98 | + ' date: Fri, 12 May 2017 23:59:15 GMT', |
| 99 | + '' |
| 100 | + ].join('\n')); |
| 101 | + t.end(); |
| 102 | + }); |
| 103 | +}); |
| 104 | + |
| 105 | +test('client_req without address does not add Host header', function (t) { |
| 106 | + exec(_('%s %s/corpus/issue504-client-req-without-address.log', |
| 107 | + BUNYAN, __dirname), |
| 108 | + function (err, stdout, stderr) { |
| 109 | + t.ifError(err) |
| 110 | + t.equal(stdout, [ |
| 111 | + // JSSTYLED |
| 112 | + '[2017-05-12T23:59:15.877Z] TRACE: minfo/66266 on sharptooth.local: request sent', |
| 113 | + ' HEAD /dap/stor HTTP/1.1', |
| 114 | + ' accept: application/json, */*', |
| 115 | + ' date: Fri, 12 May 2017 23:59:15 GMT', |
| 116 | + '' |
| 117 | + ].join('\n')); |
| 118 | + t.end(); |
| 119 | + }); |
| 120 | +}); |
0 commit comments