Skip to content

Commit

Permalink
Doesn't assume encoded query parameters.
Browse files Browse the repository at this point in the history
Allows unencoded parameters to pass through.

fixes spring-cloudgh-139
  • Loading branch information
spencergibb committed Dec 19, 2017
1 parent 6aa8cd6 commit 33bd6ff
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
.scheme(instance.isSecure()? "https" : "http") //TODO: support websockets
.host(instance.getHost())
.port(instance.getPort())
.build(true)
.build(false)
.toUri();
log.trace("LoadBalancerClientFilter url chosen: " + requestUrl);
exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, requestUrl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
log.trace("RouteToRequestUrlFilter start");
URI requestUrl = UriComponentsBuilder.fromHttpRequest(exchange.getRequest())
.uri(route.getUri())
.build(true)
.build(false)
.toUri();
exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, requestUrl);
return chain.filter(exchange);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright 2013-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.springframework.cloud.gateway.filter;

import java.net.URI;
import java.util.Collections;

import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.springframework.cloud.gateway.route.Route;
import org.springframework.http.HttpMethod;
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
import org.springframework.mock.web.server.MockServerWebExchange;
import org.springframework.web.server.ServerWebExchange;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR;

import org.springframework.web.util.UriComponentsBuilder;
import reactor.core.publisher.Mono;

/**
* @author Spencer Gibb
*/
public class RouteToRequestUrlFilterTests {

@Test
public void happyPath() {
MockServerHttpRequest request = MockServerHttpRequest
.get("http:https://localhost/get?a=b")
.build();

ServerWebExchange webExchange = testFilter(request, "http:https://myhost");
URI uri = webExchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
assertThat(uri).hasScheme("http").hasHost("myhost")
.hasParameter("a", "b");
}

@Test
public void encodedParameters() {
URI url = UriComponentsBuilder.fromUriString("http:https://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:https://myhost");
URI uri = webExchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
assertThat(uri).hasScheme("http").hasHost("myhost")
.hasParameter("a", "b")
.hasParameter("c", "d[]");
}

@Test
public void unencodedParameters() {
URI url = URI.create("http:https://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:https://myhost");

URI uri = webExchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
assertThat(uri).hasScheme("http").hasHost("myhost")
.hasParameter("a", "b")
.hasParameter("c", "d[]");
}

private ServerWebExchange testFilter(MockServerHttpRequest request, String url) {
Route value = new Route("1", URI.create(url), 0,
swe -> true, Collections.emptyList());

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

0 comments on commit 33bd6ff

Please sign in to comment.