Search in sources :

Example 51 with MockServerHttpRequest

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

the class LoadBalancerClientFilterTests method happyPathWithAttributeRatherThanScheme.

@Test
public void happyPathWithAttributeRatherThanScheme() {
    MockServerHttpRequest request = MockServerHttpRequest.get("ws://localhost/get?a=b").build();
    URI lbUri = URI.create("ws://service1?a=b");
    exchange = MockServerWebExchange.from(request);
    exchange.getAttributes().put(GATEWAY_SCHEME_PREFIX_ATTR, "lb");
    ServerWebExchange webExchange = testFilter(exchange, lbUri);
    URI uri = webExchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
    assertThat(uri).hasScheme("ws").hasHost("service1-host1").hasParameter("a", "b");
}
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 52 with MockServerHttpRequest

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

the class LoadBalancerClientFilterTests method happyPath.

@Test
public void happyPath() {
    MockServerHttpRequest request = MockServerHttpRequest.get("http://localhost/get?a=b").build();
    URI lbUri = URI.create("lb://service1?a=b");
    ServerWebExchange webExchange = testFilter(request, lbUri);
    URI uri = webExchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
    assertThat(uri).hasScheme("http").hasHost("service1-host1").hasParameter("a", "b");
}
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 53 with MockServerHttpRequest

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

the class RouteToRequestUrlFilterTests method testFilter.

private ServerWebExchange testFilter(MockServerHttpRequest request, String url) {
    Route value = Route.builder().id("1").uri(URI.create(url)).order(0).predicate(swe -> true).build();
    ServerWebExchange exchange = MockServerWebExchange.from(request);
    exchange.getAttributes().put(GATEWAY_ROUTE_ATTR, value);
    GatewayFilterChain filterChain = mock(GatewayFilterChain.class);
    ArgumentCaptor<ServerWebExchange> captor = ArgumentCaptor.forClass(ServerWebExchange.class);
    when(filterChain.filter(captor.capture())).thenReturn(Mono.empty());
    RouteToRequestUrlFilter filter = new RouteToRequestUrlFilter();
    filter.filter(exchange, filterChain);
    return captor.getValue();
}
Also used : UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Mockito.when(org.mockito.Mockito.when) ServerWebExchange(org.springframework.web.server.ServerWebExchange) ArgumentCaptor(org.mockito.ArgumentCaptor) GATEWAY_REQUEST_URL_ATTR(org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR) GATEWAY_ROUTE_ATTR(org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR) GATEWAY_SCHEME_PREFIX_ATTR(org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_SCHEME_PREFIX_ATTR) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) URI(java.net.URI) Collections(java.util.Collections) Route(org.springframework.cloud.gateway.route.Route) Mockito.mock(org.mockito.Mockito.mock) ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) Route(org.springframework.cloud.gateway.route.Route)

Example 54 with MockServerHttpRequest

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

the class RouteToRequestUrlFilterTests method noQueryParams.

@Test
public void noQueryParams() {
    MockServerHttpRequest request = MockServerHttpRequest.get("http://localhost/get").build();
    ServerWebExchange webExchange = testFilter(request, "http://myhost");
    URI uri = webExchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
    assertThat(uri).hasScheme("http").hasHost("myhost");
}
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 55 with MockServerHttpRequest

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

the class RouteToRequestUrlFilterTests method unencodedParameters.

@Test
public void unencodedParameters() {
    URI url = URI.create("http://localhost/get?a=b&c=d[]");
    // prove that it is unencoded
    assertThat(url.getRawQuery()).isEqualTo("a=b&c=d[]");
    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").hasParameter("a", "b").hasParameter("c", "d[]");
    // prove that it is NOT encoded
    assertThat(uri.getRawQuery()).isEqualTo("a=b&c=d[]");
}
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