UNPKG

@softonic/http-log-format

Version:

Functions to transform native Node.js HTTP requests and responses to the Softonic HTTP log format

191 lines (188 loc) 6.11 kB
"use strict"; var _index = require("../index"); var _HttpBuilders = require("./HttpBuilders"); describe('http-log-format', () => { describe('formatRequest(request, { whitelistHeaders, blacklistHeaders })', () => { it('should extract the fields from incoming requests', () => { const request = (0, _HttpBuilders.buildIncomingRequest)({ httpVersion: '1.1', method: 'GET', protocol: 'http:', host: 'example.com', port: '80', path: '/home', headers: { foo: 'bar' }, localAddress: '127.0.0.1', localPort: 80, remoteAddress: '8.8.8.8', remotePort: 12345, timestamp: '2017-04-01T23:01:52Z' }); const formattedRequest = (0, _index.formatRequest)(request); expect(formattedRequest).toEqual({ method: 'GET', httpVersion: '1.1', url: '/home', headers: expect.objectContaining({ host: 'example.com:80', foo: 'bar' }), localAddress: '127.0.0.1', localPort: 80, remoteAddress: '8.8.8.8', remotePort: 12345, timestamp: '2017-04-01T23:01:52Z' }); }); it('should extract the fields from outgoing requests', () => { const request = (0, _HttpBuilders.buildOutgoingRequest)({ method: 'GET', protocol: 'http:', host: 'example.com', port: '80', path: '/home', headers: { foo: 'bar' }, localAddress: '127.0.0.1', localPort: 12345, remoteAddress: '8.8.8.8', remotePort: 80, timestamp: '2017-04-01T23:01:52Z' }); const formattedRequest = (0, _index.formatRequest)(request); expect(formattedRequest).toEqual({ timestamp: '2017-04-01T23:01:52Z', method: 'GET', httpVersion: undefined, // no httpVersion in outgoing requests url: '/home', headers: expect.objectContaining({ host: 'example.com:80', foo: 'bar' }), localAddress: '127.0.0.1', localPort: 12345, remoteAddress: '8.8.8.8', remotePort: 80 }); }); it('should whitelist headers from the request', () => { const request = (0, _HttpBuilders.buildIncomingRequest)({ headers: { foo: 'bar', bar: 'baz' } }); const formattedRequest = (0, _index.formatRequest)(request, { whitelistHeaders: ['foo'] }); expect(formattedRequest.headers.foo).toBe('bar'); expect(formattedRequest.headers.bar).toBeUndefined(); }); it('should blacklist headers from the request', () => { const request = (0, _HttpBuilders.buildIncomingRequest)({ headers: { foo: 'bar', bar: 'baz' } }); const formattedRequest = (0, _index.formatRequest)(request, { blacklistHeaders: ['foo'] }); expect(formattedRequest.headers.foo).toBeUndefined(); expect(formattedRequest.headers.bar).toBe('baz'); }); }); describe('stringifyRequest(request)', () => { it('should convert a request object to its string version', () => { const request = (0, _HttpBuilders.buildIncomingRequest)({ method: 'POST', protocol: 'http:', host: 'example.com', port: '80', path: '/home' }); const stringified = (0, _index.stringifyRequest)(request); expect(stringified).toBe('POST example.com:80/home'); }); }); describe('formatResponse(response, { whitelistHeaders, blacklistHeaders })', () => { it('should extract the fields from incoming responses', () => { const response = (0, _HttpBuilders.buildIncomingResponse)({ timestamp: '2017-04-01T23:01:52Z', responseTime: 25, headers: { foo: 'bar' }, statusCode: 200 }); const formattedResponse = (0, _index.formatResponse)(response); expect(formattedResponse).toEqual({ timestamp: '2017-04-01T23:01:52Z', responseTime: 25, headers: expect.objectContaining({ foo: 'bar' }), statusCode: 200 }); }); it('should extract the fields from outgoing responses', () => { const response = (0, _HttpBuilders.buildOutgoingResponse)({ timestamp: '2017-04-01T23:01:52Z', responseTime: 25, headers: { foo: 'bar' }, statusCode: 200 }); const formattedResponse = (0, _index.formatResponse)(response); expect(formattedResponse).toEqual({ timestamp: '2017-04-01T23:01:52Z', responseTime: 25, headers: expect.objectContaining({ foo: 'bar' }), statusCode: 200 }); }); it('should whitelist headers from the response', () => { const response = (0, _HttpBuilders.buildOutgoingResponse)({ headers: { foo: 'bar', bar: 'baz' } }); const formattedResponse = (0, _index.formatResponse)(response, { whitelistHeaders: ['foo'] }); expect(formattedResponse.headers.foo).toBe('bar'); expect(formattedResponse.headers.bar).toBeUndefined(); }); it('should blacklist headers from the response', () => { const response = (0, _HttpBuilders.buildOutgoingResponse)({ headers: { foo: 'bar', bar: 'baz' } }); const formattedResponse = (0, _index.formatResponse)(response, { blacklistHeaders: ['foo'] }); expect(formattedResponse.headers.foo).toBeUndefined(); expect(formattedResponse.headers.bar).toBe('baz'); }); }); describe('stringifyResponse(response)', () => { it('should convert a response object to its string version', () => { const response = (0, _HttpBuilders.buildIncomingResponse)({ statusCode: 503 }); const stringified = (0, _index.stringifyResponse)(response); expect(stringified).toBe('503 (Service Unavailable)'); }); }); }); //# sourceMappingURL=index.spec.js.map