Search in sources :

Example 11 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-cloud-gateway by spring-cloud.

the class CloudFoundryRouteServicePredicateFactoryTest method itReturnsFalseWithNoHeaders.

@Test
public void itReturnsFalseWithNoHeaders() {
    MockServerHttpRequest request = MockServerHttpRequest.get("someurl").build();
    MockServerWebExchange exchange = MockServerWebExchange.from(request);
    assertThat(predicate.test(exchange)).isFalse();
}
Also used : MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) Test(org.junit.Test)

Example 12 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-cloud-gateway by spring-cloud.

the class CookieRoutePredicateFactoryTests method okOneCookieForYou.

@Test
public void okOneCookieForYou() {
    MockServerHttpRequest request = MockServerHttpRequest.get("http://example.com").cookie(new HttpCookie("yourcookie", "sugar"), new HttpCookie("mycookie", "chip")).build();
    MockServerWebExchange exchange = MockServerWebExchange.from(request);
    Predicate<ServerWebExchange> predicate = new CookieRoutePredicateFactory().apply(new Config().setName("mycookie").setRegexp("ch.p"));
    assertThat(predicate.test(exchange)).isTrue();
}
Also used : MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) Config(org.springframework.cloud.gateway.handler.predicate.CookieRoutePredicateFactory.Config) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) HttpCookie(org.springframework.http.HttpCookie) Test(org.junit.Test)

Example 13 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-cloud-gateway by spring-cloud.

the class LoadBalancerClientFilterTests method encodedParameters.

@Test
public void encodedParameters() {
    URI url = UriComponentsBuilder.fromUriString("http://localhost/get?a=b&c=d[]").buildAndExpand().encode().toUri();
    MockServerHttpRequest request = MockServerHttpRequest.method(HttpMethod.GET, url).build();
    URI lbUrl = UriComponentsBuilder.fromUriString("lb://service1?a=b&c=d[]").buildAndExpand().encode().toUri();
    // prove that it is encoded
    assertThat(lbUrl.getRawQuery()).isEqualTo("a=b&c=d%5B%5D");
    assertThat(lbUrl).hasParameter("c", "d[]");
    ServerWebExchange webExchange = testFilter(request, lbUrl);
    URI uri = webExchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
    assertThat(uri).hasScheme("http").hasHost("service1-host1").hasParameter("a", "b").hasParameter("c", "d[]");
    // prove that it is not double encoded
    assertThat(uri.getRawQuery()).isEqualTo("a=b&c=d%5B%5D");
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) URI(java.net.URI) Test(org.junit.Test)

Example 14 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-cloud-gateway by spring-cloud.

the class LoadBalancerClientFilterTests method noQueryParams.

@Test
public void noQueryParams() {
    MockServerHttpRequest request = MockServerHttpRequest.get("http://localhost/get").build();
    ServerWebExchange webExchange = testFilter(request, URI.create("lb://service1"));
    URI uri = webExchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
    assertThat(uri).hasScheme("http").hasHost("service1-host1");
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) URI(java.net.URI) Test(org.junit.Test)

Example 15 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-cloud-gateway by spring-cloud.

the class RouteToRequestUrlFilterTests method encodedUrl.

@Test
public void encodedUrl() {
    URI url = UriComponentsBuilder.fromUriString("http://localhost/abc def/get").buildAndExpand().encode().toUri();
    // prove that it is encoded
    assertThat(url.getRawPath()).isEqualTo("/abc%20def/get");
    assertThat(url).hasPath("/abc def/get");
    MockServerHttpRequest request = MockServerHttpRequest.method(HttpMethod.GET, url).build();
    ServerWebExchange webExchange = testFilter(request, "http://myhost");
    URI uri = webExchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
    assertThat(uri).hasScheme("http").hasHost("myhost").hasPath("/abc def/get");
    // prove that it is not double encoded
    assertThat(uri.getRawPath()).isEqualTo("/abc%20def/get");
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) URI(java.net.URI) Test(org.junit.Test)

Aggregations

MockServerHttpRequest (org.springframework.mock.http.server.reactive.MockServerHttpRequest)75 Test (org.junit.jupiter.api.Test)40 MockServerWebExchange (org.springframework.mock.web.server.MockServerWebExchange)35 Test (org.junit.Test)26 ServerWebExchange (org.springframework.web.server.ServerWebExchange)26 URI (java.net.URI)16 OAuth2AuthorizationRequest (org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest)11 HttpHeaders (org.springframework.http.HttpHeaders)9 BeforeEach (org.junit.jupiter.api.BeforeEach)7 ResponseStatusException (org.springframework.web.server.ResponseStatusException)7 AcceptHeaderLocaleContextResolver (org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver)6 Mono (reactor.core.publisher.Mono)6 InetSocketAddress (java.net.InetSocketAddress)5 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)5 Mockito.mock (org.mockito.Mockito.mock)5 ObjectError (org.springframework.validation.ObjectError)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)4 BDDMockito.given (org.mockito.BDDMockito.given)4