Encode query params in Zuul filter
Some slightly tricky manipulation involved here, but hopefully UriTemplate to the rescue. Fixes gh-682
This commit is contained in:
@@ -22,6 +22,7 @@ import java.io.InputStreamReader;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Collection;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
@@ -36,6 +37,7 @@ import org.springframework.boot.actuate.trace.TraceRepository;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.util.UriTemplate;
|
||||
import org.springframework.web.util.UriUtils;
|
||||
import org.springframework.web.util.WebUtils;
|
||||
|
||||
@@ -290,17 +292,27 @@ public class ProxyRequestHelper {
|
||||
}
|
||||
|
||||
public String getQueryString(MultiValueMap<String, String> params) {
|
||||
if (params.isEmpty()) {
|
||||
return "";
|
||||
}
|
||||
StringBuilder query = new StringBuilder();
|
||||
Map<String, Object> singles = new HashMap<>();
|
||||
for (String param : params.keySet()) {
|
||||
int i = 0;
|
||||
for (String value : params.get(param)) {
|
||||
query.append("&");
|
||||
query.append(param);
|
||||
if (!"".equals(value)) {
|
||||
query.append("=");
|
||||
query.append(value);
|
||||
singles.put(param + i, value);
|
||||
query.append("={");
|
||||
query.append(param + i);
|
||||
query.append("}");
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return (query.length() > 0) ? "?" + query.substring(1) : "";
|
||||
|
||||
UriTemplate template = new UriTemplate("?" + query.toString().substring(1));
|
||||
return template.expand(singles).toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,6 +51,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.netflix.client.ClientException;
|
||||
@@ -109,6 +110,28 @@ public class SampleZuulProxyApplicationTests extends ZuulProxyTestBase {
|
||||
result.getHeaders().get("X-Application-Context").toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void simpleHostRouteWithQuery() {
|
||||
this.routes.addRoute("/self/**", "http://localhost:" + this.port + "/");
|
||||
this.endpoint.reset();
|
||||
ResponseEntity<String> result = new TestRestTemplate().exchange(
|
||||
"http://localhost:" + this.port + "/self/query?foo=bar", HttpMethod.GET,
|
||||
new HttpEntity<>((Void) null), String.class);
|
||||
assertEquals(HttpStatus.OK, result.getStatusCode());
|
||||
assertEquals("/query?foo=bar", result.getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void simpleHostRouteWithEncodedQuery() {
|
||||
this.routes.addRoute("/self/**", "http://localhost:" + this.port + "/");
|
||||
this.endpoint.reset();
|
||||
ResponseEntity<String> result = new TestRestTemplate().exchange(
|
||||
"http://localhost:" + this.port + "/self/query?foo={foo}", HttpMethod.GET,
|
||||
new HttpEntity<>((Void) null), String.class, "weird#chars");
|
||||
assertEquals(HttpStatus.OK, result.getStatusCode());
|
||||
assertEquals("/query?foo=weird#chars", result.getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ribbonCommandForbidden() {
|
||||
ResponseEntity<String> result = new TestRestTemplate().exchange(
|
||||
@@ -166,6 +189,11 @@ class SampleZuulProxyApplication extends ZuulProxyTestBase.AbstractZuulProxyAppl
|
||||
return result;
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/query")
|
||||
public String addQuery(HttpServletRequest request, @RequestParam String foo) {
|
||||
return request.getRequestURI() + "?foo=" + foo;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RibbonCommandFactory<?> ribbonCommandFactory(
|
||||
SpringClientFactory clientFactory) {
|
||||
|
||||
@@ -54,8 +54,8 @@ public abstract class ZuulProxyTestBase {
|
||||
|
||||
@Before
|
||||
public void setTestRequestcontext() {
|
||||
RequestContext context = new RequestContext();
|
||||
RequestContext.testSetCurrentContext(context);
|
||||
RequestContext.testSetCurrentContext(null);
|
||||
RequestContext.getCurrentContext().unset();
|
||||
}
|
||||
|
||||
protected String getRoute(String path) {
|
||||
@@ -157,7 +157,7 @@ public abstract class ZuulProxyTestBase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void simpleHostRouteWithOriginalQString() {
|
||||
public void simpleHostRouteWithOriginalQueryString() {
|
||||
this.routes.addRoute("/self/**", "http://localhost:" + this.port);
|
||||
this.endpoint.reset();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user