Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
spring-cloud-netflix
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
openSource
spring-cloud-netflix
Commits
41c36400
Commit
41c36400
authored
Feb 23, 2016
by
Dave Syer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Encode query params in Zuul filter
Some slightly tricky manipulation involved here, but hopefully UriTemplate to the rescue. Fixes gh-682
parent
3f34ccc8
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
46 additions
and
6 deletions
+46
-6
ProxyRequestHelper.java
...mework/cloud/netflix/zuul/filters/ProxyRequestHelper.java
+15
-3
SampleZuulProxyApplicationTests.java
...k/cloud/netflix/zuul/SampleZuulProxyApplicationTests.java
+28
-0
ZuulProxyTestBase.java
...springframework/cloud/netflix/zuul/ZuulProxyTestBase.java
+3
-3
No files found.
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/ProxyRequestHelper.java
View file @
41c36400
...
...
@@ -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
();
}
}
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/SampleZuulProxyApplicationTests.java
View file @
41c36400
...
...
@@ -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
;
...
...
@@ -110,6 +111,28 @@ public class SampleZuulProxyApplicationTests extends ZuulProxyTestBase {
}
@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
(
"http://localhost:"
+
this
.
port
+
"/simple/throwexception/403"
,
...
...
@@ -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
)
{
...
...
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/ZuulProxyTestBase.java
View file @
41c36400
...
...
@@ -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
simpleHostRouteWithOriginalQ
uery
String
()
{
this
.
routes
.
addRoute
(
"/self/**"
,
"http://localhost:"
+
this
.
port
);
this
.
endpoint
.
reset
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment