|
| 1 | +/* global beforeEach, describe, it */ |
| 2 | + |
| 3 | +var assert = require("assert"); |
| 4 | + |
| 5 | +var handler; |
| 6 | + |
| 7 | +describe('handler/shipHttp.js', function() { |
| 8 | + describe('#process()', function() { |
| 9 | + beforeEach(function() { |
| 10 | + // Clear the module cache to reset overridden functions. |
| 11 | + delete require.cache[require.resolve('../handlers/shipHttp')]; |
| 12 | + handler = require('../handlers/shipHttp'); |
| 13 | + }); |
| 14 | + |
| 15 | + it('should ship data using the http module', |
| 16 | + function(done) { |
| 17 | + var http = require('http'); |
| 18 | + var httpReqSent = false; |
| 19 | + var httpReqEnded = false; |
| 20 | + http.request = function(options, _callback) { |
| 21 | + var callback = _callback; |
| 22 | + assert.strictEqual(options.method, 'POST', 'HTTP method provided'); |
| 23 | + assert.strictEqual(options.protocol, 'http:', |
| 24 | + 'HTTP protocol provided'); |
| 25 | + assert.strictEqual(options.hostname, 'mock', 'HTTP host provided'); |
| 26 | + assert.strictEqual(options.pathname, '/logs', 'HTTP path provided'); |
| 27 | + assert.strictEqual(options.query, 'app=abc123', |
| 28 | + 'HTTP query provided'); |
| 29 | + return { |
| 30 | + end: function() { |
| 31 | + httpReqEnded = true; |
| 32 | + callback({statusCode: 201}); |
| 33 | + }, |
| 34 | + on: function(event, _callback) { |
| 35 | + return; |
| 36 | + }, |
| 37 | + write: function(data) { |
| 38 | + assert.strictEqual(data, 'test data 123456', |
| 39 | + 'data written in request'); |
| 40 | + httpReqSent = true; |
| 41 | + } |
| 42 | + }; |
| 43 | + }; |
| 44 | + var config = { |
| 45 | + http: { |
| 46 | + url: 'http://mock/logs?app=abc123' |
| 47 | + }, |
| 48 | + data: 'test data 123456', |
| 49 | + test: 'test' |
| 50 | + }; |
| 51 | + handler.process(config) |
| 52 | + .then(function(result) { |
| 53 | + assert.ok(httpReqSent, 'HTTP request sent'); |
| 54 | + assert.ok(httpReqEnded, 'HTTP request closed'); |
| 55 | + assert.strictEqual(result.test, 'test', 'config data returned'); |
| 56 | + done(); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should fail if the HTTP request throws an error', |
| 61 | + function(done) { |
| 62 | + var http = require('http'); |
| 63 | + http.request = function(options, _callback) { |
| 64 | + return { |
| 65 | + end: function() { |
| 66 | + }, |
| 67 | + on: function(event, callback) { |
| 68 | + callback(new Error('test error')); |
| 69 | + }, |
| 70 | + write: function(data) { |
| 71 | + } |
| 72 | + }; |
| 73 | + }; |
| 74 | + var config = { |
| 75 | + http: { |
| 76 | + url: 'http://mock/logs?app=abc123' |
| 77 | + }, |
| 78 | + data: 'test data 123456', |
| 79 | + test: 'test' |
| 80 | + }; |
| 81 | + handler.process(config) |
| 82 | + .catch(function(err) { |
| 83 | + assert.ok(err, 'error is thrown'); |
| 84 | + done(); |
| 85 | + }); |
| 86 | + }); |
| 87 | + }); |
| 88 | +}); |
0 commit comments