Skip to content

Commit 6cb112c

Browse files
authored
Merge pull request #1 from launchdarkly/http-method-and-body
allow setting of HTTP request method and body
2 parents 82d38b0 + 02c66ba commit 6cb112c

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

lib/eventsource.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ function EventSource (url, eventSourceInitDict) {
130130
options.withCredentials = eventSourceInitDict.withCredentials
131131
}
132132

133+
if (eventSourceInitDict && eventSourceInitDict.method) {
134+
options.method = eventSourceInitDict.method
135+
}
136+
133137
req = (isSecure ? https : http).request(options, function (res) {
134138
// Handle HTTP errors
135139
if (res.statusCode === 500 || res.statusCode === 502 || res.statusCode === 503 || res.statusCode === 504) {
@@ -227,6 +231,10 @@ function EventSource (url, eventSourceInitDict) {
227231
})
228232
})
229233

234+
if (eventSourceInitDict && eventSourceInitDict.body) {
235+
req.write(eventSourceInitDict.body)
236+
}
237+
230238
req.on('error', function (err) {
231239
onConnectionClosed(err.message)
232240
})

test/eventsource_test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,42 @@ describe('HTTP Request', function () {
527527
}
528528
)
529529
})
530+
})
531+
532+
it('uses GET method by default', function (done) {
533+
createServer(function (err, server) {
534+
if (err) return done(err)
535+
536+
server.on('request', function (req) {
537+
assert.equal(req.method, 'GET')
538+
server.close(done)
539+
})
540+
541+
new EventSource(server.url)
542+
})
543+
})
544+
545+
it('can specify HTTP method and body', function (done) {
546+
var content = '{ "test": true }'
547+
548+
createServer(function (err, server) {
549+
if (err) return done(err)
550+
551+
server.on('request', function (req) {
552+
assert.equal(req.method, 'POST')
553+
554+
var receivedContent = ''
555+
req.on('data', function (chunk) {
556+
receivedContent += chunk
557+
})
558+
req.on('end', function () {
559+
assert.equal(content, receivedContent)
560+
server.close(done)
561+
})
562+
})
563+
564+
new EventSource(server.url, { method: 'POST', body: content })
565+
})
530566
});
531567

532568
[301, 307].forEach(function (status) {

0 commit comments

Comments
 (0)