question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make it easy to write WebSocket tests with MockWebServer

See original GitHub issue

I noticed there are some very useful classes to allow testing web sockets code with MockWebServer in okhttp3.internal.ws.*, such as WebSocketRecorder.

It would be nice if these were public (along with anything else needed) so that OkHttp users can easily test their web sockets code with MockWebServer.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:15
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

9reactions
iocanelcommented, Nov 4, 2016

For our own needs we have created a small wrapper around MockWebServer: https://github.com/fabric8io/mockwebserver. This wrapper actually provides a DSL and it does make it easy to work with websockets:

For example:

DefaultMockServer server = new DefaultMockServer();
server.expect().get().withPath("/api/v1/users/shell")
        .andUpgradeToWebSocket()
        .open()
             .waitFor(1000).andEmit("#")
            .expect("create root").andEmit("CREATED").once()
            .expect("delete root").andEmit("DELETED").once()
        .done()
        .once();

server.start();
4reactions
Kshitij09-agcommented, May 22, 2021

Anyone has working example for WebSocket testing with mockwebserver?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Okhttp.MockebServer for testing websocket
I am using okhttp.MockWebServer to test rest api calls. Is there a way to use it for mocking a websocket call as well?...
Read more >
How to work with WebSockets and unit-test them by OkHttp ...
How to work with WebSockets and unit-test them by OkHttp and Mockito ... I'll try to write an article on how to do...
Read more >
JavaScript testing #14. Mocking WebSockets using the mock ...
We create a simple application that uses WebSockets. We also write tests that ensure that our React code works correctly.
Read more >
Testing REST APIs Using MockWebServer
Learn how to mock a REST API with MockWebServer and easily test your ... How to write UI tests to make sure your...
Read more >
okhttp-tests/src/test/java/com/squareup/okhttp/internal/ws ...
Unless required by applicable law or agreed to in writing, software ... Test;. import static com.squareup.okhttp.internal.ws.WebSocket.PayloadType.TEXT;.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found