-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Add an integration test for WebSocket #11433
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
Changes from 1 commit
8bc60e6
81bab38
06c897a
3f10a62
117725e
eb794b1
d424414
4ba83e2
76f6883
abc0103
85ec4c2
7274a29
53fdd5e
5315921
3e76249
83128ef
81de2aa
515bc88
cac5bed
0aa1d9f
bc1c018
68eb8fd
f930694
304399d
04de66b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| /** | ||
| * Copyright (c) 2015-present, Facebook, Inc. | ||
| * All rights reserved. | ||
| * | ||
| * This source code is licensed under the BSD-style license found in the | ||
| * LICENSE file in the root directory of this source tree. An additional grant | ||
| * of patent rights can be found in the PATENTS file in the same directory. | ||
| * | ||
| * @flow | ||
| */ | ||
| 'use strict'; | ||
|
|
||
| var React = require('react'); | ||
| var ReactNative = require('react-native'); | ||
| var { View } = ReactNative; | ||
| var { TestModule } = ReactNative.NativeModules; | ||
|
|
||
| const DEFAULT_WS_URL = 'ws://localhost:5555/'; | ||
|
|
||
| const WS_EVENTS = [ | ||
| 'close', | ||
| 'error', | ||
| 'message', | ||
| 'open', | ||
| ]; | ||
| const WS_STATES = [ | ||
| /* 0 */ 'CONNECTING', | ||
| /* 1 */ 'OPEN', | ||
| /* 2 */ 'CLOSING', | ||
| /* 3 */ 'CLOSED', | ||
| ]; | ||
|
|
||
| class WebSocketTest extends React.Component { | ||
| state: State = { | ||
| url: DEFAULT_WS_URL, | ||
| fetchStatus: null, | ||
| socket: null, | ||
| socketState: null, | ||
| lastSocketEvent: null, | ||
| lastMessage: null, | ||
| outgoingMessage: '', | ||
| }; | ||
|
|
||
| _waitFor = (condition: Function, timeout: Number, callback: Function) => { | ||
| var remaining = timeout; | ||
| var t; | ||
| var timeoutFunction = function() { | ||
| if (condition()) { | ||
| callback(true); | ||
| return; | ||
| } | ||
| remaining--; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Number The operand of an arithmetic operation must be a number. |
||
| if (remaining === 0) { | ||
| callback(false); | ||
| } else { | ||
| t = setTimeout(timeoutFunction,1000); | ||
| } | ||
| }; | ||
| t = setTimeout(timeoutFunction,1000); | ||
| } | ||
|
|
||
| _connect = () => { | ||
| const socket = new WebSocket(this.state.url); | ||
| WS_EVENTS.forEach(ev => socket.addEventListener(ev, this._onSocketEvent)); | ||
| this.setState({ | ||
| socket, | ||
| socketState: socket.readyState, | ||
| }); | ||
| }; | ||
|
|
||
| _socketIsConnected = () => { | ||
| return this.state.socketState === 1; //'OPEN' | ||
| } | ||
|
|
||
| _socketIsDisconnected = () => { | ||
| return this.state.socketState === 3; //'CLOSED' | ||
| } | ||
|
|
||
| _disconnect = () => { | ||
| if (!this.state.socket) { | ||
| return; | ||
| } | ||
| this.state.socket.close(); | ||
| }; | ||
|
|
||
| _onSocketEvent = (event: any) => { | ||
| const state: any = { | ||
| socketState: event.target.readyState, | ||
| lastSocketEvent: event.type, | ||
| }; | ||
| if (event.type === 'message') { | ||
| state.lastMessage = event.data; | ||
| } | ||
| this.setState(state); | ||
| }; | ||
|
|
||
| _sendText = () => { | ||
| if (!this.state.socket) { | ||
| return; | ||
| } | ||
| this.state.socket.send(this.state.outgoingMessage); | ||
| this.setState({outgoingMessage: ''}); | ||
| }; | ||
|
|
||
| componentDidMount() { | ||
| debugger; | ||
| this.testConnectAndDisconnect(); | ||
| } | ||
|
|
||
| testConnectAndDisconnect = () => { | ||
| var connectSucceeded = true; | ||
| var component = this; | ||
| component._connect(); | ||
| component._waitFor(component._socketIsConnected,5,function(result) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. number This type is incompatible with the expected param type of Number |
||
|
|
||
| connectSucceeded = result; | ||
| if(!connectSucceeded) { | ||
| TestModule.markTestPassed(false); | ||
| TestModule.markTestCompleted(); | ||
| return; | ||
| } | ||
| var disconnectSucceeded = true; | ||
| component._disconnect(); | ||
| component._waitFor(component._socketIsDisconnected,5,function(result2) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. number This type is incompatible with the expected param type of Number |
||
| disconnectSucceeded = result2; | ||
| TestModule.markTestPassed(disconnectSucceeded); | ||
| TestModule.markTestCompleted(); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| render(): React.Element<any> { | ||
| return <View />; | ||
| } | ||
| } | ||
|
|
||
| WebSocketTest.displayName = 'WebSocketTest'; | ||
|
|
||
| module.exports = WebSocketTest; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| # Copyright (c) 2015-present, Facebook, Inc. | ||
| # All rights reserved. | ||
| # | ||
| # This source code is licensed under the BSD-style license found in the | ||
| # LICENSE file in the root directory of this source tree. An additional grant | ||
| # of patent rights can be found in the PATENTS file in the same directory. | ||
|
|
||
| # Set terminal title | ||
| echo -en "\033]0;Web Socket Test Server\a" | ||
| clear | ||
|
|
||
| THIS_DIR=$(dirname "$0") | ||
| pushd "$THIS_DIR" | ||
| ./websocket_integration_test_server.js & | ||
| popd | ||
|
|
||
| echo "Process terminated. Press <enter> to close the window" | ||
| read |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| /** | ||
| * Copyright (c) 2013-present, Facebook, Inc. | ||
| * All rights reserved. | ||
| * | ||
| * This source code is licensed under the BSD-style license found in the | ||
| * LICENSE file in the root directory of this source tree. An additional grant | ||
| * of patent rights can be found in the PATENTS file in the same directory. | ||
| * | ||
| * The examples provided by Facebook are for non-commercial testing and | ||
| * evaluation purposes only. | ||
| * | ||
| * Facebook reserves all rights not expressly granted. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
| * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL | ||
| * FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN | ||
| * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
| * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| * | ||
| * @flow | ||
| */ | ||
| 'use strict'; | ||
|
|
||
| /* eslint-env node */ | ||
|
|
||
| const WebSocket = require('ws'); | ||
|
|
||
| console.log(`\ | ||
| WebSocket integration test server | ||
|
|
||
| This will send each incoming message back, with the string '_response' appended. | ||
| An incoming message of 'exit' will shut down the server. | ||
|
|
||
| `); | ||
|
|
||
| const server = new WebSocket.Server({port: 5555}); | ||
| server.on('connection', (ws) => { | ||
| ws.on('message', (message) => { | ||
| console.log('Received message:', message); | ||
| if (message === 'exit') { | ||
| console.log('WebSocket integration test server exit'); | ||
| process.exit(0); | ||
| } | ||
| console.log('Cookie:', ws.upgradeReq.headers.cookie); | ||
| ws.send(message + '_response'); | ||
| }); | ||
|
|
||
| ws.send('hello'); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
identifier
StateCould not resolve name