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

[BUG #212] Send data should be serialized to String #213

Merged
merged 1 commit into from
Jun 29, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/helpers/normalize-send.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function normalizeSendData(data) {
if (Object.prototype.toString.call(data) !== '[object Blob]' && !(data instanceof ArrayBuffer)) {
Copy link

@shanshanzhu shanshanzhu Dec 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works for us only when the mock-socket to be run in browser environment. In node, binary data can be instanceof Buffer
we should also check for !(data instanceof Buffer) to support mock-socket to be run in Node environment.

data = String(data);
}

return data;
}
4 changes: 4 additions & 0 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import networkBridge from './network-bridge';
import { CLOSE_CODES } from './constants';
import globalObject from './helpers/global-object';
import dedupe from './helpers/dedupe';
import normalizeSendData from './helpers/normalize-send';
import { createEvent, createMessageEvent, createCloseEvent } from './event/factory';

/*
Expand Down Expand Up @@ -113,6 +114,9 @@ class Server extends EventTarget {

if (typeof options !== 'object' || arguments.length > 3) {
data = Array.prototype.slice.call(arguments, 1, arguments.length);
data = data.map(item => normalizeSendData(item));
} else {
data = normalizeSendData(data);
}

websockets.forEach(socket => {
Expand Down
15 changes: 5 additions & 10 deletions src/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import lengthInUtf8Bytes from './helpers/byte-length';
import urlVerification from './helpers/url-verification';
import protocolVerification from './helpers/protocol-verification';
import delay from './helpers/delay';
import normalizeSendData from './helpers/normalize-send';
import EventTarget from './event/target';
import networkBridge from './network-bridge';
import { CLOSE_CODES, ERROR_PREFIX } from './constants';
Expand All @@ -13,7 +14,7 @@ import { closeWebSocketConnection, failWebSocketConnection } from './algorithms/
* The main websocket class which is designed to mimick the native WebSocket class as close
* as possible.
*
* https://developer.mozilla.org/en-US/docs/Web/API/WebSocket
* https://html.spec.whatwg.org/multipage/web-sockets.html
*/
class WebSocket extends EventTarget {
constructor(url, protocols) {
Expand Down Expand Up @@ -126,20 +127,17 @@ class WebSocket extends EventTarget {
this.addEventListener('error', listener);
}

/*
* Transmits data to the server over the WebSocket connection.
*
* https://developer.mozilla.org/en-US/docs/Web/API/WebSocket#send()
*/
send(data) {
if (this.readyState === WebSocket.CLOSING || this.readyState === WebSocket.CLOSED) {
throw new Error('WebSocket is already in CLOSING or CLOSED state');
}

// TODO: handle bufferedAmount

const messageEvent = createMessageEvent({
type: 'message',
origin: this.url,
data
data: normalizeSendData(data)
});

const server = networkBridge.serverLookup(this.url);
Expand All @@ -151,9 +149,6 @@ class WebSocket extends EventTarget {
}
}

/*
* https://html.spec.whatwg.org/multipage/web-sockets.html#dom-websocket-close
*/
close(code, reason) {
if (code !== undefined) {
if (typeof code !== 'number' || (code !== 1000 && (code < 3000 || code > 4999))) {
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,18 @@ test('that calling close will trigger the onclose of websockets', t => {
t.is(myServer.originalWebSocket, null, 'server forgets about the original websocket');
t.deepEqual(globalObj.WebSocket, originalWebSocket, 'the original websocket is returned to the global object');
});

test.cb('that send will normalize data', t => {
const myServer = new Server('ws://not-real/');

myServer.on('connection', (server, socket) => {
myServer.send([1, 2]);
});

const socketFoo = new WebSocket('ws://not-real/');
socketFoo.onmessage = message => {
t.is(message.data, '1,2', 'data non string, non blob/arraybuffers get toStringed');
myServer.close();
t.end();
};
});