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");
}
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");
}
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();
}
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();
}
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));
}
Aggregations