Skip to content

Commit 82b57a3

Browse files
zhujf1989adriancole
authored andcommitted
Fix the issue encountered when the value of queries starting with '{' (OpenFeign#555)
support the query values starting with {, when use RibbonClient。different from OpenFeign#540.
1 parent d7f40f5 commit 82b57a3

2 files changed

Lines changed: 50 additions & 6 deletions

File tree

ribbon/src/main/java/feign/ribbon/LBClient.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,16 @@
2727

2828
import java.io.IOException;
2929
import java.net.URI;
30+
import java.util.Arrays;
3031
import java.util.Collection;
32+
import java.util.LinkedHashMap;
3133
import java.util.Map;
3234

3335
import feign.Client;
3436
import feign.Request;
3537
import feign.RequestTemplate;
3638
import feign.Response;
39+
import feign.Util;
3740

3841
public final class LBClient extends
3942
AbstractLoadBalancerAwareClient<LBClient.RibbonRequest, LBClient.RibbonResponse> {
@@ -94,12 +97,14 @@ static class RibbonRequest extends ClientRequest implements Cloneable {
9497
}
9598

9699
Request toRequest() {
97-
return new RequestTemplate()
98-
.method(request.method())
99-
.append(getUri().toASCIIString())
100-
.headers(request.headers())
101-
.body(request.body(), request.charset())
102-
.request();
100+
// add header "Content-Length" according to the request body
101+
final byte[] body = request.body();
102+
final int bodyLength = body != null ? body.length : 0;
103+
// create a new Map to avoid side effect, not to change the old headers
104+
Map<String, Collection<String>> headers = new LinkedHashMap<String, Collection<String>>();
105+
headers.putAll(request.headers());
106+
headers.put(Util.CONTENT_LENGTH, Arrays.asList(String.valueOf(bodyLength)));
107+
return Request.create(request.method(), getUri().toASCIIString(), headers, body, request.charset());
103108
}
104109

105110
Client client() {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package feign.ribbon;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.net.URI;
6+
import java.net.URISyntaxException;
7+
import java.nio.charset.Charset;
8+
import java.util.Collection;
9+
import java.util.LinkedHashMap;
10+
import java.util.Map;
11+
12+
import org.junit.Test;
13+
14+
import feign.Request;
15+
import feign.ribbon.LBClient.RibbonRequest;
16+
17+
public class LBClientTest {
18+
19+
@Test
20+
public void testRibbonRequest() throws URISyntaxException {
21+
// test for RibbonRequest.toRequest()
22+
// the url has a query whose value is an encoded json string
23+
String urlWithEncodedJson = "http://test.feign.com/p?q=%7b%22a%22%3a1%7d";
24+
String method = "GET";
25+
URI uri = new URI(urlWithEncodedJson);
26+
Map<String, Collection<String>> headers = new LinkedHashMap<String, Collection<String>>();
27+
// create a Request for recreating another Request by toRequest()
28+
Request requestOrigin = Request.create(method, uri.toASCIIString(), headers, null, Charset.forName("utf-8"));
29+
RibbonRequest ribbonRequest = new RibbonRequest(null, requestOrigin, uri);
30+
31+
// use toRequest() recreate a Request
32+
Request requestRecreate = ribbonRequest.toRequest();
33+
34+
// test that requestOrigin and requestRecreate are same except the header 'Content-Length'
35+
// ps, requestOrigin and requestRecreate won't be null
36+
assertThat(requestOrigin.toString()).isEqualTo(String.format("%s %s HTTP/1.1\n", method, urlWithEncodedJson));
37+
assertThat(requestRecreate.toString()).isEqualTo(String.format("%s %s HTTP/1.1\nContent-Length: 0\n", method, urlWithEncodedJson));
38+
}
39+
}

0 commit comments

Comments
 (0)