Skip to content

Commit 01a34fd

Browse files
committed
handle case where websocket does not have on method; add tests
1 parent e63e718 commit 01a34fd

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

lib/transport/websocket.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@ function WebSocketTransport(transUrl, ignore, options) {
5454
self.emit('close', 1006, 'WebSocket connection broken');
5555
self._cleanup();
5656
};
57-
this.ws.on('drain', function() {
58-
self.emit('drain');
59-
})
57+
if (this.ws.on)
58+
this.ws.on('drain', function() {
59+
self.emit('drain');
60+
});
6061
}
6162

6263
inherits(WebSocketTransport, EventEmitter);

tests/lib/senders.js

+18
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,22 @@ function ajaxStreaming (Obj) {
6363
});
6464
}
6565

66+
function ajaxCustomHeaders (Obj) {
67+
it('simple', function (done) {
68+
var x = new Obj('GET', testUtils.getSameOriginUrl() + '/echo-custom-data-header.txt', null,
69+
{ headers: { 'X-Custom-Data': 'abc' }});
70+
x.on('finish', function (status, text) {
71+
try {
72+
expect(text).to.equal('abc');
73+
} catch (e) {
74+
done(e);
75+
return;
76+
}
77+
done();
78+
});
79+
});
80+
}
81+
6682
function wrongUrl(Obj, url, statuses) {
6783
it('wrong url ' + url, function (done) {
6884
var test = this.runnable();
@@ -110,6 +126,7 @@ describe('Senders', function () {
110126
describe('xhr-local', function () {
111127
ajaxSimple(XhrLocal);
112128
ajaxStreaming(XhrLocal);
129+
ajaxCustomHeaders(XhrLocal);
113130
// TODO senders don't have a timeouts so these tests can fail
114131
// BUT info-receiver has a timeout so they will never not-return
115132
// wrongPort(XhrLocal);
@@ -123,6 +140,7 @@ describe('Senders', function () {
123140
}
124141
ajaxSimple(Xdr);
125142
ajaxStreaming(Xdr);
143+
ajaxCustomHeaders(Xdr);
126144
wrongPort(Xdr);
127145
wrongUrl(Xdr, testUtils.getSameOriginUrl() + '/wrong_url_indeed.txt', [0, 400]);
128146
});

tests/support/sockjs_server.js

+5
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ function startServer(port, config, prefix) {
4242
res.setHeader('Access-Control-Allow-Origin', '*');
4343
res.writeHead(200);
4444
res.end(new Array(2049).join('a') + '\nb\n');
45+
} else if ( /\/echo-custom-data-header.txt/.test(req.url) ) {
46+
res.setHeader('content-type', 'text/plain');
47+
res.setHeader('Access-Control-Allow-Origin', '*');
48+
res.writeHead(200);
49+
res.end(req.headers['x-custom-data'] || '');
4550
} else if (req.url === '/config.js') {
4651
if (req.headers.referer) {
4752
var parsedOrigin = new URL(req.headers.referer);

0 commit comments

Comments
 (0)