Ws: Option to create fake websocket connection for test cases?

Created on 25 Sep 2018  路  6Comments  路  Source: websockets/ws

Is there any option to create fake websocket connections? I want to use this in test environment to check the behaviour of server and client.

const WebSocket = require('ws');
const wss1 = new WebSocket('ws://www.host.com/path');
const wss3 = new WebSocket('ws://www.host.com/path');

In above code wss1 and wss3 are same.
I want to create different connections for both of them.
Is it possible to achieve?

Most helpful comment

All 6 comments

What do you mean with "fake"?
The above code creates two different connections.
If you don't want to establish a connection you can use a custom agent:

https://github.com/websockets/ws/blob/7d7ddfd2e2e010bbdba6acfd9b8fb0f0ab79c951/test/websocket.test.js#L28-L37

Hi @lpinca

I think my question is not clear. Let me try to explain my requirement.

const WebSocket = require('ws');

let wss = new WebSocket.Server({ clientTracking: true, port: 8000 });

wss.on('connection', ws => {
if(ws._socket.remoteAddress == "127.0.0.1"){ // Some code.. }
else{//Code for connection from different url..}
})
const wss1 = new WebSocket('ws://www.host.com/path');
const wss3 = new WebSocket('ws://www.host.com/path');

Now for connection from wss1 and wss3 both satisfies the if condition inside on('connection').

Is there any way I can define some different ip for ws._socket.remoteAddress while connecting to websocket from clients for test environment?

I am getting the error mentioned in the on error part. EADDRNOTAVAIL. @lpinca

It should work on Linux/Windows, if you are on macOS you have to add additional loopback addresses, search on Google how to do that.

Thanks @lpinca
I did the changes and now able to connect ws for different ip addresses.

For anyone using macOS and getting error EADDRNOTAVAIL can follow the link:

https://superuser.com/a/458877/597839

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bartosz-m picture bartosz-m  路  3Comments

dcflow picture dcflow  路  4Comments

pmcalabrese picture pmcalabrese  路  4Comments

anonaka picture anonaka  路  5Comments

makc picture makc  路  4Comments