-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest.js
63 lines (56 loc) · 1.68 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
var sse, express, app, EventSource, http, assert;
sse = require('./index')();
express = require('express');
EventSource = require('eventsource');
http = require('http');
assert = require('assert');
app = express();
server = http.createServer(app);
app.get('/events', sse, function (req, res) {
res.json("this is an event");
setTimeout(function() {
res.json({here: "is", another: "event"});
res.json({here: "is a named event", to: "listen with addEventListener('example', function(){})"}, "example");
}, 100);
});
process.stdout.write("start server...");
server.listen(0, 'localhost', function() {
var expected_messages, url, source;
console.log("OK");
process.stdout.write("checking messages...");
url = "http://localhost:" + server.address().port + "/events";
source = new EventSource(url);
source.onerror = function(error) {
assert(false, error);
};
message_validators = [
function (msg) {
assert(msg === "this is an event");
},
function (msg) {
assert(msg.here === "is");
assert(msg.another === "event");
},
function (msg) {
assert(msg.here === "is a named event");
assert(msg.to === "listen with addEventListener('example', function(){})");
}
];
source.onmessage = function(e) {
var validate;
validate = message_validators.shift();
validate(JSON.parse(e.data));
process.stdout.write('.');
};
source.addEventListener("example", function(e) {
var validate;
validate = message_validators.shift();
validate(JSON.parse(e.data));
process.stdout.write('.');
if (message_validators.length === 0) {
console.log("OK");
source.close();
server.close();
}
});
});