use of org.springframework.web.testfixture.http.client.reactive.MockClientHttpResponse in project spring-framework by spring-projects.
the class BodyExtractorsTests method toMonoVoidAsClientShouldConsumeAndCancel.
@Test
public void toMonoVoidAsClientShouldConsumeAndCancel() {
byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
TestPublisher<DataBuffer> body = TestPublisher.create();
BodyExtractor<Mono<Void>, ReactiveHttpInputMessage> extractor = BodyExtractors.toMono(Void.class);
MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK);
response.setBody(body.flux());
StepVerifier.create(extractor.extract(response, this.context)).then(() -> {
body.assertWasSubscribed();
body.emit(dataBuffer);
}).verifyComplete();
body.assertCancelled();
}
use of org.springframework.web.testfixture.http.client.reactive.MockClientHttpResponse in project spring-framework by spring-projects.
the class DefaultClientResponseBuilderTests method mutate.
@Test
public void mutate() {
Flux<DataBuffer> otherBody = Flux.just("foo", "bar").map(s -> s.getBytes(StandardCharsets.UTF_8)).map(DefaultDataBufferFactory.sharedInstance::wrap);
HttpRequest mockClientHttpRequest = new MockClientHttpRequest(HttpMethod.GET, "/path");
MockClientHttpResponse httpResponse = new MockClientHttpResponse(HttpStatus.OK);
httpResponse.getHeaders().add("foo", "bar");
httpResponse.getHeaders().add("bar", "baz");
httpResponse.getCookies().add("baz", ResponseCookie.from("baz", "qux").build());
httpResponse.setBody(otherBody);
DefaultClientResponse otherResponse = new DefaultClientResponse(httpResponse, ExchangeStrategies.withDefaults(), "my-prefix", "", () -> mockClientHttpRequest);
ClientResponse result = otherResponse.mutate().statusCode(HttpStatus.BAD_REQUEST).headers(headers -> headers.set("foo", "baar")).cookies(cookies -> cookies.set("baz", ResponseCookie.from("baz", "quux").build())).build();
assertThat(result.statusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
assertThat(result.headers().asHttpHeaders().size()).isEqualTo(3);
assertThat(result.headers().asHttpHeaders().getFirst("foo")).isEqualTo("baar");
assertThat(result.headers().asHttpHeaders().getFirst("bar")).isEqualTo("baz");
assertThat(result.cookies().size()).isEqualTo(1);
assertThat(result.cookies().getFirst("baz").getValue()).isEqualTo("quux");
assertThat(result.logPrefix()).isEqualTo("my-prefix");
StepVerifier.create(result.bodyToFlux(String.class)).expectNext("foobar").verifyComplete();
}
use of org.springframework.web.testfixture.http.client.reactive.MockClientHttpResponse in project spring-framework by spring-projects.
the class BodyExtractorsTests method unsupportedMediaTypeShouldConsumeAndCancel.
// SPR-17054
@Test
public void unsupportedMediaTypeShouldConsumeAndCancel() {
NettyDataBufferFactory factory = new NettyDataBufferFactory(new PooledByteBufAllocator(true));
NettyDataBuffer buffer = factory.wrap(ByteBuffer.wrap("spring".getBytes(StandardCharsets.UTF_8)));
TestPublisher<DataBuffer> body = TestPublisher.create();
MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK);
response.getHeaders().setContentType(MediaType.APPLICATION_PDF);
response.setBody(body.flux());
BodyExtractor<Mono<User>, ReactiveHttpInputMessage> extractor = BodyExtractors.toMono(User.class);
StepVerifier.create(extractor.extract(response, this.context)).then(() -> {
body.assertWasSubscribed();
body.emit(buffer);
}).expectErrorSatisfies(throwable -> {
boolean condition = throwable instanceof UnsupportedMediaTypeException;
assertThat(condition).isTrue();
assertThatExceptionOfType(IllegalReferenceCountException.class).isThrownBy(buffer::release);
body.assertCancelled();
}).verify();
}
use of org.springframework.web.testfixture.http.client.reactive.MockClientHttpResponse in project spring-framework by spring-projects.
the class BodyExtractorsTests method toMonoVoidAsClientWithEmptyBody.
@Test
public void toMonoVoidAsClientWithEmptyBody() {
TestPublisher<DataBuffer> body = TestPublisher.create();
BodyExtractor<Mono<Void>, ReactiveHttpInputMessage> extractor = BodyExtractors.toMono(Void.class);
MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK);
response.setBody(body.flux());
StepVerifier.create(extractor.extract(response, this.context)).then(() -> {
body.assertWasSubscribed();
body.complete();
}).verifyComplete();
}
use of org.springframework.web.testfixture.http.client.reactive.MockClientHttpResponse in project spring-framework by spring-projects.
the class ResourceHttpMessageReaderTests method readResourceAsMono.
@Test
void readResourceAsMono() throws IOException {
String filename = "test.txt";
String body = "Test resource content";
ContentDisposition contentDisposition = ContentDisposition.attachment().name("file").filename(filename).build();
MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK);
response.getHeaders().setContentType(MediaType.TEXT_PLAIN);
response.getHeaders().setContentDisposition(contentDisposition);
response.setBody(Mono.just(stringBuffer(body)));
Resource resource = reader.readMono(ResolvableType.forClass(ByteArrayResource.class), response, Collections.emptyMap()).block();
assertThat(resource).isNotNull();
assertThat(resource.getFilename()).isEqualTo(filename);
assertThat(resource.getInputStream()).hasContent(body);
}
Aggregations