Skip to content

Commit

Permalink
Add support for GET + Query String. We need to improve the Request AP…
Browse files Browse the repository at this point in the history
…I to add query String programmatically. Patch provided by Matthias Wessendorf at apache dot org
  • Loading branch information
jfarcand committed Mar 5, 2010
1 parent 8cca15b commit e8e0dbf
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/ning/http/client/RequestBuilderBase.java
Expand Up @@ -28,7 +28,7 @@

/**
* Builder for {@link Request}
*
*
* @param <T>
*/
abstract class RequestBuilderBase<T extends RequestBuilderBase<?>> {
Expand Down
Expand Up @@ -192,8 +192,17 @@ HttpRequest construct(Request request, HttpMethod m, Url url) throws IOException
host = request.getVirtualHost();
}

HttpRequest nettyRequest = new DefaultHttpRequest(
HttpRequest nettyRequest;
String queryString = url.getQueryString();

// does this GET request have a query string
if (RequestType.GET.equals(request.getType()) && queryString != null) {
nettyRequest = new DefaultHttpRequest(
HttpVersion.HTTP_1_1, m, url.getUri());
} else {
nettyRequest = new DefaultHttpRequest(
HttpVersion.HTTP_1_1, m, url.getPath());
}
nettyRequest.setHeader(HttpHeaders.Names.HOST, host + ":" + url.getPort());

Headers h = request.getHeaders();
Expand Down
Expand Up @@ -79,6 +79,14 @@ public void handle(String pathInContext,
requestBody.append("_");
}

String pathInfo = httpRequest.getPathInfo();
if (pathInfo != null)
httpResponse.addHeader("X-pathInfo", pathInfo);

String queryString = httpRequest.getQueryString();
if (queryString != null)
httpResponse.addHeader("X-queryString", queryString);

httpResponse.addHeader("X-KEEP-ALIVE", httpRequest.getRemoteAddr() + ":" + httpRequest.getRemotePort());

javax.servlet.http.Cookie[] cs = httpRequest.getCookies();
Expand Down
Expand Up @@ -936,6 +936,33 @@ public void onThrowable(Throwable t) {
}
}

@Test(groups = "async")
public void asyncDoGetQueryStringTest() throws Throwable {
AsyncHttpClient client = new AsyncHttpClient();

// Use a latch in case the assert fail
final CountDownLatch latch = new CountDownLatch(1);

AsyncCompletionHandler handler = new AsyncCompletionHandlerAdapter() {

@Override
public Response onCompleted(Response response) throws Exception {
Assert.assertTrue(response.getHeader("X-pathInfo") != null);
Assert.assertTrue(response.getHeader("X-queryString") != null);
latch.countDown();
return response;
}
};

Request req = new RequestBuilder(RequestType.GET)
.setUrl(TARGET_URL + "?foo=bar").build();

client.executeRequest(req,handler).get();

if (!latch.await(10, TimeUnit.SECONDS)) {
Assert.fail("Timed out");
}
}

@Test(groups = "async")
public void asyncDoGetKeepAliveHandlerTest() throws Throwable {
Expand Down

0 comments on commit e8e0dbf

Please sign in to comment.