Search in sources :

Example 76 with MockServerHttpRequest

use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest in project spring-framework by spring-projects.

the class MultipartHttpMessageWriterTests method parse.

static MultiValueMap<String, Part> parse(MockServerHttpResponse response, Map<String, Object> hints) {
    MediaType contentType = response.getHeaders().getContentType();
    assertThat(contentType.getParameter("boundary")).as("No boundary found").isNotNull();
    // see if we can read what we wrote
    DefaultPartHttpMessageReader partReader = new DefaultPartHttpMessageReader();
    MultipartHttpMessageReader reader = new MultipartHttpMessageReader(partReader);
    MockServerHttpRequest request = MockServerHttpRequest.post("/").contentType(MediaType.parseMediaType(contentType.toString())).body(response.getBody());
    ResolvableType elementType = ResolvableType.forClassWithGenerics(MultiValueMap.class, String.class, Part.class);
    MultiValueMap<String, Part> result = reader.readMono(elementType, request, hints).block(Duration.ofSeconds(5));
    assertThat(result).isNotNull();
    return result;
}
Also used : MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) MediaType(org.springframework.http.MediaType) ResolvableType(org.springframework.core.ResolvableType)

Example 77 with MockServerHttpRequest

use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest in project spring-framework by spring-projects.

the class ServerSentEventHttpMessageReaderTests method maxInMemoryLimitAllowsReadingPojoLargerThanDefaultSize.

// gh-24312
@Test
public void maxInMemoryLimitAllowsReadingPojoLargerThanDefaultSize() {
    int limit = this.jsonDecoder.getMaxInMemorySize();
    String fooValue = getStringOfSize(limit) + "and then some more";
    String content = "data:{\"foo\": \"" + fooValue + "\"}\n\n";
    MockServerHttpRequest request = MockServerHttpRequest.post("/").body(Mono.just(stringBuffer(content)));
    Jackson2JsonDecoder jacksonDecoder = new Jackson2JsonDecoder();
    ServerSentEventHttpMessageReader messageReader = new ServerSentEventHttpMessageReader(jacksonDecoder);
    jacksonDecoder.setMaxInMemorySize(limit + 1024);
    messageReader.setMaxInMemorySize(limit + 1024);
    Flux<Pojo> data = messageReader.read(ResolvableType.forClass(Pojo.class), request, Collections.emptyMap()).cast(Pojo.class);
    StepVerifier.create(data).consumeNextWith(pojo -> assertThat(pojo.getFoo()).isEqualTo(fooValue)).expectComplete().verify();
}
Also used : Pojo(org.springframework.web.testfixture.xml.Pojo) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) Jackson2JsonDecoder(org.springframework.http.codec.json.Jackson2JsonDecoder) Test(org.junit.jupiter.api.Test)

Example 78 with MockServerHttpRequest

use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest in project spring-framework by spring-projects.

the class ServerSentEventHttpMessageReaderTests method readPojoWithCommentOnly.

// gh-24389
@Test
void readPojoWithCommentOnly() {
    MockServerHttpRequest request = MockServerHttpRequest.post("/").body(Flux.just(stringBuffer(":ping\n"), stringBuffer("\n")));
    Flux<Object> data = this.reader.read(ResolvableType.forType(String.class), request, Collections.emptyMap());
    StepVerifier.create(data).expectComplete().verify();
}
Also used : MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) Test(org.junit.jupiter.api.Test)

Example 79 with MockServerHttpRequest

use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest in project spring-framework by spring-projects.

the class ServerSentEventHttpMessageReaderTests method maxInMemoryLimit.

@Test
public void maxInMemoryLimit() {
    this.reader.setMaxInMemorySize(17);
    MockServerHttpRequest request = MockServerHttpRequest.post("/").body(Flux.just(stringBuffer("data:\"TOO MUCH DATA\"\ndata:bar\n\ndata:baz\n\n")));
    Flux<String> data = this.reader.read(ResolvableType.forClass(String.class), request, Collections.emptyMap()).cast(String.class);
    StepVerifier.create(data).expectError(DataBufferLimitException.class).verify();
}
Also used : DataBufferLimitException(org.springframework.core.io.buffer.DataBufferLimitException) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) Test(org.junit.jupiter.api.Test)

Example 80 with MockServerHttpRequest

use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest in project spring-framework by spring-projects.

the class ServerSentEventHttpMessageReaderTests method trimWhitespace.

@Test
public void trimWhitespace() {
    MockServerHttpRequest request = MockServerHttpRequest.post("/").body(Mono.just(stringBuffer("data: \tfoo \ndata:bar\t\n\n")));
    Flux<String> data = reader.read(ResolvableType.forClass(String.class), request, Collections.emptyMap()).cast(String.class);
    StepVerifier.create(data).expectNext("\tfoo \nbar\t").expectComplete().verify();
}
Also used : MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) Test(org.junit.jupiter.api.Test)

Aggregations

MockServerHttpRequest (org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest)182 Test (org.junit.jupiter.api.Test)160 MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)101 Mono (reactor.core.publisher.Mono)52 DataBuffer (org.springframework.core.io.buffer.DataBuffer)51 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)50 Collections (java.util.Collections)48 StepVerifier (reactor.test.StepVerifier)46 HttpHeaders (org.springframework.http.HttpHeaders)39 Flux (reactor.core.publisher.Flux)37 MediaType (org.springframework.http.MediaType)34 ServerWebExchange (org.springframework.web.server.ServerWebExchange)34 MultiValueMap (org.springframework.util.MultiValueMap)33 HttpStatus (org.springframework.http.HttpStatus)31 StandardCharsets (java.nio.charset.StandardCharsets)29 MockServerHttpResponse (org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse)29 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)27 ClassPathResource (org.springframework.core.io.ClassPathResource)25 DefaultDataBuffer (org.springframework.core.io.buffer.DefaultDataBuffer)24 Optional (java.util.Optional)23