Search in sources :

Example 41 with MockServerHttpRequest

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

the class RequestPredicateTests method negate.

@Test
public void negate() {
    RequestPredicate predicate = request -> false;
    RequestPredicate negated = predicate.negate();
    MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://example.com").build();
    ServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
    assertThat(negated.test(request)).isTrue();
    predicate = r -> true;
    negated = predicate.negate();
    assertThat(negated.test(request)).isFalse();
}
Also used : Test(org.junit.jupiter.api.Test) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Collections(java.util.Collections) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) Test(org.junit.jupiter.api.Test)

Example 42 with MockServerHttpRequest

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

the class RequestPredicateTests method or.

@Test
public void or() {
    RequestPredicate predicate1 = request -> true;
    RequestPredicate predicate2 = request -> false;
    RequestPredicate predicate3 = request -> false;
    MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://example.com").build();
    ServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
    assertThat(predicate1.or(predicate2).test(request)).isTrue();
    assertThat(predicate2.or(predicate1).test(request)).isTrue();
    assertThat(predicate2.or(predicate3).test(request)).isFalse();
}
Also used : Test(org.junit.jupiter.api.Test) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Collections(java.util.Collections) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) Test(org.junit.jupiter.api.Test)

Example 43 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 44 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 45 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)

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