Skip to content

Commit

Permalink
Added support for spaces in path
Browse files Browse the repository at this point in the history
  • Loading branch information
asukhyy committed Sep 20, 2015
1 parent 0904133 commit 93cd0b7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 16 deletions.
40 changes: 30 additions & 10 deletions httpclient/src/main/java/feign/httpclient/ApacheHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
Expand Down Expand Up @@ -101,30 +102,22 @@ HttpUriRequest toHttpUriRequest(Request request, Request.Options options) throws

URI uri = new URIBuilder(request.url()).build();

//request url
requestBuilder.setUri(uri.getScheme() + "://" + uri.getAuthority() + uri.getPath());
requestBuilder.setUri(uri.getScheme() + "://" + uri.getAuthority() + uri.getRawPath());

//request query params
List<NameValuePair> queryParams = URLEncodedUtils.parse(uri, requestBuilder.getCharset().name());
for (NameValuePair queryParam: queryParams) {
requestBuilder.addParameter(queryParam);
}

//request body
if (request.body() != null) {
HttpEntity entity = request.charset() != null ?
new StringEntity(new String(request.body(), request.charset())) :
new ByteArrayEntity(request.body());
requestBuilder.setEntity(entity);
}

//request headers
boolean hasAcceptHeader = false;
for (Map.Entry<String, Collection<String>> headerEntry : request.headers().entrySet()) {
String headerName = headerEntry.getKey();
if (headerName.equalsIgnoreCase(ACCEPT_HEADER_NAME)) {
hasAcceptHeader = true;
}

if (headerName.equalsIgnoreCase(Util.CONTENT_LENGTH) &&
requestBuilder.getHeaders(headerName) != null) {
//if the 'Content-Length' header is already present, it's been set from HttpEntity, so we
Expand All @@ -141,9 +134,36 @@ HttpUriRequest toHttpUriRequest(Request request, Request.Options options) throws
requestBuilder.addHeader(ACCEPT_HEADER_NAME, "*/*");
}

//request body
if (request.body() != null) {
HttpEntity entity = null;
if (request.charset() != null) {
ContentType contentType = getContentType(request);
String content = new String(request.body(), request.charset());
entity = new StringEntity(content, contentType);
} else {
entity = new ByteArrayEntity(request.body());
}

requestBuilder.setEntity(entity);
}

return requestBuilder.build();
}

private ContentType getContentType(Request request) {
ContentType contentType = ContentType.DEFAULT_TEXT;
for (Map.Entry<String, Collection<String>> entry : request.headers().entrySet())
if (entry.getKey().equalsIgnoreCase("Content-Type")) {
Collection values = entry.getValue();
if (values != null && !values.isEmpty()) {
contentType = ContentType.create(entry.getValue().iterator().next(), request.charset());
break;
}
}
return contentType;
}

Response toFeignResponse(HttpResponse httpResponse) throws IOException {
StatusLine statusLine = httpResponse.getStatusLine();
int statusCode = statusLine.getStatusCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@

import com.squareup.okhttp.mockwebserver.MockResponse;
import com.squareup.okhttp.mockwebserver.MockWebServer;
import feign.Feign;
import feign.FeignException;
import feign.Headers;
import feign.Logger;
import feign.RequestLine;
import feign.Response;

import org.junit.Rule;
import org.junit.Test;
Expand All @@ -31,6 +25,14 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;

import feign.Feign;
import feign.FeignException;
import feign.Headers;
import feign.Logger;
import feign.Param;
import feign.RequestLine;
import feign.Response;

import static feign.Util.UTF_8;
import static feign.assertj.MockWebServerAssertions.assertThat;
import static java.util.Arrays.asList;
Expand Down Expand Up @@ -143,6 +145,20 @@ public void noResponseBody() {

api.noPostBody();
}
@Test
public void postWithSpacesInPath() throws IOException, InterruptedException {
server.enqueue(new MockResponse().setBody("foo"));

TestInterface api = Feign.builder()
.client(new ApacheHttpClient())
.target(TestInterface.class, "http://localhost:" + server.getPort());

Response response = api.post("current documents", "foo");

assertThat(server.takeRequest()).hasMethod("POST")
.hasPath("/path/current%20documents/resource")
.hasBody("foo");
}

interface TestInterface {

Expand All @@ -160,5 +176,9 @@ interface TestInterface {

@RequestLine("POST")
String noPostBody();

@RequestLine("POST /path/{to}/resource")
@Headers("Accept: text/plain")
Response post(@Param("to") String to, String body);
}
}

0 comments on commit 93cd0b7

Please sign in to comment.