-
Notifications
You must be signed in to change notification settings - Fork 283
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Streaming] create NodeWebSocketFactory, refactor code, new tests (#1331
- Loading branch information
Showing
14 changed files
with
424 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
libraries/botframework-streaming/src/webSocket/factories/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* @module botframework-streaming | ||
*/ | ||
/** | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. | ||
*/ | ||
|
||
export * from './nodeWebSocketFactory'; | ||
export * from './nodeWebSocketFactoryBase'; |
32 changes: 32 additions & 0 deletions
32
libraries/botframework-streaming/src/webSocket/factories/nodeWebSocketFactory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* @module botframework-streaming | ||
*/ | ||
/** | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. | ||
*/ | ||
|
||
import { IncomingMessage } from 'http'; | ||
import { Socket } from 'net'; | ||
|
||
import { NodeWebSocket } from '../nodeWebSocket'; | ||
import { NodeWebSocketFactoryBase } from './nodeWebSocketFactoryBase'; | ||
|
||
export class NodeWebSocketFactory extends NodeWebSocketFactoryBase { | ||
constructor() { | ||
super(); | ||
} | ||
|
||
/** | ||
* Creates a NodeWebSocket instance. | ||
* @param req | ||
* @param socket | ||
* @param head | ||
*/ | ||
public createWebSocket(req: IncomingMessage, socket: Socket, head: Buffer): NodeWebSocket { | ||
const s = new NodeWebSocket(); | ||
s.create(req, socket, head); | ||
|
||
return s; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
libraries/botframework-streaming/src/webSocket/factories/nodeWebSocketFactoryBase.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* @module botframework-streaming | ||
*/ | ||
/** | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. | ||
*/ | ||
|
||
import { IncomingMessage } from 'http'; | ||
import { Socket } from 'net'; | ||
import { ISocket } from '../../interfaces'; | ||
|
||
export abstract class NodeWebSocketFactoryBase { | ||
public abstract createWebSocket(req: IncomingMessage, socket: Socket, head: Buffer): ISocket; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
libraries/botframework-streaming/tests/NodeWebSocket.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
const { NodeWebSocket } = require('../'); | ||
const { expect } = require('chai'); | ||
const { FauxSock, TestRequest } = require('./helpers'); | ||
|
||
describe('NodeSocket', () => { | ||
it('creates a new NodeSocket', () => { | ||
const ns = new NodeWebSocket(new FauxSock); | ||
expect(ns).to.be.instanceOf(NodeWebSocket); | ||
expect(ns.close()).to.not.be.undefined; | ||
}); | ||
|
||
it('requires a valid URL', () => { | ||
try { | ||
const ns = new NodeWebSocket(new FauxSock); | ||
} catch (error) { | ||
expect(error.message).to.equal('Invalid URL: fakeURL'); | ||
} | ||
}); | ||
|
||
it('starts out connected', () => { | ||
const ns = new NodeWebSocket(new FauxSock); | ||
expect(ns.isConnected()).to.be.true; | ||
}); | ||
|
||
it('writes to the socket', () => { | ||
const ns = new NodeWebSocket(new FauxSock); | ||
const buff = Buffer.from('hello'); | ||
expect(ns.write(buff)).to.not.throw; | ||
}); | ||
|
||
it('attempts to open a connection', () => { | ||
const ns = new NodeWebSocket(new FauxSock); | ||
expect(ns.connect().catch((error) => { | ||
expect(error.message).to.equal('connect ECONNREFUSED 127.0.0.1:8082'); | ||
})); | ||
}); | ||
|
||
it('can set message handlers on the socket', () => { | ||
const sock = new FauxSock(); | ||
const ns = new NodeWebSocket(sock); | ||
expect(sock.textHandler).to.be.undefined; | ||
expect(sock.binaryHandler).to.be.undefined; | ||
expect(ns.setOnMessageHandler(() => { })).to.not.throw; | ||
expect(sock.textHandler).to.not.be.undefined; | ||
expect(sock.binaryHandler).to.not.be.undefined; | ||
}); | ||
|
||
it('can set error handler on the socket', () => { | ||
const sock = new FauxSock(); | ||
const ns = new NodeWebSocket(sock); | ||
expect(sock.errorHandler).to.be.undefined; | ||
expect(ns.setOnErrorHandler(() => { })).to.not.throw; | ||
expect(sock.errorHandler).to.not.be.undefined; | ||
}); | ||
|
||
it('can set end handler on the socket', () => { | ||
const sock = new FauxSock(); | ||
const ns = new NodeWebSocket(sock); | ||
expect(sock.endHandler).to.be.undefined; | ||
expect(ns.setOnCloseHandler(() => { })).to.not.throw; | ||
expect(sock.endHandler).to.not.be.undefined; | ||
}); | ||
|
||
it('create() should be successful and set a WebSocket', () => { | ||
const sock = new FauxSock(); | ||
const nodeSocket = new NodeWebSocket(); | ||
const request = new TestRequest(); | ||
request.setIsUpgradeRequest(true); | ||
request.headers = []; | ||
request.headers['upgrade'] = 'websocket'; | ||
request.headers['sec-websocket-key'] = 'BFlat'; | ||
request.headers['sec-websocket-version'] = '13'; | ||
request.headers['sec-websocket-protocol'] = ''; | ||
nodeSocket.create(request, sock, Buffer.from([])); | ||
nodeSocket.waterShedSocket.destroy(); | ||
}); | ||
}); |
Oops, something went wrong.