Java Examples for org.springframework.web.reactive.function.client.ClientResponse
The following java examples will help you to understand the usage of org.springframework.web.reactive.function.client.ClientResponse. These source code samples are taken from different open source projects.
Example 1
| Project: reactive-ms-example-master File: RestServiceHelper.java View source code |
public static WebClient mockWebClient(final WebClient originalClient, final Mono<?> mono) {
WebClient client = spy(originalClient);
WebClient.UriSpec uriSpec = mock(WebClient.UriSpec.class);
doReturn(uriSpec).when(client).get();
WebClient.RequestHeadersSpec<?> headerSpec = mock(WebClient.RequestHeadersSpec.class);
doReturn(headerSpec).when(uriSpec).uri(anyString());
doReturn(headerSpec).when(headerSpec).accept(any());
ClientResponse clientResponse = mock(ClientResponse.class);
doReturn(mono).when(clientResponse).bodyToMono(Mockito.any());
doReturn(Mono.just(clientResponse)).when(headerSpec).exchange();
return client;
}Example 2
| Project: spring-security-master File: WebClientOAuth2PocTests.java View source code |
@Test
public void httpBasicWhenNeeded() throws Exception {
this.server.enqueue(new MockResponse().setResponseCode(401).setHeader("WWW-Authenticate", "Basic realm=\"Test\""));
this.server.enqueue(new MockResponse().setResponseCode(200).setBody("OK"));
ClientResponse response = this.webClient.filter(basicIfNeeded("rob", "rob")).get().uri("/").exchange().block();
assertThat(response.statusCode()).isEqualTo(HttpStatus.OK);
assertThat(this.server.takeRequest().getHeader("Authorization")).isNull();
assertThat(this.server.takeRequest().getHeader("Authorization")).isEqualTo("Basic cm9iOnJvYg==");
}Example 3
| Project: spring-boot-master File: AbstractReactiveWebServerFactoryTests.java View source code |
@Test
public void startStopServer() {
this.webServer = getFactory().getWebServer(new EchoHandler());
this.webServer.start();
assertThat(this.output.toString()).contains("started on port");
Mono<String> result = getWebClient().post().uri("/test").contentType(MediaType.TEXT_PLAIN).body(BodyInserters.fromObject("Hello World")).exchange().flatMap( response -> response.bodyToMono(String.class));
assertThat(result.block()).isEqualTo("Hello World");
this.webServer.stop();
Mono<ClientResponse> response = getWebClient().post().uri("/test").contentType(MediaType.TEXT_PLAIN).body(BodyInserters.fromObject("Hello World")).exchange();
StepVerifier.create(response).expectError().verify();
}Example 4
| Project: spring-integration-master File: ReactiveHttpRequestExecutingMessageHandler.java View source code |
@Override
protected Object exchange(Supplier<URI> uriSupplier, HttpMethod httpMethod, HttpEntity<?> httpRequest, Object expectedResponseType, Message<?> requestMessage) {
WebClient.RequestBodySpec requestSpec = this.webClient.method(httpMethod).uri( b -> uriSupplier.get()).headers(httpRequest.getHeaders());
if (httpRequest.hasBody()) {
requestSpec.body(BodyInserters.fromObject(httpRequest.getBody()));
}
Mono<ClientResponse> responseMono = requestSpec.exchange().doOnNext( response -> {
HttpStatus httpStatus = response.statusCode();
if (httpStatus.is4xxClientError() || httpStatus.is5xxServerError()) {
throw new WebClientException("ClientResponse has erroneous status code: " + httpStatus.value() + " " + httpStatus.getReasonPhrase());
}
});
if (isExpectReply()) {
ResolvableType responseType;
if (expectedResponseType instanceof ParameterizedTypeReference<?>) {
responseType = ResolvableType.forType(((ParameterizedTypeReference<?>) expectedResponseType).getType());
} else if (expectedResponseType != null) {
responseType = ResolvableType.forClass((Class<?>) expectedResponseType);
} else {
responseType = null;
}
return responseMono.map( response -> new ResponseEntity<>(responseType != null ? response.body(BodyExtractors.toMono(responseType)).block() : null, response.headers().asHttpHeaders(), response.statusCode())).map(this::getReply);
} else {
responseMono.subscribe( v -> {
}, ex -> sendErrorMessage(requestMessage, ex));
return null;
}
}Example 5
| Project: spring-framework-master File: MultipartIntegrationTests.java View source code |
@Test
public void requestPart() {
Mono<ClientResponse> result = webClient.post().uri("/requestPart").contentType(MediaType.MULTIPART_FORM_DATA).body(BodyInserters.fromMultipartData(generateBody())).exchange();
StepVerifier.create(result).consumeNextWith( response -> assertEquals(HttpStatus.OK, response.statusCode())).verifyComplete();
}