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

add WS support? #2240

Open
stevenpollack opened this issue Nov 1, 2021 · 6 comments
Open

add WS support? #2240

stevenpollack opened this issue Nov 1, 2021 · 6 comments
Labels

Comments

@stevenpollack
Copy link

Context

I love using nock for my HTTP server mocking, and I'd love to use it for a WS project I'm working on. I was hoping it could intercept messages (at least from) the websocket server.

Has the feature been requested before?

This was discussed in #129.

If the feature request is accepted, would you be willing to submit a PR?

Sadly, I don't think I'm skilled enough to implement this on my own, but I'd be willing to help if someone could give me guidance.

@gr2m
Copy link
Member

gr2m commented Nov 8, 2021

I hope to finally have the time to tackle the decomposition of nock into smaller module as mentioned in #129 (comment). Once we have that we might look into an alternative intercept module that will work with websockets.

If anyone knows of prior work for websocket mocking, please share!

@gr2m gr2m added the feature label Nov 8, 2021
@stevenpollack
Copy link
Author

I'm happy to help however I can!

@izzqz
Copy link

izzqz commented Nov 25, 2021

+1 am Interested in feature

@gr2m
Copy link
Member

gr2m commented Nov 25, 2021

A good place to start is to discuss a "dream API". Gather your use cases and make up the best possible APIs to make them possible. Then we have something we can work towards.

@izzqz
Copy link

izzqz commented Dec 6, 2021

I suggest simply adding a pipeline function for the various sub-protocols in websockets or HTTP. Here's an example for a node:

import nock from 'nock';
import WebSocket, { createWebSocketStream } from 'ws';

const ws = new WebSocket('wss://echo.websocket.org/', {
    origin: 'https://websocket.org',
});

const duplexWebSocket = createWebSocketStream(ws, { encoding: 'utf8' });

// nock will intercept request, check header and pipe to websocket server
nock('https://websocket.org')
    .matchHeader('X-MBX-APIKEY', 'foobar')
    .pipe(duplexWebSocket);

duplexWebSocket.pipe(process.stdout);

With this nock API, we can create any mocking implementation.

@gr2m
Copy link
Member

gr2m commented Dec 6, 2021

This looks great! How would like to define mocks beyond just headers?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants