Skip to content

Commit

Permalink
add TestSSEClient
Browse files Browse the repository at this point in the history
  • Loading branch information
cmonkey committed Mar 8, 2018
1 parent e8d2297 commit d4e314a
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/test/java/com/github/cmonkey/webflux/TestSSEClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.github.cmonkey.webflux;

import org.junit.Test;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.MediaType;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.reactive.function.BodyExtractors;
import org.springframework.web.reactive.function.client.WebClient;

import java.util.Objects;

public class TestSSEClient {
public TestSSEClient() {
}

@Test
public void testSSE() {

final WebClient client = WebClient.create();

client.get()
.uri("http://localhost:8080/sse/randomNumbers")
.accept(MediaType.TEXT_EVENT_STREAM)
.exchange()
.flatMapMany(response -> response.body(BodyExtractors.toFlux(
new ParameterizedTypeReference<ServerSentEvent<String>>() {
}
)))
.filter(sse -> Objects.nonNull(sse.data()))
.map(ServerSentEvent::data)
.buffer(20)
.doOnNext(System.out::println)
.blockFirst();
}
}

0 comments on commit d4e314a

Please sign in to comment.