Skip to content

Commit

Permalink
Add createConnection option for http or https requests (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
xv2 authored and rexxars committed Jan 25, 2020
1 parent fe907fd commit 4ad734b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/eventsource.js
Expand Up @@ -98,6 +98,10 @@ function EventSource (url, eventSourceInitDict) {
// but for now exists as a backwards-compatibility layer
options.rejectUnauthorized = !(eventSourceInitDict && !eventSourceInitDict.rejectUnauthorized)

if (eventSourceInitDict && eventSourceInitDict.createConnection !== undefined) {
options.createConnection = eventSourceInitDict.createConnection
}

// If specify http proxy, make the request to sent to the proxy server,
// and include the original url in path and Host headers
var useProxy = eventSourceInitDict && eventSourceInitDict.proxy
Expand Down
26 changes: 26 additions & 0 deletions test/eventsource_test.js
Expand Up @@ -8,6 +8,7 @@ var fs = require('fs')
var mocha = require('mocha')
var assert = require('assert')
var u = require('url')
var net = require('net')

var it = mocha.it
var describe = mocha.describe
Expand Down Expand Up @@ -599,6 +600,31 @@ describe('HTTP Request', function () {
})
})
})

it('checks createConnection option', function (done) {
createServer(function (err, server) {
if (err) return done(err)

var testResult = false

server.on('request', function () {
assert.ok(testResult)
server.close(done)
})

var urlObj = u.parse(server.url)

new EventSource(server.url, {
createConnection: function () {
var connection = net.createConnection({ port: urlObj.port, host: urlObj.hostname })
connection.on('connect', function () {
testResult = true
})
return connection
}
})
})
})
})

describe('HTTPS Support', function () {
Expand Down

0 comments on commit 4ad734b

Please sign in to comment.