use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.
the class DefaultClientResponseTests method toEntityListTypeReference.
@Test
public void toEntityListTypeReference() {
byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
Flux<DataBuffer> body = Flux.just(dataBuffer);
mockTextPlainResponse(body);
List<HttpMessageReader<?>> messageReaders = Collections.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes()));
given(mockExchangeStrategies.messageReaders()).willReturn(messageReaders);
ResponseEntity<List<String>> result = defaultClientResponse.toEntityList(new ParameterizedTypeReference<String>() {
}).block();
assertThat(result.getBody()).isEqualTo(Collections.singletonList("foo"));
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(result.getStatusCodeValue()).isEqualTo(HttpStatus.OK.value());
assertThat(result.getHeaders().getContentType()).isEqualTo(MediaType.TEXT_PLAIN);
}
use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.
the class DefaultClientResponseTests method toEntityListWithUnknownStatusCode.
@Test
public void toEntityListWithUnknownStatusCode() {
byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
Flux<DataBuffer> body = Flux.just(dataBuffer);
httpHeaders.setContentType(MediaType.TEXT_PLAIN);
given(mockResponse.getHeaders()).willReturn(httpHeaders);
given(mockResponse.getStatusCode()).willThrow(new IllegalArgumentException("999"));
given(mockResponse.getRawStatusCode()).willReturn(999);
given(mockResponse.getBody()).willReturn(body);
List<HttpMessageReader<?>> messageReaders = Collections.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes()));
given(mockExchangeStrategies.messageReaders()).willReturn(messageReaders);
ResponseEntity<List<String>> result = defaultClientResponse.toEntityList(String.class).block();
assertThat(result.getBody()).isEqualTo(Collections.singletonList("foo"));
assertThatIllegalArgumentException().isThrownBy(result::getStatusCode);
assertThat(result.getStatusCodeValue()).isEqualTo(999);
assertThat(result.getHeaders().getContentType()).isEqualTo(MediaType.TEXT_PLAIN);
}
use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.
the class BodyExtractorsTests method toParts.
@Test
public void toParts() {
BodyExtractor<Flux<Part>, ServerHttpRequest> extractor = BodyExtractors.toParts();
String bodyContents = "-----------------------------9051914041544843365972754266\r\n" + "Content-Disposition: form-data; name=\"text\"\r\n" + "\r\n" + "text default\r\n" + "-----------------------------9051914041544843365972754266\r\n" + "Content-Disposition: form-data; name=\"file1\"; filename=\"a.txt\"\r\n" + "Content-Type: text/plain\r\n" + "\r\n" + "Content of a.txt.\r\n" + "\r\n" + "-----------------------------9051914041544843365972754266\r\n" + "Content-Disposition: form-data; name=\"file2\"; filename=\"a.html\"\r\n" + "Content-Type: text/html\r\n" + "\r\n" + "<!DOCTYPE html><title>Content of a.html.</title>\r\n" + "\r\n" + "-----------------------------9051914041544843365972754266--\r\n";
byte[] bytes = bodyContents.getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
Flux<DataBuffer> body = Flux.just(dataBuffer);
MockServerHttpRequest request = MockServerHttpRequest.post("/").header("Content-Type", "multipart/form-data; boundary=---------------------------9051914041544843365972754266").body(body);
Flux<Part> result = extractor.extract(request, this.context);
StepVerifier.create(result).consumeNextWith(part -> {
assertThat(part.name()).isEqualTo("text");
boolean condition = part instanceof FormFieldPart;
assertThat(condition).isTrue();
FormFieldPart formFieldPart = (FormFieldPart) part;
assertThat(formFieldPart.value()).isEqualTo("text default");
}).consumeNextWith(part -> {
assertThat(part.name()).isEqualTo("file1");
boolean condition = part instanceof FilePart;
assertThat(condition).isTrue();
FilePart filePart = (FilePart) part;
assertThat(filePart.filename()).isEqualTo("a.txt");
assertThat(filePart.headers().getContentType()).isEqualTo(MediaType.TEXT_PLAIN);
}).consumeNextWith(part -> {
assertThat(part.name()).isEqualTo("file2");
boolean condition = part instanceof FilePart;
assertThat(condition).isTrue();
FilePart filePart = (FilePart) part;
assertThat(filePart.filename()).isEqualTo("a.html");
assertThat(filePart.headers().getContentType()).isEqualTo(MediaType.TEXT_HTML);
}).expectComplete().verify();
}
use of org.springframework.core.io.buffer.DataBuffer 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.core.io.buffer.DataBuffer in project spring-framework by spring-projects.
the class BodyExtractorsTests method toDataBuffers.
@Test
public void toDataBuffers() {
BodyExtractor<Flux<DataBuffer>, ReactiveHttpInputMessage> extractor = BodyExtractors.toDataBuffers();
byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
Flux<DataBuffer> body = Flux.just(dataBuffer);
MockServerHttpRequest request = MockServerHttpRequest.post("/").body(body);
Flux<DataBuffer> result = extractor.extract(request, this.context);
StepVerifier.create(result).expectNext(dataBuffer).expectComplete().verify();
}
Aggregations