-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathssestream_test.js
More file actions
143 lines (125 loc) · 3.22 KB
/
ssestream_test.js
File metadata and controls
143 lines (125 loc) · 3.22 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
'use strict'
const assert = require('assert')
const Stream = require('stream')
const http = require('http')
const EventSource = require('eventsource')
const SseStream = require('..')
const written = stream => new Promise((resolve, reject) => stream.on('error', reject).on('finish', resolve))
class Sink extends Stream.Writable {
constructor() {
super({ objectMode: true })
this._chunks = []
}
_write(chunk, encoding, callback) {
this._chunks.push(chunk)
callback()
}
get content() {
return this._chunks.join('')
}
}
describe('SseStream', () => {
let sse, sink
beforeEach(() => {
sse = new SseStream()
sink = new Sink()
})
it('writes multiple multiline messages', async () => {
sse.pipe(sink)
sse.write({
data: 'hello\nworld',
})
sse.write({
data: 'bonjour\nmonde',
})
sse.end()
await written(sink)
assert.equal(
sink.content,
`:ok
data: hello
data: world
data: bonjour
data: monde
`
)
})
it('writes object messages as JSON', async () => {
sse.pipe(sink)
sse.write({
data: { hello: 'world' },
})
sse.end()
await written(sink)
assert.equal(sink.content, ':ok\n\ndata: {"hello":"world"}\n\n')
})
it('writes all message attributes', async () => {
sse.pipe(sink)
sse.write({
comment: 'jibber jabber',
event: 'tea-time',
id: 'the-id',
retry: 222,
data: 'hello',
})
sse.end()
await written(sink)
assert.equal(
sink.content,
`:ok
: jibber jabber
event: tea-time
id: the-id
retry: 222
data: hello
`
)
})
it('sets headers on destination when it looks like a HTTP Response', callback => {
sink.writeHead = (status, headers) => {
assert.deepEqual(headers, {
'Content-Type': 'text/event-stream; charset=utf-8',
'Transfer-Encoding': 'identity',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
})
callback()
}
sse.pipe(sink)
})
it('extends and replaces default headers with custom ones', callback => {
sink.writeHead = (status, headers) => {
assert.deepEqual(headers, {
'Content-Type': 'text/event-stream; charset=utf-8',
'Transfer-Encoding': 'identity',
// 'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Cache-Control': 'no-cache, no-transform', // replace existing header
'Custom-Header': 'foo' // add custom header
})
callback()
}
const customHeaders = {
'Cache-Control': 'no-cache, no-transform', // replace existing header
'Custom-Header': 'foo' // add custom header
}
sse.pipe(sink, undefined, customHeaders)
})
it('allows an eventsource to connect', callback => {
const server = http.createServer((req, res) => {
sse = new SseStream(req)
sse.pipe(res)
})
server.listen(err => {
if (err) return callback(err)
const es = new EventSource(`http://localhost:${server.address().port}`)
es.onmessage = e => {
assert.equal(e.data, 'hello')
callback()
}
es.onopen = () => sse.write({data: 'hello'})
es.onerror = e =>
callback(new Error(`Error from EventSource: ${JSON.stringify(e)}`))
})
})
})