6

I am considering usage of "WireMock" utility in order to create simple simulators of HTTP based APIs for my automatic tests. My problem is that some of the tests require server sent events (SSE), which is (as far as I read in their github comments) not currently supported, at least not out of the box. Does anyone know if I can implement simple SSE simulator with the existing capabilities of WireMock library, or maybe know for some alternative library of the same type, that supports SSE?

Paul R
  • 208,748
  • 37
  • 389
  • 560
pinpinokio
  • 505
  • 5
  • 19

2 Answers2

1

As an option, you can construct the event stream manually and mock a response from SSE endpoint like this (I'm using wiremock-jre8-standalone:2.27.1):

...
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.givenThat;
import static com.github.tomakehurst.wiremock.client.WireMock.notFound;
import static com.github.tomakehurst.wiremock.client.WireMock.okForContentType;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
...
String eventStream =
            "id:id1\nevent:event1\ndata:data1\n\n" +
            "id:id2\nevent:event2\ndata:data2\n\n" +
            "id:id3\nevent:event3\ndata:data3\n\n";
givenThat(get(urlPathEqualTo("/sse"))
            .willReturn(okForContentType("text/event-stream", eventStream)));
Ivan Koryshev
  • 426
  • 5
  • 9
1

You could use embedded muServer https://muserver.io/sse. Wiremock or MockWebServer have open issues for that:
https://github.com/square/okhttp/issues/2894
https://github.com/wiremock/wiremock/issues/460

Although Ivan's answer should be sufficient to send some one-time SSEs I believe the web server will close the connection after sending them, which is unexpected in SSE and may result in some unexpected/side-effects error behavior on the client side.

Kamil Roman
  • 973
  • 5
  • 15
  • 30