|
32 | 32 | import static feign.Util.UTF_8; |
33 | 33 | import static org.testng.Assert.assertEquals; |
34 | 34 |
|
| 35 | +import javax.inject.Named; |
| 36 | + |
35 | 37 | @Test |
36 | 38 | public class RibbonClientTest { |
37 | 39 | interface TestInterface { |
38 | 40 | @RequestLine("POST /") void post(); |
| 41 | + @RequestLine("GET /?a={a}") void getWithQueryParameters(@Named("a") String a); |
39 | 42 |
|
40 | 43 | @dagger.Module(injects = Feign.class, overrides = true, addsTo = Feign.Defaults.class) |
41 | 44 | static class Module { |
@@ -108,6 +111,43 @@ public void ioExceptionRetry() throws IOException, InterruptedException { |
108 | 111 | } |
109 | 112 | } |
110 | 113 |
|
| 114 | + /* |
| 115 | + This test-case replicates a bug that occurs when using RibbonRequest with a query string. |
| 116 | +
|
| 117 | + The querystrings would not be URL-encoded, leading to invalid HTTP-requests if the query string contained |
| 118 | + invalid characters (ex. space). |
| 119 | + */ |
| 120 | + @Test public void urlEncodeQueryStringParameters () throws IOException, InterruptedException { |
| 121 | + String client = "RibbonClientTest-urlEncodeQueryStringParameters"; |
| 122 | + String serverListKey = client + ".ribbon.listOfServers"; |
| 123 | + |
| 124 | + String queryStringValue = "some string with space"; |
| 125 | + String expectedQueryStringValue = "some+string+with+space"; |
| 126 | + String expectedRequestLine = String.format("GET /?a=%s HTTP/1.1", expectedQueryStringValue); |
| 127 | + |
| 128 | + MockWebServer server = new MockWebServer(); |
| 129 | + server.enqueue(new MockResponse().setBody("success!".getBytes(UTF_8))); |
| 130 | + server.play(); |
| 131 | + |
| 132 | + getConfigInstance().setProperty(serverListKey, hostAndPort(server.getUrl(""))); |
| 133 | + |
| 134 | + try { |
| 135 | + |
| 136 | + TestInterface api = Feign.create(TestInterface.class, "http://" + client, new TestInterface.Module(), new RibbonModule()); |
| 137 | + |
| 138 | + api.getWithQueryParameters(queryStringValue); |
| 139 | + |
| 140 | + final String recordedRequestLine = server.takeRequest().getRequestLine(); |
| 141 | + |
| 142 | + assertEquals(recordedRequestLine, expectedRequestLine); |
| 143 | + } finally { |
| 144 | + server.shutdown(); |
| 145 | + getConfigInstance().clearProperty(serverListKey); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + |
| 150 | + |
111 | 151 | static String hostAndPort(URL url) { |
112 | 152 | // our build slaves have underscores in their hostnames which aren't permitted by ribbon |
113 | 153 | return "localhost:" + url.getPort(); |
|
0 commit comments