|
| 1 | +// Licensed to Elasticsearch B.V. under one or more contributor |
| 2 | +// license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright |
| 4 | +// ownership. Elasticsearch B.V. licenses this file to you under |
| 5 | +// the Apache License, Version 2.0 (the "License"); you may |
| 6 | +// not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +'use strict' |
| 19 | + |
| 20 | +const http = require('http') |
| 21 | + |
| 22 | +const express = require('express') |
| 23 | +const test = require('tap').test |
| 24 | + |
| 25 | +const { |
| 26 | + formatHttpRequest, |
| 27 | + formatHttpResponse |
| 28 | +} = require('../') |
| 29 | + |
| 30 | +test('express res/req serialization', t => { |
| 31 | + const app = express() |
| 32 | + let server |
| 33 | + |
| 34 | + app.get('/', (req, res) => { |
| 35 | + const rec = {} |
| 36 | + |
| 37 | + res.setHeader('Foo', 'Bar') |
| 38 | + res.write('hi') |
| 39 | + res.end() |
| 40 | + |
| 41 | + formatHttpRequest(rec, req) |
| 42 | + formatHttpResponse(rec, res) |
| 43 | + |
| 44 | + t.deepEqual(rec.user_agent, { original: 'cool-agent' }) |
| 45 | + t.deepEqual(rec.url, { |
| 46 | + path: '/', |
| 47 | + full: `http://localhost:${server.address().port}/`, |
| 48 | + domain: 'localhost' |
| 49 | + }) |
| 50 | + t.deepEqual(rec.http, { |
| 51 | + version: '1.1', |
| 52 | + request: { |
| 53 | + method: 'get', |
| 54 | + headers: { |
| 55 | + host: `localhost:${server.address().port}`, |
| 56 | + connection: 'close' |
| 57 | + } |
| 58 | + }, |
| 59 | + response: { |
| 60 | + status_code: 200, |
| 61 | + headers: { |
| 62 | + 'x-powered-by': 'Express', |
| 63 | + foo: 'Bar' |
| 64 | + } |
| 65 | + } |
| 66 | + }) |
| 67 | + // https://www.elastic.co/guide/en/ecs/current/ecs-client.html fields |
| 68 | + t.ok(rec.client, 'client fields are set') |
| 69 | + t.ok(rec.client.address === '127.0.0.1' || rec.client.address === '::ffff:127.0.0.1', |
| 70 | + 'client.address is set') |
| 71 | + t.ok(rec.client.ip === rec.client.address, |
| 72 | + 'client.address duplicated to client.ip') |
| 73 | + t.equal(typeof (rec.client.port), 'number') |
| 74 | + }) |
| 75 | + |
| 76 | + app.listen(0, function () { |
| 77 | + server = this |
| 78 | + const req = http.get( |
| 79 | + `http://localhost:${server.address().port}/`, |
| 80 | + { |
| 81 | + headers: { 'user-agent': 'cool-agent' } |
| 82 | + }, |
| 83 | + function (res) { |
| 84 | + res.on('data', function () {}) |
| 85 | + res.on('end', function () { |
| 86 | + server.close(function () { |
| 87 | + t.end() |
| 88 | + }) |
| 89 | + }) |
| 90 | + } |
| 91 | + ) |
| 92 | + req.on('error', t.ifErr) |
| 93 | + }) |
| 94 | +}) |
0 commit comments