Search in sources :

Example 56 with MockServerHttpRequest

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

the class RouteToRequestUrlFilterTests method encodedParameters.

@Test
public void encodedParameters() {
    URI url = UriComponentsBuilder.fromUriString("http://localhost/get?a=b&c=d[]").buildAndExpand().encode().toUri();
    // prove that it is encoded
    assertThat(url.getRawQuery()).isEqualTo("a=b&c=d%5B%5D");
    assertThat(url).hasParameter("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 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 57 with MockServerHttpRequest

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

the class XForwardedHeadersFilterTests method appendDisabled.

@Test
public void appendDisabled() throws Exception {
    MockServerHttpRequest request = MockServerHttpRequest.get("http://localhost:8080/get").remoteAddress(new InetSocketAddress(InetAddress.getByName("10.0.0.1"), 80)).header(X_FORWARDED_FOR_HEADER, "192.168.0.2").header(X_FORWARDED_HOST_HEADER, "example.com").header(X_FORWARDED_PORT_HEADER, "443").header(X_FORWARDED_PROTO_HEADER, "https").build();
    XForwardedHeadersFilter filter = new XForwardedHeadersFilter();
    filter.setForAppend(false);
    filter.setHostAppend(false);
    filter.setPortAppend(false);
    filter.setProtoAppend(false);
    HttpHeaders headers = filter.filter(request.getHeaders(), MockServerWebExchange.from(request));
    assertThat(headers).containsKeys(X_FORWARDED_FOR_HEADER, X_FORWARDED_HOST_HEADER, X_FORWARDED_PORT_HEADER, X_FORWARDED_PROTO_HEADER);
    assertThat(headers.getFirst(X_FORWARDED_FOR_HEADER)).isEqualTo("10.0.0.1");
    assertThat(headers.getFirst(X_FORWARDED_HOST_HEADER)).isEqualTo("localhost:8080");
    assertThat(headers.getFirst(X_FORWARDED_PORT_HEADER)).isEqualTo("8080");
    assertThat(headers.getFirst(X_FORWARDED_PROTO_HEADER)).isEqualTo("http");
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Example 58 with MockServerHttpRequest

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

the class CloudFoundryRouteServicePredicateFactoryTest method itReturnsTrueWithAllHeadersPresent.

@Test
public void itReturnsTrueWithAllHeadersPresent() {
    MockServerHttpRequest request = MockServerHttpRequest.get("someurl").header(CloudFoundryRouteServicePredicateFactory.X_CF_FORWARDED_URL, "url").header(CloudFoundryRouteServicePredicateFactory.X_CF_PROXY_METADATA, "metadata").header(CloudFoundryRouteServicePredicateFactory.X_CF_PROXY_SIGNATURE, "signature").build();
    MockServerWebExchange exchange = MockServerWebExchange.from(request);
    assertThat(predicate.test(exchange)).isTrue();
}
Also used : MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) Test(org.junit.Test)

Example 59 with MockServerHttpRequest

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

the class CloudFoundryRouteServicePredicateFactoryTest method itReturnsFalseWithAHeadersMissing.

@Test
public void itReturnsFalseWithAHeadersMissing() {
    MockServerHttpRequest request = MockServerHttpRequest.get("someurl").header(CloudFoundryRouteServicePredicateFactory.X_CF_FORWARDED_URL, "url").header(CloudFoundryRouteServicePredicateFactory.X_CF_PROXY_METADATA, "metadata").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 60 with MockServerHttpRequest

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

the class RequestRateLimiterGatewayFilterFactoryTests method assertFilterFactory.

private void assertFilterFactory(KeyResolver keyResolver, String key, boolean allowed, HttpStatus expectedStatus) {
    Tuple args = tuple().build();
    when(rateLimiter.isAllowed("myroute", key)).thenReturn(Mono.just(new Response(allowed, 1)));
    MockServerHttpRequest request = MockServerHttpRequest.get("/").build();
    MockServerWebExchange exchange = MockServerWebExchange.from(request);
    exchange.getResponse().setStatusCode(HttpStatus.OK);
    exchange.getAttributes().put(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR, Route.builder().id("myroute").predicate(ex -> true).uri("http://localhost").build());
    when(this.filterChain.filter(exchange)).thenReturn(Mono.empty());
    RequestRateLimiterGatewayFilterFactory factory = this.context.getBean(RequestRateLimiterGatewayFilterFactory.class);
    GatewayFilter filter = factory.apply(config -> config.setKeyResolver(keyResolver));
    Mono<Void> response = filter.filter(exchange, this.filterChain);
    response.subscribe(aVoid -> assertThat(exchange.getResponse().getStatusCode()).isEqualTo(expectedStatus));
}
Also used : Response(org.springframework.cloud.gateway.filter.ratelimit.RateLimiter.Response) DirtiesContext(org.springframework.test.annotation.DirtiesContext) TupleBuilder.tuple(org.springframework.tuple.TupleBuilder.tuple) GatewayFilterChain(org.springframework.cloud.gateway.filter.GatewayFilterChain) ServerWebExchangeUtils(org.springframework.cloud.gateway.support.ServerWebExchangeUtils) Response(org.springframework.cloud.gateway.filter.ratelimit.RateLimiter.Response) RateLimiter(org.springframework.cloud.gateway.filter.ratelimit.RateLimiter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) BaseWebClientTests(org.springframework.cloud.gateway.test.BaseWebClientTests) Autowired(org.springframework.beans.factory.annotation.Autowired) Qualifier(org.springframework.beans.factory.annotation.Qualifier) KeyResolver(org.springframework.cloud.gateway.filter.ratelimit.KeyResolver) SpringRunner(org.springframework.test.context.junit4.SpringRunner) RANDOM_PORT(org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT) MockBean(org.springframework.boot.test.mock.mockito.MockBean) EnableAutoConfiguration(org.springframework.boot.autoconfigure.EnableAutoConfiguration) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) Import(org.springframework.context.annotation.Import) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Mockito.when(org.mockito.Mockito.when) ApplicationContext(org.springframework.context.ApplicationContext) HttpStatus(org.springframework.http.HttpStatus) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Tuple(org.springframework.tuple.Tuple) SpringBootConfiguration(org.springframework.boot.SpringBootConfiguration) GatewayFilter(org.springframework.cloud.gateway.filter.GatewayFilter) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) Bean(org.springframework.context.annotation.Bean) Route(org.springframework.cloud.gateway.route.Route) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) Tuple(org.springframework.tuple.Tuple) GatewayFilter(org.springframework.cloud.gateway.filter.GatewayFilter)

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