diff --git a/lib/eventsource.js b/lib/eventsource.js index 816440d..f980a6c 100644 --- a/lib/eventsource.js +++ b/lib/eventsource.js @@ -230,6 +230,7 @@ function EventSource (url, eventSourceInitDict) { var callback = function (res) { // Handle HTTP redirects if (res.statusCode === 301 || res.statusCode === 307) { + res.destroy() if (!res.headers.location) { // Server sent redirect response without Location header. failOnce({ status: res.statusCode, headers: res.headers, message: res.statusMessage }) @@ -243,6 +244,7 @@ function EventSource (url, eventSourceInitDict) { // Handle HTTP errors if (res.statusCode !== 200) { + res.destroy() failOnce({ status: res.statusCode, headers: res.headers, message: res.statusMessage }) return } diff --git a/test/eventsource_test.js b/test/eventsource_test.js index 1a527a6..9ec8b88 100644 --- a/test/eventsource_test.js +++ b/test/eventsource_test.js @@ -3,6 +3,7 @@ var EventSource = require('../lib/eventsource').EventSource var bufferFrom = require('buffer-from') var mocha = require('mocha') var assert = require('assert') +var http = require('http') var tunnel = require('tunnel') const { AsyncQueue, TestHttpHandlers, TestHttpServers, sleepAsync, withCloseable } = require('launchdarkly-js-test-helpers') @@ -27,6 +28,15 @@ async function withEventSource (server, opts, action) { await withCloseable(es, action || opts) } +async function withKeepAliveAgent (action) { + const agent = new http.Agent({ keepAlive: true }) + try { + await action(agent) + } finally { + agent.destroy() + } +} + async function waitForOpenEvent (es) { const opened = new AsyncQueue() es.onopen = e => opened.add(e) @@ -66,6 +76,13 @@ function writeEvents (chunks, headers = {}) { return TestHttpHandlers.chunkedStream(200, resHeaders, q) } +function notifyOnSocketClose (queue, handler) { + return (req, res) => { + res.socket.on('close', () => queue.add(true)) + handler(req, res) + } +} + function assertRange (min, max, value) { if (value < min || value > max) { throw new Error('' + value + ' was not in range [' + min + ', ' + max + ']') @@ -529,6 +546,38 @@ describe('HTTP Request', () => { }) }) }) + + it('closes the connection of an http ' + status + ' redirect', async () => { + const redirectSuffix = '/foobar' + + await withKeepAliveAgent(async agent => { + await withServer(async server => { + const closed = new AsyncQueue() + server.forMethodAndPath('get', '/', notifyOnSocketClose(closed, + TestHttpHandlers.respond(status, {'Location': server.url + redirectSuffix}))) + server.forMethodAndPath('get', redirectSuffix, writeEvents(['data: hello\n\n'])) + + await withEventSource(server, { agent }, async es => { + await closed.take() + }) + }) + }) + }) + }); + + [301, 307, 401, 503].forEach(function (status) { + it('closes the connection when response status is ' + status, async () => { + await withKeepAliveAgent(async agent => { + await withServer(async server => { + const closed = new AsyncQueue() + server.byDefault(notifyOnSocketClose(closed, TestHttpHandlers.respond(status))) + + await withEventSource(server, { agent }, async es => { + await closed.take() + }) + }) + }) + }) }); [401, 403].forEach(function (status) {