Skip to content

Commit

Permalink
Merge pull request #74 from ScalaWilliam/eventsource-500
Browse files Browse the repository at this point in the history
Eventsource 50x support
  • Loading branch information
rexxars committed May 10, 2017
2 parents 3a4445f + 1328a06 commit 91437de
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
7 changes: 7 additions & 0 deletions lib/eventsource.js
Expand Up @@ -110,6 +110,13 @@ function EventSource (url, eventSourceInitDict) {
}

req = (isSecure ? https : http).request(options, function (res) {
// Handle HTTP errors
if (res.statusCode === 500 || res.statusCode === 502 || res.statusCode === 503 || res.statusCode === 504) {
_emit('error', new Event('error', {status: res.statusCode}))
onConnectionClosed()
return
}

// Handle HTTP redirects
if (res.statusCode === 301 || res.statusCode === 307) {
if (!res.headers.location) {
Expand Down
37 changes: 36 additions & 1 deletion test/eventsource_test.js
Expand Up @@ -601,6 +601,41 @@ describe('Reconnection', function () {
})
})

it('is attempted when the server responds with a 500', function (done) {
createServer(function (err, server) {
if (err) return done(err)

server.on('request', function (req, res) {
res.writeHead(500)
res.end()
})

var es = new EventSource(server.url)
es.reconnectInterval = 0

var errored = false

es.onerror = function () {
if (errored) return
errored = true
server.close(function (err) {
if (err) return done(err)

var port = u.parse(es.url).port
configureServer(http.createServer(), 'http', port, function (err, server2) {
if (err) return done(err)

server2.on('request', writeEvents(['data: hello\n\n']))
es.onmessage = function (m) {
assert.equal('hello', m.data)
server2.close(done)
}
})
})
}
})
})

it('is stopped when server goes down and eventsource is being closed', function (done) {
createServer(function (err, server) {
if (err) return done(err)
Expand Down Expand Up @@ -642,7 +677,7 @@ describe('Reconnection', function () {
})
})

it('is not attempted when server responds with non-200', function (done) {
it('is not attempted when server responds with non-200 and non-500', function (done) {
createServer(function (err, server) {
if (err) return done(err)

Expand Down

0 comments on commit 91437de

Please sign in to comment.