-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example of Spring WebClient usage (#418)
- Add example of WebClient usage - Add additional test case with WebClient usage Signed-off-by: Dave Syer <dsyer@vmware.com>
- Loading branch information
Showing
4 changed files
with
207 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
examples/spring-reactive/src/test/java/io/cloudevents/examples/spring/WebClientTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package io.cloudevents.examples.spring; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.net.URI; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; | ||
import org.springframework.boot.web.server.LocalServerPort; | ||
import org.springframework.web.reactive.function.client.WebClient; | ||
|
||
import io.cloudevents.CloudEvent; | ||
import io.cloudevents.core.builder.CloudEventBuilder; | ||
import reactor.core.publisher.Mono; | ||
|
||
/** | ||
* Test case to show example usage of WebClient and CloudEvent. The actual | ||
* content of the request and response are asserted separately in | ||
* {@link DemoApplicationTests}. | ||
*/ | ||
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) | ||
public class WebClientTests { | ||
|
||
@Autowired | ||
private WebClient.Builder rest; | ||
|
||
@LocalServerPort | ||
private int port; | ||
|
||
private CloudEvent event; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
event = CloudEventBuilder.v1() // | ||
.withId("12345") // | ||
.withSource(URI.create("https://spring.io/events")) // | ||
.withType("io.spring.event") // | ||
.withData("{\"value\":\"Dave\"}".getBytes()) // | ||
.build(); | ||
} | ||
|
||
@Test | ||
void echoWithCorrectHeaders() { | ||
|
||
Mono<CloudEvent> result = rest.build() // | ||
.post() // | ||
.uri("http://localhost:" + port + "/event") // | ||
.bodyValue(event) // | ||
.exchangeToMono(response -> response.bodyToMono(CloudEvent.class)); | ||
|
||
assertThat(result.block().getData()).isEqualTo(event.getData()); | ||
|
||
} | ||
|
||
} |