Skip to content

Commit

Permalink
Fix the issue encountered when the value of queries starting with '{' (
Browse files Browse the repository at this point in the history
…OpenFeign#555)

support the query values starting with {, when use RibbonClient。different from OpenFeign#540.
  • Loading branch information
zhujf1989 authored and adriancole committed May 6, 2017
1 parent d7f40f5 commit 82b57a3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
17 changes: 11 additions & 6 deletions ribbon/src/main/java/feign/ribbon/LBClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@

import java.io.IOException;
import java.net.URI;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

import feign.Client;
import feign.Request;
import feign.RequestTemplate;
import feign.Response;
import feign.Util;

public final class LBClient extends
AbstractLoadBalancerAwareClient<LBClient.RibbonRequest, LBClient.RibbonResponse> {
Expand Down Expand Up @@ -94,12 +97,14 @@ static class RibbonRequest extends ClientRequest implements Cloneable {
}

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

Client client() {
Expand Down
39 changes: 39 additions & 0 deletions ribbon/src/test/java/feign/ribbon/LBClientTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package feign.ribbon;

import static org.assertj.core.api.Assertions.assertThat;

import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

import org.junit.Test;

import feign.Request;
import feign.ribbon.LBClient.RibbonRequest;

public class LBClientTest {

@Test
public void testRibbonRequest() throws URISyntaxException {
// test for RibbonRequest.toRequest()
// the url has a query whose value is an encoded json string
String urlWithEncodedJson = "http://test.feign.com/p?q=%7b%22a%22%3a1%7d";
String method = "GET";
URI uri = new URI(urlWithEncodedJson);
Map<String, Collection<String>> headers = new LinkedHashMap<String, Collection<String>>();
// create a Request for recreating another Request by toRequest()
Request requestOrigin = Request.create(method, uri.toASCIIString(), headers, null, Charset.forName("utf-8"));
RibbonRequest ribbonRequest = new RibbonRequest(null, requestOrigin, uri);

// use toRequest() recreate a Request
Request requestRecreate = ribbonRequest.toRequest();

// test that requestOrigin and requestRecreate are same except the header 'Content-Length'
// ps, requestOrigin and requestRecreate won't be null
assertThat(requestOrigin.toString()).isEqualTo(String.format("%s %s HTTP/1.1\n", method, urlWithEncodedJson));
assertThat(requestRecreate.toString()).isEqualTo(String.format("%s %s HTTP/1.1\nContent-Length: 0\n", method, urlWithEncodedJson));
}
}

0 comments on commit 82b57a3

Please sign in to comment.