-
Notifications
You must be signed in to change notification settings - Fork 25k
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
Closed
Closed
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
8bc60e6
Add an integration test for WebSocket
douglowder 81bab38
Fix flow errors
douglowder 06c897a
Merge remote-tracking branch 'upstream/master' into websocket-test
douglowder 3f10a62
Fix syntax error
douglowder 117725e
Merge remote-tracking branch 'upstream/master' into websocket-test
douglowder eb794b1
Merge branch 'websocket-test' of https://github.com/dlowder-salesforc…
douglowder d424414
Clean up WebSocket test, add message exchange test
douglowder 4ba83e2
Remove debugger statement
douglowder 76f6883
Merge remote-tracking branch 'upstream/master' into websocket-test
douglowder abc0103
Merge remote-tracking branch 'upstream/master' into websocket-test
douglowder 85ec4c2
Merge remote-tracking branch 'upstream/master' into websocket-test
douglowder 7274a29
Merge remote-tracking branch 'upstream/master' into websocket-test
douglowder 53fdd5e
Merge remote-tracking branch 'upstream/master' into websocket-test
douglowder 5315921
Merge remote-tracking branch 'upstream/master' into websocket-test
douglowder 3e76249
Merge branch 'websocket-test' of https://github.com/dlowder-salesforc…
douglowder 83128ef
Merge remote-tracking branch 'upstream/master' into websocket-test
douglowder 81de2aa
Merge remote-tracking branch 'upstream/master' into websocket-test
douglowder 515bc88
Merge branch 'master' into websocket-test
satya164 cac5bed
Merge remote-tracking branch 'upstream/master' into websocket-test
douglowder 0aa1d9f
Merge remote-tracking branch 'upstream/master' into websocket-test
douglowder bc1c018
Merge remote-tracking branch 'upstream/master' into websocket-test
douglowder 68eb8fd
Merge remote-tracking branch 'upstream/master' into websocket-test
douglowder f930694
Merge remote-tracking branch 'upstream/master' into websocket-test
douglowder 304399d
Merge remote-tracking branch 'upstream/master' into websocket-test
douglowder 04de66b
Only run WebSocket test if packager is running
douglowder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,167 @@ | ||
| /** | ||
| * 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', | ||
| ]; | ||
|
|
||
| type State = { | ||
| url: string; | ||
| fetchStatus: ?string; | ||
| socket: ?WebSocket; | ||
| socketState: ?number; | ||
| lastSocketEvent: ?string; | ||
| lastMessage: ?string | ?ArrayBuffer; | ||
| testMessage: string; | ||
| testExpectedResponse: string; | ||
| }; | ||
|
|
||
| class WebSocketTest extends React.Component { | ||
| state: State = { | ||
| url: DEFAULT_WS_URL, | ||
| fetchStatus: null, | ||
| socket: null, | ||
| socketState: null, | ||
| lastSocketEvent: null, | ||
| lastMessage: null, | ||
| testMessage: 'testMessage', | ||
| testExpectedResponse: 'testMessage_response' | ||
| }; | ||
|
|
||
| _waitFor = (condition: any, timeout: any, callback: any) => { | ||
| 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 = (text: string) => { | ||
| if (!this.state.socket) { | ||
| return; | ||
| } | ||
| this.state.socket.send(text); | ||
| }; | ||
|
|
||
| _sendTestMessage = () => { | ||
| this._sendText(this.state.testMessage); | ||
| }; | ||
|
|
||
| _receivedTestExpectedResponse = () => { | ||
| return (this.state.lastMessage === this.state.testExpectedResponse); | ||
| }; | ||
|
|
||
| componentDidMount() { | ||
| this.testConnect(); | ||
| } | ||
|
|
||
| testConnect = () => { | ||
| var component = this; | ||
| component._connect(); | ||
| component._waitFor(component._socketIsConnected, 5, function(connectSucceeded) { | ||
| if (!connectSucceeded) { | ||
| TestModule.markTestPassed(false); | ||
| return; | ||
| } | ||
| component.testSendAndReceive(); | ||
| }); | ||
| } | ||
|
|
||
| testSendAndReceive = () => { | ||
| var component = this; | ||
| component._sendTestMessage(); | ||
| component._waitFor(component._receivedTestExpectedResponse, 5, function(messageReceived) { | ||
| if (!messageReceived) { | ||
| TestModule.markTestPassed(false); | ||
| return; | ||
| } | ||
| component.testDisconnect(); | ||
| }); | ||
| } | ||
|
|
||
| testDisconnect = () => { | ||
| var component = this; | ||
| component._disconnect(); | ||
| component._waitFor(component._socketIsDisconnected, 5, function(disconnectSucceeded) { | ||
| TestModule.markTestPassed(disconnectSucceeded); | ||
| }); | ||
| } | ||
|
|
||
| render(): React.Element<any> { | ||
| return <View />; | ||
| } | ||
| } | ||
|
|
||
| WebSocketTest.displayName = 'WebSocketTest'; | ||
|
|
||
| module.exports = WebSocketTest; | ||
This file contains hidden or 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,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 |
This file contains hidden or 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,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'); | ||
| }); |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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