Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/eventsource.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand All @@ -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
}
Expand Down
49 changes: 49 additions & 0 deletions test/eventsource_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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)
Expand Down Expand Up @@ -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 + ']')
Expand Down Expand Up @@ -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) {
Expand Down
Loading