Search in sources :

Example 81 with DataBuffer

use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.

the class DefaultClientResponseBuilderTests method normal.

@Test
public void normal() {
    Flux<DataBuffer> body = Flux.just("baz").map(s -> s.getBytes(StandardCharsets.UTF_8)).map(DefaultDataBufferFactory.sharedInstance::wrap);
    ClientResponse response = ClientResponse.create(HttpStatus.BAD_GATEWAY, ExchangeStrategies.withDefaults()).header("foo", "bar").cookie("baz", "qux").body(body).build();
    assertThat(response.statusCode()).isEqualTo(HttpStatus.BAD_GATEWAY);
    HttpHeaders responseHeaders = response.headers().asHttpHeaders();
    assertThat(responseHeaders.getFirst("foo")).isEqualTo("bar");
    assertThat(response.cookies().getFirst("baz")).as("qux").isNotNull();
    assertThat(response.cookies().getFirst("baz").getValue()).isEqualTo("qux");
    StepVerifier.create(response.bodyToFlux(String.class)).expectNext("baz").verifyComplete();
}
Also used : StepVerifier(reactor.test.StepVerifier) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) MockClientHttpResponse(org.springframework.web.testfixture.http.client.reactive.MockClientHttpResponse) HttpHeaders(org.springframework.http.HttpHeaders) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HttpMethod(org.springframework.http.HttpMethod) HttpRequest(org.springframework.http.HttpRequest) MockClientHttpRequest(org.springframework.web.testfixture.http.client.reactive.MockClientHttpRequest) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) ResponseCookie(org.springframework.http.ResponseCookie) HttpHeaders(org.springframework.http.HttpHeaders) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 82 with DataBuffer

use of org.springframework.core.io.buffer.DataBuffer 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();
}
Also used : StepVerifier(reactor.test.StepVerifier) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) MockClientHttpResponse(org.springframework.web.testfixture.http.client.reactive.MockClientHttpResponse) HttpHeaders(org.springframework.http.HttpHeaders) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HttpMethod(org.springframework.http.HttpMethod) HttpRequest(org.springframework.http.HttpRequest) MockClientHttpRequest(org.springframework.web.testfixture.http.client.reactive.MockClientHttpRequest) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) ResponseCookie(org.springframework.http.ResponseCookie) HttpRequest(org.springframework.http.HttpRequest) MockClientHttpRequest(org.springframework.web.testfixture.http.client.reactive.MockClientHttpRequest) MockClientHttpRequest(org.springframework.web.testfixture.http.client.reactive.MockClientHttpRequest) DataBuffer(org.springframework.core.io.buffer.DataBuffer) MockClientHttpResponse(org.springframework.web.testfixture.http.client.reactive.MockClientHttpResponse) Test(org.junit.jupiter.api.Test)

Example 83 with DataBuffer

use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.

the class DefaultPartHttpMessageReaderTests method createRequest.

private MockServerHttpRequest createRequest(Resource resource, String boundary) {
    Flux<DataBuffer> body = DataBufferUtils.readByteChannel(resource::readableChannel, bufferFactory, BUFFER_SIZE);
    MediaType contentType = new MediaType("multipart", "form-data", singletonMap("boundary", boundary));
    return MockServerHttpRequest.post("/").contentType(contentType).body(body);
}
Also used : MediaType(org.springframework.http.MediaType) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 84 with DataBuffer

use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.

the class MultipartHttpMessageWriterTests method customContentDisposition.

// SPR-16376
@Test
public void customContentDisposition() throws IOException {
    Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg");
    Flux<DataBuffer> buffers = DataBufferUtils.read(logo, DefaultDataBufferFactory.sharedInstance, 1024);
    long contentLength = logo.contentLength();
    MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
    bodyBuilder.part("resource", logo).headers(h -> h.setContentDispositionFormData("resource", "spring.jpg"));
    bodyBuilder.asyncPart("buffers", buffers, DataBuffer.class).headers(h -> {
        h.setContentDispositionFormData("buffers", "buffers.jpg");
        h.setContentType(MediaType.IMAGE_JPEG);
        h.setContentLength(contentLength);
    });
    MultiValueMap<String, HttpEntity<?>> multipartData = bodyBuilder.build();
    Map<String, Object> hints = Collections.emptyMap();
    this.writer.write(Mono.just(multipartData), null, MediaType.MULTIPART_FORM_DATA, this.response, hints).block();
    MultiValueMap<String, Part> requestParts = parse(this.response, hints);
    assertThat(requestParts.size()).isEqualTo(2);
    Part part = requestParts.getFirst("resource");
    assertThat(part instanceof FilePart).isTrue();
    assertThat(((FilePart) part).filename()).isEqualTo("spring.jpg");
    assertThat(part.headers().getContentLength()).isEqualTo(logo.getFile().length());
    part = requestParts.getFirst("buffers");
    assertThat(part instanceof FilePart).isTrue();
    assertThat(((FilePart) part).filename()).isEqualTo("buffers.jpg");
    assertThat(part.headers().getContentLength()).isEqualTo(logo.getFile().length());
}
Also used : HttpEntity(org.springframework.http.HttpEntity) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) MultipartBodyBuilder(org.springframework.http.client.MultipartBodyBuilder) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 85 with DataBuffer

use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.

the class MultipartHttpMessageWriterTests method writeMultipartFormData.

@Test
public void writeMultipartFormData() throws Exception {
    Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg");
    Resource utf8 = new ClassPathResource("/org/springframework/http/converter/logo.jpg") {

        @Override
        public String getFilename() {
            // SPR-12108
            return "Hall\u00F6le.jpg";
        }
    };
    Flux<DataBuffer> bufferPublisher = Flux.just(this.bufferFactory.wrap("Aa".getBytes(StandardCharsets.UTF_8)), this.bufferFactory.wrap("Bb".getBytes(StandardCharsets.UTF_8)), this.bufferFactory.wrap("Cc".getBytes(StandardCharsets.UTF_8)));
    FilePart mockPart = mock(FilePart.class);
    HttpHeaders partHeaders = new HttpHeaders();
    partHeaders.setContentType(MediaType.TEXT_PLAIN);
    partHeaders.setContentDispositionFormData("foo", "file.txt");
    partHeaders.add("foo", "bar");
    given(mockPart.headers()).willReturn(partHeaders);
    given(mockPart.content()).willReturn(bufferPublisher);
    MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
    bodyBuilder.part("name 1", "value 1");
    bodyBuilder.part("name 2", "value 2+1");
    bodyBuilder.part("name 2", "value 2+2");
    bodyBuilder.part("logo", logo);
    bodyBuilder.part("utf8", utf8);
    bodyBuilder.part("json", new Foo("bar"), MediaType.APPLICATION_JSON);
    bodyBuilder.asyncPart("publisher", Flux.just("foo", "bar", "baz"), String.class);
    bodyBuilder.part("filePublisher", mockPart);
    Mono<MultiValueMap<String, HttpEntity<?>>> result = Mono.just(bodyBuilder.build());
    Map<String, Object> hints = Collections.emptyMap();
    this.writer.write(result, null, MediaType.MULTIPART_FORM_DATA, this.response, hints).block(Duration.ofSeconds(5));
    MultiValueMap<String, Part> requestParts = parse(this.response, hints);
    assertThat(requestParts.size()).isEqualTo(7);
    Part part = requestParts.getFirst("name 1");
    assertThat(part instanceof FormFieldPart).isTrue();
    assertThat(part.name()).isEqualTo("name 1");
    assertThat(((FormFieldPart) part).value()).isEqualTo("value 1");
    List<Part> parts2 = requestParts.get("name 2");
    assertThat(parts2.size()).isEqualTo(2);
    part = parts2.get(0);
    assertThat(part instanceof FormFieldPart).isTrue();
    assertThat(part.name()).isEqualTo("name 2");
    assertThat(((FormFieldPart) part).value()).isEqualTo("value 2+1");
    part = parts2.get(1);
    assertThat(part instanceof FormFieldPart).isTrue();
    assertThat(part.name()).isEqualTo("name 2");
    assertThat(((FormFieldPart) part).value()).isEqualTo("value 2+2");
    part = requestParts.getFirst("logo");
    assertThat(part instanceof FilePart).isTrue();
    assertThat(part.name()).isEqualTo("logo");
    assertThat(((FilePart) part).filename()).isEqualTo("logo.jpg");
    assertThat(part.headers().getContentType()).isEqualTo(MediaType.IMAGE_JPEG);
    assertThat(part.headers().getContentLength()).isEqualTo(logo.getFile().length());
    part = requestParts.getFirst("utf8");
    assertThat(part instanceof FilePart).isTrue();
    assertThat(part.name()).isEqualTo("utf8");
    assertThat(((FilePart) part).filename()).isEqualTo("Hall\u00F6le.jpg");
    assertThat(part.headers().getContentType()).isEqualTo(MediaType.IMAGE_JPEG);
    assertThat(part.headers().getContentLength()).isEqualTo(utf8.getFile().length());
    part = requestParts.getFirst("json");
    assertThat(part).isNotNull();
    assertThat(part.name()).isEqualTo("json");
    assertThat(part.headers().getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
    String value = decodeToString(part);
    assertThat(value).isEqualTo("{\"bar\":\"bar\"}");
    part = requestParts.getFirst("publisher");
    assertThat(part).isNotNull();
    assertThat(part.name()).isEqualTo("publisher");
    value = decodeToString(part);
    assertThat(value).isEqualTo("foobarbaz");
    part = requestParts.getFirst("filePublisher");
    assertThat(part).isNotNull();
    assertThat(part.name()).isEqualTo("filePublisher");
    assertThat(part.headers()).containsEntry("foo", Collections.singletonList("bar"));
    assertThat(((FilePart) part).filename()).isEqualTo("file.txt");
    value = decodeToString(part);
    assertThat(value).isEqualTo("AaBbCc");
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) MultipartBodyBuilder(org.springframework.http.client.MultipartBodyBuilder) MultiValueMap(org.springframework.util.MultiValueMap) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Aggregations

DataBuffer (org.springframework.core.io.buffer.DataBuffer)230 Test (org.junit.jupiter.api.Test)111 Mono (reactor.core.publisher.Mono)55 Flux (reactor.core.publisher.Flux)52 DefaultDataBuffer (org.springframework.core.io.buffer.DefaultDataBuffer)49 ResolvableType (org.springframework.core.ResolvableType)41 DefaultDataBufferFactory (org.springframework.core.io.buffer.DefaultDataBufferFactory)36 StepVerifier (reactor.test.StepVerifier)36 List (java.util.List)34 Test (org.junit.Test)31 DataBufferUtils (org.springframework.core.io.buffer.DataBufferUtils)29 IOException (java.io.IOException)28 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)27 MockServerHttpRequest (org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest)27 Map (java.util.Map)25 NettyDataBuffer (org.springframework.core.io.buffer.NettyDataBuffer)25 DataBufferFactory (org.springframework.core.io.buffer.DataBufferFactory)24 HttpHeaders (org.springframework.http.HttpHeaders)24 MediaType (org.springframework.http.MediaType)24 ByteBuffer (java.nio.ByteBuffer)23