Search in sources :

Example 1 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.test.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();
    assertEquals("Invalid result", 3, result.size());
    assertEquals("Invalid result", "value 1", result.getFirst("name 1"));
    List<String> values = result.get("name 2");
    assertEquals("Invalid result", 2, values.size());
    assertEquals("Invalid result", "value 2+1", values.get(0));
    assertEquals("Invalid result", "value 2+2", values.get(1));
    assertNull("Invalid result", result.getFirst("name 3"));
}
Also used : MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) Test(org.junit.Test)

Example 2 with MockServerHttpRequest

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

the class ResourceHttpMessageWriterTests method shouldSetRangeNotSatisfiableStatus.

@Test
public void shouldSetRangeNotSatisfiableStatus() throws Exception {
    MockServerHttpRequest request = get("/").header(HttpHeaders.RANGE, "invalid").build();
    testWrite(request);
    assertThat(this.response.getHeaders().getFirst(HttpHeaders.ACCEPT_RANGES), is("bytes"));
    assertThat(this.response.getStatusCode(), is(HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE));
}
Also used : MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) Test(org.junit.Test)

Example 3 with MockServerHttpRequest

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

the class ServerSentEventHttpMessageReaderTests method readServerSentEvents.

@Test
public void readServerSentEvents() {
    MockServerHttpRequest request = MockServerHttpRequest.post("/").body("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\n");
    Flux<ServerSentEvent> events = this.messageReader.read(ResolvableType.forClassWithGenerics(ServerSentEvent.class, String.class), request, Collections.emptyMap()).cast(ServerSentEvent.class);
    StepVerifier.create(events).consumeNextWith(event -> {
        assertEquals("c42", event.id().get());
        assertEquals("foo", event.event().get());
        assertEquals(Duration.ofMillis(123), event.retry().get());
        assertEquals("bla\nbla bla\nbla bla bla", event.comment().get());
        assertEquals("bar", event.data().get());
    }).consumeNextWith(event -> {
        assertEquals("c43", event.id().get());
        assertEquals("bar", event.event().get());
        assertEquals(Duration.ofMillis(456), event.retry().get());
        assertFalse(event.comment().isPresent());
        assertEquals("baz", event.data().get());
    }).expectComplete().verify();
}
Also used : Jackson2JsonDecoder(org.springframework.http.codec.json.Jackson2JsonDecoder) StepVerifier(reactor.test.StepVerifier) AbstractDataBufferAllocatingTestCase(org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase) MediaType(org.springframework.http.MediaType) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Flux(reactor.core.publisher.Flux) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) Assert.assertFalse(org.junit.Assert.assertFalse) Duration(java.time.Duration) ResolvableType(org.springframework.core.ResolvableType) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) Test(org.junit.Test)

Example 4 with MockServerHttpRequest

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

the class CorsUtilsTests method isNotPreFlightRequest.

@Test
public void isNotPreFlightRequest() {
    MockServerHttpRequest request = get("/").build();
    assertFalse(CorsUtils.isPreFlightRequest(request));
    request = options("/").header(HttpHeaders.ORIGIN, "http://domain.com").build();
    assertFalse(CorsUtils.isPreFlightRequest(request));
    request = options("/").header(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET").build();
    assertFalse(CorsUtils.isPreFlightRequest(request));
}
Also used : MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) Test(org.junit.Test)

Example 5 with MockServerHttpRequest

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

the class WebHttpHandlerBuilderTests method orderedWebFilterBeans.

// SPR-15074
@Test
public void orderedWebFilterBeans() throws Exception {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.register(OrderedWebFilterBeanConfig.class);
    context.refresh();
    HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(context).build();
    MockServerHttpRequest request = MockServerHttpRequest.get("/").build();
    MockServerHttpResponse response = new MockServerHttpResponse();
    httpHandler.handle(request, response).blockMillis(5000);
    assertEquals("FilterB::FilterA", response.getBodyAsString().blockMillis(5000));
}
Also used : HttpHandler(org.springframework.http.server.reactive.HttpHandler) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) Test(org.junit.Test)

Aggregations

MockServerHttpRequest (org.springframework.mock.http.server.reactive.test.MockServerHttpRequest)34 Test (org.junit.Test)33 Mono (reactor.core.publisher.Mono)14 MockServerHttpResponse (org.springframework.mock.http.server.reactive.test.MockServerHttpResponse)12 StepVerifier (reactor.test.StepVerifier)11 HttpHandler (org.springframework.http.server.reactive.HttpHandler)9 Flux (reactor.core.publisher.Flux)8 Assert (org.junit.Assert)7 DataBuffer (org.springframework.core.io.buffer.DataBuffer)7 DefaultDataBuffer (org.springframework.core.io.buffer.DefaultDataBuffer)7 DefaultDataBufferFactory (org.springframework.core.io.buffer.DefaultDataBufferFactory)7 Mockito (org.mockito.Mockito)6 HttpHeaders (org.springframework.http.HttpHeaders)6 HttpStatus (org.springframework.http.HttpStatus)6 ReactiveHttpInputMessage (org.springframework.http.ReactiveHttpInputMessage)6 ResponseStatusException (org.springframework.web.server.ResponseStatusException)6 ServerWebExchange (org.springframework.web.server.ServerWebExchange)6 Collections (java.util.Collections)5 MediaType (org.springframework.http.MediaType)5 Jackson2JsonDecoder (org.springframework.http.codec.json.Jackson2JsonDecoder)5