Skip to content

Commit

Permalink
Encode query params in Zuul filter
Browse files Browse the repository at this point in the history
Some slightly tricky manipulation involved here, but hopefully
UriTemplate to the rescue.

Fixes gh-682
  • Loading branch information
Dave Syer committed Feb 23, 2016
1 parent 3f34ccc commit 41c3640
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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;
Expand All @@ -36,6 +37,7 @@
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;

Expand Down Expand Up @@ -290,17 +292,27 @@ private void debugRequestEntity(Map<String, Object> info, InputStream inputStrea
}

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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
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;
Expand Down Expand Up @@ -109,6 +110,28 @@ public void simpleHostRouteDefaultIgnoredHeader() {
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(
Expand Down Expand Up @@ -166,6 +189,11 @@ public ResponseEntity<String> addHeader(HttpServletRequest request) {
return result;
}

@RequestMapping(value = "/query")
public String addQuery(HttpServletRequest request, @RequestParam String foo) {
return request.getRequestURI() + "?foo=" + foo;
}

@Bean
public RibbonCommandFactory<?> ribbonCommandFactory(
SpringClientFactory clientFactory) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -157,7 +157,7 @@ public void simpleHostRouteWithSpace() {
}

@Test
public void simpleHostRouteWithOriginalQString() {
public void simpleHostRouteWithOriginalQueryString() {
this.routes.addRoute("/self/**", "http://localhost:" + this.port);
this.endpoint.reset();

Expand Down

0 comments on commit 41c3640

Please sign in to comment.