Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no-cache and https support #10

Merged
merged 2 commits into from
Feb 11, 2012
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 7 additions & 4 deletions lib/eventsource.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var http = require('http')
, https =require('https')
, util = require('util')
, events = require('events')
, eventstream = require('./eventstream');
Expand Down Expand Up @@ -26,6 +27,7 @@ function EventSource(url) {
var self = this;
self.reconnectInterval = 1000;
var options = require('url').parse(url);
var isSecure = options.protocol == 'https:';

var closedByMe = false;
function onConnectionClosed() {
Expand All @@ -42,11 +44,12 @@ function EventSource(url) {
var lastEventId = '';
var req;
function connect() {
if (lastEventId != '') {
options.headers = { 'last-event-id': lastEventId };
}
options.headers = {
'Cache-Control': 'no-cache'
};
if (lastEventId) options.headers['last-event-id'] = lastEventId;

req = http.request(options, function(res) {
req = (isSecure ? https : http).request(options, function(res) {
readyState = EventSource.OPEN;
_emit('open');

Expand Down
13 changes: 13 additions & 0 deletions test/certificate.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-----BEGIN CERTIFICATE-----
MIICATCCAWoCCQDPufXH86n2QzANBgkqhkiG9w0BAQUFADBFMQswCQYDVQQGEwJu
bzETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0
cyBQdHkgTHRkMB4XDTEyMDEwMTE0NDQwMFoXDTIwMDMxOTE0NDQwMFowRTELMAkG
A1UEBhMCbm8xEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0
IFdpZGdpdHMgUHR5IEx0ZDCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAtrQ7
+r//2iV/B6F+4boH0XqFn7alcV9lpjvAmwRXNKnxAoa0f97AjYPGNLKrjpkNXXhB
JROIdbRbZnCNeC5fzX1a+JCo7KStzBXuGSZr27TtFmcV4H+9gIRIcNHtZmJLnxbJ
sIhkGR8yVYdmJZe4eT5ldk1zoB1adgPF1hZhCBMCAwEAATANBgkqhkiG9w0BAQUF
AAOBgQCeWBEHYJ4mCB5McwSSUox0T+/mJ4W48L/ZUE4LtRhHasU9hiW92xZkTa7E
QLcoJKQiWfiLX2ysAro0NX4+V8iqLziMqvswnPzz5nezaOLE/9U/QvH3l8qqNkXu
rNbsW1h/IO6FV8avWFYVFoutUwOaZ809k7iMh2F2JMgXQ5EymQ==
-----END CERTIFICATE-----
71 changes: 65 additions & 6 deletions test/eventsource_test.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
var http = require('http');
var EventSource = require('eventsource');
var EventSource = require('eventsource')
, http = require('http')
, https = require('https')
, fs = require('fs');

var port = 20000;
function createServer(chunks, callback, onreq) {
function createServer(chunks, callback, onreq, secure) {
var options = {};
var isSecure = onreq === true || secure === true;
if (isSecure) {
options = {
key: fs.readFileSync(__dirname + '/key.pem'),
cert: fs.readFileSync(__dirname + '/certificate.pem')
};
}
var responses = [];
var server = http.createServer(function (req, res) {
if (onreq) onreq(req);
function open(req, res) {
if (typeof onreq == 'function') onreq(req);
res.writeHead(200, {'Content-Type': 'text/event-stream'});
chunks.forEach(function(chunk) {
res.write(chunk);
});
res.write(':'); // send a dummy comment to ensure that the response is flushed
responses.push(res);
});
}
function close(closed) {
responses.forEach(function(res) {
res.end();
});
server.on('close', closed);
server.close();
}
var server;
if (isSecure) server = https.createServer(options, open);
else server = http.createServer(open);
server.listen(port, function() {
callback(close);
});
Expand Down Expand Up @@ -219,6 +232,52 @@ exports['Messages'] = {
},
};

exports['HTTP Request'] = {
setUp: function(done) {
port++;
done();
},

'passes cache-control: no-cache to server': function(test) {
var headers;
createServer([], function(close) {
var url = 'http://localhost:' + port;
var es = new EventSource(url);
es.onopen = function() {
test.equal('no-cache', headers['cache-control']);
es.close();
close(test.done);
}
}, function(req) { headers = req.headers; });
},
};

exports['HTTPS Support'] = {
setUp: function(done) {
port++;
done();
},

'uses https for https urls': function(test) {
var chopped = "data: Aslak\n\ndata: Hellesøy\n\n".split("");
createServer(chopped, function(close) {
var es = new EventSource('https://localhost:' + port);
es.onmessage = first;

function first(m) {
test.equal("Aslak", m.data);
es.onmessage = second;
}

function second(m) {
test.equal("Hellesøy", m.data);
es.close();
close(test.done);
}
}, true);
},
};

exports['Reconnect'] = {
setUp: function(done) {
port++;
Expand Down
15 changes: 15 additions & 0 deletions test/key.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQC2tDv6v//aJX8HoX7hugfReoWftqVxX2WmO8CbBFc0qfEChrR/
3sCNg8Y0squOmQ1deEElE4h1tFtmcI14Ll/NfVr4kKjspK3MFe4ZJmvbtO0WZxXg
f72AhEhw0e1mYkufFsmwiGQZHzJVh2Yll7h5PmV2TXOgHVp2A8XWFmEIEwIDAQAB
AoGAAlVY8sHi/aE+9xT77twWX3mGHV0SzdjfDnly40fx6S1Gc7bOtVdd9DC7pk6l
3ENeJVR02IlgU8iC5lMHq4JEHPE272jtPrLlrpWLTGmHEqoVFv9AITPqUDLhB9Kk
Hjl7h8NYBKbr2JHKICr3DIPKOT+RnXVb1PD4EORbJ3ooYmkCQQDfknUnVxPgxUGs
ouABw1WJIOVgcCY/IFt4Ihf6VWTsxBgzTJKxn3HtgvE0oqTH7V480XoH0QxHhjLq
DrgobWU9AkEA0TRJ8/ouXGnFEPAXjWr9GdPQRZ1Use2MrFjneH2+Sxc0CmYtwwqL
Kr5kS6mqJrxprJeluSjBd+3/ElxURrEXjwJAUvmlN1OPEhXDmRHd92mKnlkyKEeX
OkiFCiIFKih1S5Y/sRJTQ0781nyJjtJqO7UyC3pnQu1oFEePL+UEniRztQJAMfav
AtnpYKDSM+1jcp7uu9BemYGtzKDTTAYfoiNF42EzSJiGrWJDQn4eLgPjY0T0aAf/
yGz3Z9ErbhMm/Ysl+QJBAL4kBxRT8gM4ByJw4sdOvSeCCANFq8fhbgm8pGWlCPb5
JGmX3/GHFM8x2tbWMGpyZP1DLtiNEFz7eCGktWK5rqE=
-----END RSA PRIVATE KEY-----