https://linen.dev logo
#wiremock-java
Title
# wiremock-java
h

Hantsy Bai

09/07/2023, 12:06 PM
Hi, anyone here using Wiremock to mock SSE endpoint in Quarkus? I have tried to use WireMock to mock a SSE like this to test the
RestClient
, it did not work. The complete example project: https://github.com/hantsy/quarkus-sandbox/blob/master/restclient-reactive The rest client interface is like this .
Copy code
@GET
    @Path("events")
    @Produces(MediaType.SERVER_SENT_EVENTS)
    @RestStreamElementType(MediaType.APPLICATION_JSON)
    Multi<PostCreated> events();
And the testing codes:
Copy code
@Test
    void getEvents() throws Throwable {
        var url = "/posts/events";
        String dataStream = """
                id:1
                event:random
                data:{"id":"test post 1","createdAt":"2023-03-26T10:15:30"}
                                                     
                id:2
                event:random
                data:{"id":"test post 2","createdAt":"2023-03-26T10:15:31"}
                
                """.stripIndent().stripLeading();

//        String dataStream = "id:1\n" +
//                "event:random\n" +
//                "data:{\"id\":\"test post 1\", \"createdAt\":\"2023-03-26T10:15:30\"}\n\n" +
//                "id:2\n" +
//                "event:random\n" +
//                "data:{\"id\":\"test post 2\", \"createdAt\":\"2023-03-26T10:15:31\"}\n\n";
        stubFor(
                get(url)
                        .withHeader("Accept", equalTo("text/event-stream"))
                        .willReturn(okForContentType("text/event-stream", dataStream))
        );

        var events = VertxContextSupport.subscribeAndAwait(() ->
                client.events()
                        .onItem().invoke(c -> LOGGER.log(<http://Level.INFO|Level.INFO>, "item is: " + c))
                        .onFailure().invoke(error -> LOGGER.log(<http://Level.INFO|Level.INFO>, "error: " + error.getMessage()))
                        .collect().asList()
        );

        assertThat(events.get(0).id()).isEqualTo("test post 1");

        verify(
                getRequestedFor(urlEqualTo(url))
                        .withHeader("Accept", equalTo("text/event-stream"))
        );
    }
3 Views