HTTPCLIENT-1195: URI rewrite methods in URIUtils now use URIBuilder internally

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1349669 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2012-06-13 07:56:35 +00:00
parent 637f046eb1
commit 2e3efc7d27
2 changed files with 16 additions and 187 deletions

View File

@ -68,7 +68,10 @@ public class URIUtils {
* components violates RFC 2396, or if the authority
* component of the string is present but cannot be parsed
* as a server-based authority
*
* @deprecated (4.2) use {@link URIBuilder}.
*/
@Deprecated
public static URI createURI(
final String scheme,
final String host,
@ -76,7 +79,6 @@ public class URIUtils {
final String path,
final String query,
final String fragment) throws URISyntaxException {
StringBuilder buffer = new StringBuilder();
if (host != null) {
if (scheme != null) {
@ -129,23 +131,21 @@ public class URIUtils {
if (uri == null) {
throw new IllegalArgumentException("URI may not be null");
}
URIBuilder uribuilder = new URIBuilder(uri);
if (target != null) {
return URIUtils.createURI(
target.getSchemeName(),
target.getHostName(),
target.getPort(),
normalizePath(uri.getRawPath()),
uri.getRawQuery(),
dropFragment ? null : uri.getRawFragment());
uribuilder.setScheme(target.getSchemeName());
uribuilder.setHost(target.getHostName());
uribuilder.setPort(target.getPort());
} else {
return URIUtils.createURI(
null,
null,
-1,
normalizePath(uri.getRawPath()),
uri.getRawQuery(),
dropFragment ? null : uri.getRawFragment());
uribuilder.setScheme(null);
uribuilder.setHost(null);
uribuilder.setPort(-1);
}
uribuilder.setPath(normalizePath(uribuilder.getPath()));
if (dropFragment) {
uribuilder.setFragment(null);
}
return uribuilder.build();
}
private static String normalizePath(String path) {
@ -190,13 +190,7 @@ public class URIUtils {
throw new IllegalArgumentException("URI may not be null");
}
if (uri.getFragment() != null) {
return URIUtils.createURI(
uri.getScheme(),
uri.getHost(),
uri.getPort(),
uri.getRawPath(),
uri.getRawQuery(),
null);
return new URIBuilder(uri).setFragment(null).build();
} else {
return uri;
}

View File

@ -1,165 +0,0 @@
/*
* ====================================================================
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.http.client.protocol;
import java.io.IOException;
import java.net.InetSocketAddress;
import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.ProtocolVersion;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.localserver.BasicServerTestBase;
import org.apache.http.localserver.LocalTestServer;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestHandler;
import org.apache.http.util.EntityUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class TestUriEscapes extends BasicServerTestBase {
@Before
public void setUp() throws Exception {
this.localServer = new LocalTestServer(null, null);
this.localServer.registerDefaultHandlers();
this.localServer.start();
this.httpclient = new DefaultHttpClient();
}
private static class UriListeningService implements HttpRequestHandler {
private volatile String requestedUri;
public void handle(
final HttpRequest request,
final HttpResponse response,
final HttpContext context) throws HttpException, IOException {
ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
this.requestedUri = request.getRequestLine().getUri();
response.setStatusLine(ver, HttpStatus.SC_OK);
StringEntity entity = new StringEntity("Response Body");
response.setEntity(entity);
}
public String getRequestedUri() {
return requestedUri;
}
}
private void doTest(String uri, boolean relative) throws Exception {
InetSocketAddress address = this.localServer.getServiceAddress();
int port = address.getPort();
String host = address.getHostName();
UriListeningService listener = new UriListeningService();
this.localServer.register("*", listener);
HttpResponse response;
if(!relative) {
String request = "http://" + host + ":" + port + uri;
HttpGet httpget = new HttpGet(request);
response = this.httpclient.execute(httpget);
EntityUtils.consume(response.getEntity());
} else {
HttpHost target = new HttpHost(host, port);
HttpGet httpget = new HttpGet(uri);
response = this.httpclient.execute(target, httpget);
EntityUtils.consume(response.getEntity());
}
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
Assert.assertEquals(uri, listener.getRequestedUri());
}
@Test
public void testEscapedAmpersandInQueryAbsolute() throws Exception {
doTest("/path/a=b&c=%26d", false);
}
@Test
public void testEscapedAmpersandInQueryRelative() throws Exception {
doTest("/path/a=b&c=%26d", true);
}
@Test
public void testPlusInPathAbsolute() throws Exception {
doTest("/path+go", false);
}
@Test
public void testPlusInPathRelative() throws Exception {
doTest("/path+go", true);
}
@Test
public void testEscapedSpaceInPathAbsolute() throws Exception {
doTest("/path%20go?a=b&c=d", false);
}
@Test
public void testEscapedSpaceInPathRelative() throws Exception {
doTest("/path%20go?a=b&c=d", true);
}
@Test
public void testEscapedAmpersandInPathAbsolute() throws Exception {
doTest("/this%26that?a=b&c=d", false);
}
@Test
public void testEscapedAmpersandInPathRelative() throws Exception {
doTest("/this%26that?a=b&c=d", true);
}
@Test
public void testEscapedSpaceInQueryAbsolute() throws Exception {
doTest("/path?a=b&c=d%20e", false);
}
@Test
public void testEscapedSpaceInQueryRelative() throws Exception {
doTest("/path?a=b&c=d%20e", true);
}
@Test
public void testPlusInQueryAbsolute() throws Exception {
doTest("/path?a=b&c=d+e", false);
}
@Test
public void testPlusInQueryRelative() throws Exception {
doTest("/path?a=b&c=d+e", true);
}
}