Search in sources :

Example 31 with ServerWebExchange

use of org.springframework.web.server.ServerWebExchange in project spring-framework by spring-projects.

the class ProducesRequestConditionTests method match.

@Test
public void match() throws Exception {
    ServerWebExchange exchange = MockServerHttpRequest.get("/").header("Accept", "text/plain").toExchange();
    ProducesRequestCondition condition = new ProducesRequestCondition("text/plain");
    assertNotNull(condition.getMatchingCondition(exchange));
}
Also used : MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Test(org.junit.Test)

Example 32 with ServerWebExchange

use of org.springframework.web.server.ServerWebExchange in project spring-framework by spring-projects.

the class ProducesRequestConditionTests method compareTo.

@Test
public void compareTo() throws Exception {
    ProducesRequestCondition html = new ProducesRequestCondition("text/html");
    ProducesRequestCondition xml = new ProducesRequestCondition("application/xml");
    ProducesRequestCondition none = new ProducesRequestCondition();
    ServerWebExchange exchange = MockServerHttpRequest.get("/").header("Accept", "application/xml, text/html").toExchange();
    assertTrue(html.compareTo(xml, exchange) > 0);
    assertTrue(xml.compareTo(html, exchange) < 0);
    assertTrue(xml.compareTo(none, exchange) < 0);
    assertTrue(none.compareTo(xml, exchange) > 0);
    assertTrue(html.compareTo(none, exchange) < 0);
    assertTrue(none.compareTo(html, exchange) > 0);
    exchange = MockServerHttpRequest.get("/").header("Accept", "application/xml, text/*").toExchange();
    assertTrue(html.compareTo(xml, exchange) > 0);
    assertTrue(xml.compareTo(html, exchange) < 0);
    exchange = MockServerHttpRequest.get("/").header("Accept", "application/pdf").toExchange();
    assertTrue(html.compareTo(xml, exchange) == 0);
    assertTrue(xml.compareTo(html, exchange) == 0);
    // See SPR-7000
    exchange = MockServerHttpRequest.get("/").header("Accept", "text/html;q=0.9,application/xml").toExchange();
    assertTrue(html.compareTo(xml, exchange) > 0);
    assertTrue(xml.compareTo(html, exchange) < 0);
}
Also used : MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Test(org.junit.Test)

Example 33 with ServerWebExchange

use of org.springframework.web.server.ServerWebExchange in project spring-framework by spring-projects.

the class RequestMappingInfoTests method matchCustomCondition.

@Test
public void matchCustomCondition() {
    ServerWebExchange exchange = MockServerHttpRequest.get("/foo?foo=bar").toExchange();
    RequestMappingInfo info = paths("/foo").params("foo=bar").build();
    RequestMappingInfo match = info.getMatchingCondition(exchange);
    assertNotNull(match);
    info = paths("/foo").params("foo!=bar").customCondition(new ParamsRequestCondition("foo!=bar")).build();
    match = info.getMatchingCondition(exchange);
    assertNull(match);
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) RequestMappingInfo(org.springframework.web.reactive.result.method.RequestMappingInfo) Test(org.junit.Test)

Example 34 with ServerWebExchange

use of org.springframework.web.server.ServerWebExchange in project spring-framework by spring-projects.

the class RequestMappingInfoTests method compareTwoHttpMethodsOneParam.

@Test
public void compareTwoHttpMethodsOneParam() {
    RequestMappingInfo none = paths().build();
    RequestMappingInfo oneMethod = paths().methods(RequestMethod.GET).build();
    RequestMappingInfo oneMethodOneParam = paths().methods(RequestMethod.GET).params("foo").build();
    ServerWebExchange exchange = MockServerHttpRequest.get("/foo").toExchange();
    Comparator<RequestMappingInfo> comparator = (info, otherInfo) -> info.compareTo(otherInfo, exchange);
    List<RequestMappingInfo> list = asList(none, oneMethod, oneMethodOneParam);
    Collections.shuffle(list);
    list.sort(comparator);
    assertEquals(oneMethodOneParam, list.get(0));
    assertEquals(oneMethod, list.get(1));
    assertEquals(none, list.get(2));
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) Assert.assertNotNull(org.junit.Assert.assertNotNull) MediaType(org.springframework.http.MediaType) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Test(org.junit.Test) RequestMappingInfo.paths(org.springframework.web.reactive.result.method.RequestMappingInfo.paths) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) ServerWebExchange(org.springframework.web.server.ServerWebExchange) List(java.util.List) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) Assert.assertNull(org.junit.Assert.assertNull) Ignore(org.junit.Ignore) Arrays.asList(java.util.Arrays.asList) Assert.assertFalse(org.junit.Assert.assertFalse) Comparator(java.util.Comparator) Collections(java.util.Collections) RequestMappingInfo(org.springframework.web.reactive.result.method.RequestMappingInfo) Assert.assertEquals(org.junit.Assert.assertEquals) ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) RequestMappingInfo(org.springframework.web.reactive.result.method.RequestMappingInfo) Test(org.junit.Test)

Example 35 with ServerWebExchange

use of org.springframework.web.server.ServerWebExchange in project spring-framework by spring-projects.

the class RequestMappingInfoTests method matchProducesCondition.

@Test
public void matchProducesCondition() {
    ServerWebExchange exchange = MockServerHttpRequest.get("/foo").accept(MediaType.TEXT_PLAIN).toExchange();
    RequestMappingInfo info = paths("/foo").produces("text/plain").build();
    RequestMappingInfo match = info.getMatchingCondition(exchange);
    assertNotNull(match);
    info = paths("/foo").produces("application/xml").build();
    match = info.getMatchingCondition(exchange);
    assertNull(match);
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) RequestMappingInfo(org.springframework.web.reactive.result.method.RequestMappingInfo) Test(org.junit.Test)

Aggregations

ServerWebExchange (org.springframework.web.server.ServerWebExchange)158 Test (org.junit.Test)138 MockServerWebExchange (org.springframework.mock.http.server.reactive.test.MockServerWebExchange)53 Mono (reactor.core.publisher.Mono)22 ServerHttpResponse (org.springframework.http.server.reactive.ServerHttpResponse)20 MediaType (org.springframework.http.MediaType)18 BindingContext (org.springframework.web.reactive.BindingContext)17 MethodParameter (org.springframework.core.MethodParameter)15 List (java.util.List)14 ResolvableType (org.springframework.core.ResolvableType)14 Map (java.util.Map)12 Collections (java.util.Collections)11 HttpStatus (org.springframework.http.HttpStatus)11 StepVerifier (reactor.test.StepVerifier)11 Assert.assertEquals (org.junit.Assert.assertEquals)10 MockServerHttpRequest (org.springframework.mock.http.server.reactive.test.MockServerHttpRequest)10 ResponseStatusException (org.springframework.web.server.ResponseStatusException)9 ServerWebInputException (org.springframework.web.server.ServerWebInputException)9 ReactiveAdapterRegistry (org.springframework.core.ReactiveAdapterRegistry)8 NotAcceptableStatusException (org.springframework.web.server.NotAcceptableStatusException)8