Skip to content

Commit f4b2a6e

Browse files
author
Stephane Landelle
committed
Have a getUrl shortcut on Request without having to grab the UriComponents, close #689
1 parent 9571d2d commit f4b2a6e

File tree

6 files changed

+21
-15
lines changed

6 files changed

+21
-15
lines changed

src/main/java/com/ning/http/client/Request.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public interface Request {
4747

4848
UriComponents getURI();
4949

50+
String getUrl();
51+
5052
/**
5153
* Return the InetAddress to override
5254
*

src/main/java/com/ning/http/client/RequestBuilderBase.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ public UriComponents getURI() {
116116
return uri;
117117
}
118118

119+
public String getUrl() {
120+
return uri.toUrl();
121+
}
122+
119123
public FluentCaseInsensitiveStringsMap getHeaders() {
120124
return headers;
121125
}
@@ -208,7 +212,7 @@ public List<Param> getQueryParams() {
208212

209213
@Override
210214
public String toString() {
211-
StringBuilder sb = new StringBuilder(getURI().toUrl());
215+
StringBuilder sb = new StringBuilder(getUrl());
212216

213217
sb.append("\t");
214218
sb.append(method);

src/main/java/com/ning/http/client/providers/apache/ApacheAsyncHttpProvider.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ private HttpMethodBase createMethod(HttpClient client, Request request) throws I
250250
String methodName = request.getMethod();
251251
HttpMethodBase method = null;
252252
if (methodName.equalsIgnoreCase("POST") || methodName.equalsIgnoreCase("PUT")) {
253-
EntityEnclosingMethod post = methodName.equalsIgnoreCase("POST") ? new PostMethod(request.getURI().toUrl()) : new PutMethod(request.getURI().toUrl());
253+
EntityEnclosingMethod post = methodName.equalsIgnoreCase("POST") ? new PostMethod(request.getUrl()) : new PutMethod(request.getUrl());
254254

255255
String bodyCharset = request.getBodyEncoding() == null ? DEFAULT_CHARSET.name() : request.getBodyEncoding();
256256

@@ -340,13 +340,13 @@ private HttpMethodBase createMethod(HttpClient client, Request request) throws I
340340
}
341341
method = post;
342342
} else if (methodName.equalsIgnoreCase("DELETE")) {
343-
method = new DeleteMethod(request.getURI().toUrl());
343+
method = new DeleteMethod(request.getUrl());
344344
} else if (methodName.equalsIgnoreCase("HEAD")) {
345-
method = new HeadMethod(request.getURI().toUrl());
345+
method = new HeadMethod(request.getUrl());
346346
} else if (methodName.equalsIgnoreCase("GET")) {
347-
method = new GetMethod(request.getURI().toUrl());
347+
method = new GetMethod(request.getUrl());
348348
} else if (methodName.equalsIgnoreCase("OPTIONS")) {
349-
method = new OptionsMethod(request.getURI().toUrl());
349+
method = new OptionsMethod(request.getUrl());
350350
} else {
351351
throw new IllegalStateException(String.format("Invalid Method", methodName));
352352
}

src/main/java/com/ning/http/client/resumable/ResumableAsyncHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public AsyncHandler.STATE onHeadersReceived(HttpResponseHeaders headers) throws
185185
*/
186186
public Request adjustRequestRange(Request request) {
187187

188-
Long ri = resumableIndex.get(request.getURI().toUrl());
188+
Long ri = resumableIndex.get(request.getUrl());
189189
if (ri != null) {
190190
byteTransferred.set(ri);
191191
}

src/test/java/com/ning/http/client/async/AsyncProvidersBasicTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void asyncProviderEncodingTest() throws Throwable {
7070
AsyncHttpClient client = getAsyncHttpClient(null);
7171
try {
7272
Request request = new RequestBuilder("GET").setUrl(getTargetUrl() + "?q=+%20x").build();
73-
String requestUrl = request.getURI().toUrl();
73+
String requestUrl = request.getUrl();
7474
Assert.assertEquals(requestUrl, getTargetUrl() + "?q=%20%20x");
7575
Future<String> responseFuture = client.executeRequest(request, new AsyncCompletionHandler<String>() {
7676
@Override

src/test/java/com/ning/http/client/async/RequestBuilderTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public void testEncodesQueryParameters() throws UnsupportedEncodingException {
7272
}
7373
String expValue = sb.toString();
7474
Request request = builder.build();
75-
assertEquals(request.getURI().toUrl(), "http://example.com/?name=" + expValue);
75+
assertEquals(request.getUrl(), "http://example.com/?name=" + expValue);
7676
}
7777
}
7878

@@ -95,7 +95,7 @@ public void testParsesQueryParams() throws IOException, ExecutionException, Inte
9595
.addQueryParam("param2", "value2")
9696
.build();
9797

98-
assertEquals(request.getURI().toUrl(), "http://foo.com/?param1=value1&param2=value2");
98+
assertEquals(request.getUrl(), "http://foo.com/?param1=value1&param2=value2");
9999
List<Param> params = request.getQueryParams();
100100
assertEquals(params.size(), 2);
101101
assertEquals(params.get(0), new Param("param1", "value1"));
@@ -106,14 +106,14 @@ public void testParsesQueryParams() throws IOException, ExecutionException, Inte
106106
public void testUserProvidedRequestMethod() {
107107
Request req = new RequestBuilder("ABC").setUrl("http://foo.com").build();
108108
assertEquals(req.getMethod(), "ABC");
109-
assertEquals(req.getURI().toUrl(), "http://foo.com/");
109+
assertEquals(req.getUrl(), "http://foo.com/");
110110
}
111111

112112
@Test(groups = {"standalone", "default_provider"})
113113
public void testPercentageEncodedUserInfo() {
114114
final Request req = new RequestBuilder("GET").setUrl("http://hello:wor%[email protected]").build();
115115
assertEquals(req.getMethod(), "GET");
116-
assertEquals(req.getURI().toUrl(), "http://hello:wor%[email protected]");
116+
assertEquals(req.getUrl(), "http://hello:wor%[email protected]");
117117
}
118118

119119
@Test(groups = {"standalone", "default_provider"})
@@ -130,15 +130,15 @@ public void testAddQueryParameter() throws UnsupportedEncodingException {
130130
.addQueryParam("a", "1?&")
131131
.addQueryParam("b", "+ =");
132132
Request request = rb.build();
133-
assertEquals(request.getURI().toUrl(), "http://example.com/path?a=1%3F%26&b=%2B%20%3D");
133+
assertEquals(request.getUrl(), "http://example.com/path?a=1%3F%26&b=%2B%20%3D");
134134
}
135-
135+
136136
@Test(groups = {"standalone", "default_provider"})
137137
public void testRawUrlQuery() throws UnsupportedEncodingException, URISyntaxException {
138138
String preEncodedUrl = "http://example.com/space%20mirror.php?%3Bteile";
139139
RequestBuilder rb = new RequestBuilder("GET", true).setUrl(preEncodedUrl);
140140
Request request = rb.build();
141-
assertEquals(request.getURI().toUrl(), preEncodedUrl);
141+
assertEquals(request.getUrl(), preEncodedUrl);
142142
assertEquals(request.getURI().toURI().toString(), preEncodedUrl);
143143
}
144144
}

0 commit comments

Comments
 (0)