Search in sources :

Example 81 with MockServerHttpRequest

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

the class ServerSentEventHttpMessageReaderTests method readError.

@Test
public void readError() {
    Flux<DataBuffer> body = Flux.just(stringBuffer("data:foo\ndata:bar\n\ndata:baz\n\n")).concatWith(Flux.error(new RuntimeException()));
    MockServerHttpRequest request = MockServerHttpRequest.post("/").body(body);
    Flux<String> data = reader.read(ResolvableType.forClass(String.class), request, Collections.emptyMap()).cast(String.class);
    StepVerifier.create(data).expectNextMatches(elem -> elem.equals("foo\nbar")).expectNextMatches(elem -> elem.equals("baz")).expectError().verify();
}
Also used : Jackson2JsonDecoder(org.springframework.http.codec.json.Jackson2JsonDecoder) StepVerifier(reactor.test.StepVerifier) AbstractLeakCheckingTests(org.springframework.core.testfixture.io.buffer.AbstractLeakCheckingTests) MediaType(org.springframework.http.MediaType) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Pojo(org.springframework.web.testfixture.xml.Pojo) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) DataBufferLimitException(org.springframework.core.io.buffer.DataBufferLimitException) Duration(java.time.Duration) ResolvableType(org.springframework.core.ResolvableType) Collections(java.util.Collections) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 82 with MockServerHttpRequest

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

the class FormHttpMessageReaderTests method readFormError.

@Test
public void readFormError() {
    DataBuffer fooBuffer = stringBuffer("name=value");
    Flux<DataBuffer> body = Flux.just(fooBuffer).concatWith(Flux.error(new RuntimeException()));
    MockServerHttpRequest request = request(body);
    Flux<MultiValueMap<String, String>> result = this.reader.read(null, request, null);
    StepVerifier.create(result).expectError().verify();
}
Also used : MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 83 with MockServerHttpRequest

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

the class FormHttpMessageReaderTests method readFormAsFlux.

@Test
public void readFormAsFlux() {
    String body = "name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3";
    MockServerHttpRequest request = request(body);
    MultiValueMap<String, String> result = this.reader.read(null, request, null).single().block();
    assertThat(result.size()).as("Invalid result").isEqualTo(3);
    assertThat(result.getFirst("name 1")).as("Invalid result").isEqualTo("value 1");
    List<String> values = result.get("name 2");
    assertThat(values.size()).as("Invalid result").isEqualTo(2);
    assertThat(values.get(0)).as("Invalid result").isEqualTo("value 2+1");
    assertThat(values.get(1)).as("Invalid result").isEqualTo("value 2+2");
    assertThat(result.getFirst("name 3")).as("Invalid result").isNull();
}
Also used : MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) Test(org.junit.jupiter.api.Test)

Example 84 with MockServerHttpRequest

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

the class ServerSentEventHttpMessageReaderTests method readServerSentEvents.

@Test
@SuppressWarnings("rawtypes")
public void readServerSentEvents() {
    MockServerHttpRequest request = MockServerHttpRequest.post("/").body(Mono.just(stringBuffer("id:c42\nevent:foo\nretry:123\n:bla\n:bla bla\n:bla bla bla\ndata:bar\n\n" + "id:c43\nevent:bar\nretry:456\ndata:baz\n\ndata:\n\ndata: \n\n")));
    Flux<ServerSentEvent> events = this.reader.read(ResolvableType.forClassWithGenerics(ServerSentEvent.class, String.class), request, Collections.emptyMap()).cast(ServerSentEvent.class);
    StepVerifier.create(events).consumeNextWith(event -> {
        assertThat(event.id()).isEqualTo("c42");
        assertThat(event.event()).isEqualTo("foo");
        assertThat(event.retry()).isEqualTo(Duration.ofMillis(123));
        assertThat(event.comment()).isEqualTo("bla\nbla bla\nbla bla bla");
        assertThat(event.data()).isEqualTo("bar");
    }).consumeNextWith(event -> {
        assertThat(event.id()).isEqualTo("c43");
        assertThat(event.event()).isEqualTo("bar");
        assertThat(event.retry()).isEqualTo(Duration.ofMillis(456));
        assertThat(event.comment()).isNull();
        assertThat(event.data()).isEqualTo("baz");
    }).consumeNextWith(event -> assertThat(event.data()).isNull()).consumeNextWith(event -> assertThat(event.data()).isNull()).expectComplete().verify();
}
Also used : Jackson2JsonDecoder(org.springframework.http.codec.json.Jackson2JsonDecoder) StepVerifier(reactor.test.StepVerifier) AbstractLeakCheckingTests(org.springframework.core.testfixture.io.buffer.AbstractLeakCheckingTests) MediaType(org.springframework.http.MediaType) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Pojo(org.springframework.web.testfixture.xml.Pojo) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) DataBufferLimitException(org.springframework.core.io.buffer.DataBufferLimitException) Duration(java.time.Duration) ResolvableType(org.springframework.core.ResolvableType) Collections(java.util.Collections) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) Test(org.junit.jupiter.api.Test)

Example 85 with MockServerHttpRequest

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

the class RequestHeaderMethodArgumentResolverTests method resolveNameFromSystemPropertyThroughPlaceholder.

@Test
public void resolveNameFromSystemPropertyThroughPlaceholder() {
    String expected = "foo";
    MockServerHttpRequest request = MockServerHttpRequest.get("/").header("bar", expected).build();
    ServerWebExchange exchange = MockServerWebExchange.from(request);
    System.setProperty("systemProperty", "bar");
    try {
        Mono<Object> mono = this.resolver.resolveArgument(this.paramResolvedNameWithPlaceholder, this.bindingContext, exchange);
        Object result = mono.block();
        boolean condition = result instanceof String;
        assertThat(condition).isTrue();
        assertThat(result).isEqualTo(expected);
    } finally {
        System.clearProperty("systemProperty");
    }
}
Also used : MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) 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