TestURIUtils.java (httpcomponents-client-4.5.7-src) | : | TestURIUtils.java (httpcomponents-client-4.5.8-src) | ||
---|---|---|---|---|
skipping to change at line 37 | skipping to change at line 37 | |||
package org.apache.http.client.utils; | package org.apache.http.client.utils; | |||
import java.net.URI; | import java.net.URI; | |||
import java.util.Arrays; | import java.util.Arrays; | |||
import org.apache.http.HttpHost; | import org.apache.http.HttpHost; | |||
import org.apache.http.conn.routing.HttpRoute; | import org.apache.http.conn.routing.HttpRoute; | |||
import org.junit.Assert; | import org.junit.Assert; | |||
import org.junit.Test; | import org.junit.Test; | |||
import static org.apache.http.client.utils.URIUtils.DROP_FRAGMENT_AND_NORMALIZE; | ||||
import static org.apache.http.client.utils.URIUtils.NORMALIZE; | ||||
/** | /** | |||
* This TestCase contains test methods for URI resolving according to RFC 3986. | * This TestCase contains test methods for URI resolving according to RFC 3986. | |||
* The examples are listed in section "5.4 Reference Resolution Examples" | * The examples are listed in section "5.4 Reference Resolution Examples" | |||
*/ | */ | |||
public class TestURIUtils { | public class TestURIUtils { | |||
private final URI baseURI = URI.create("http://a/b/c/d;p?q"); | private final URI baseURI = URI.create("http://a/b/c/d;p?q"); | |||
@Test | @Test | |||
public void testRewrite() throws Exception { | public void testRewrite() throws Exception { | |||
final HttpHost target = new HttpHost("thathost", -1); | final HttpHost target = new HttpHost("thathost", -1); | |||
Assert.assertEquals("http://thathost/stuff", URIUtils.rewriteURI( | Assert.assertEquals("http://thathost/stuff", URIUtils.rewriteURI( | |||
URI.create("http://thishost/stuff"), target).toString()); | URI.create("http://thishost/stuff"), target).toString()); | |||
Assert.assertEquals("/stuff", URIUtils.rewriteURI( | Assert.assertEquals("/stuff", URIUtils.rewriteURI( | |||
URI.create("http://thishost/stuff"), null).toString()); | URI.create("http://thishost/stuff"), null).toString()); | |||
Assert.assertEquals("/", URIUtils.rewriteURI( | Assert.assertEquals("/", URIUtils.rewriteURI( | |||
URI.create("http://thishost//"), null).toString()); | URI.create("http://thishost//"), null).toString()); | |||
Assert.assertEquals("/stuff/morestuff", URIUtils.rewriteURI( | Assert.assertEquals("/stuff/morestuff", URIUtils.rewriteURI( | |||
URI.create("http://thishost//stuff/morestuff"), null).toString() ); | URI.create("http://thishost//stuff///morestuff"), null).toString ()); | |||
Assert.assertEquals("http://thathost/stuff", URIUtils.rewriteURI( | Assert.assertEquals("http://thathost/stuff", URIUtils.rewriteURI( | |||
URI.create("http://thishost/stuff#crap"), target, true).toString ()); | URI.create("http://thishost/stuff#crap"), target, DROP_FRAGMENT_ AND_NORMALIZE).toString()); | |||
Assert.assertEquals("http://thathost/stuff#crap", URIUtils.rewriteURI( | Assert.assertEquals("http://thathost/stuff#crap", URIUtils.rewriteURI( | |||
URI.create("http://thishost/stuff#crap"), target, false).toStrin g()); | URI.create("http://thishost/stuff#crap"), target, NORMALIZE).toS tring()); | |||
Assert.assertEquals("http://thathost/", URIUtils.rewriteURI( | Assert.assertEquals("http://thathost/", URIUtils.rewriteURI( | |||
URI.create("http://thishost#crap"), target, true).toString()); | URI.create("http://thishost#crap"), target, DROP_FRAGMENT_AND_NO RMALIZE).toString()); | |||
Assert.assertEquals("http://thathost/#crap", URIUtils.rewriteURI( | Assert.assertEquals("http://thathost/#crap", URIUtils.rewriteURI( | |||
URI.create("http://thishost#crap"), target, false).toString()); | URI.create("http://thishost#crap"), target, NORMALIZE).toString( )); | |||
Assert.assertEquals("/stuff/", URIUtils.rewriteURI( | Assert.assertEquals("/stuff/", URIUtils.rewriteURI( | |||
URI.create("http://thishost//////////////stuff/"), null).toStrin g()); | URI.create("http://thishost//////////////stuff/"), null).toStrin g()); | |||
Assert.assertEquals("http://thathost/stuff", URIUtils.rewriteURI( | Assert.assertEquals("http://thathost/stuff", URIUtils.rewriteURI( | |||
URI.create("http://thathost/stuff")).toString()); | URI.create("http://thathost/stuff")).toString()); | |||
Assert.assertEquals("http://thathost/stuff", URIUtils.rewriteURI( | Assert.assertEquals("http://thathost/stuff", URIUtils.rewriteURI( | |||
URI.create("http://thathost/stuff#fragment")).toString()); | URI.create("http://thathost/stuff#fragment")).toString()); | |||
Assert.assertEquals("http://thathost/stuff", URIUtils.rewriteURI( | Assert.assertEquals("http://thathost/stuff", URIUtils.rewriteURI( | |||
URI.create("http://userinfo@thathost/stuff#fragment")).toString( )); | URI.create("http://userinfo@thathost/stuff#fragment")).toString( )); | |||
Assert.assertEquals("http://thathost/", URIUtils.rewriteURI( | Assert.assertEquals("http://thathost/", URIUtils.rewriteURI( | |||
URI.create("http://thathost")).toString()); | URI.create("http://thathost")).toString()); | |||
skipping to change at line 87 | skipping to change at line 90 | |||
URI.create("http://That_Host")).toString()); | URI.create("http://That_Host")).toString()); | |||
Assert.assertEquals("http://thishost/Fragment_identifier%23Examples", | Assert.assertEquals("http://thishost/Fragment_identifier%23Examples", | |||
URIUtils.rewriteURI( | URIUtils.rewriteURI( | |||
URI.create("http://thishost/Fragment_identifier%23Exampl es")).toString()); | URI.create("http://thishost/Fragment_identifier%23Exampl es")).toString()); | |||
} | } | |||
@Test | @Test | |||
public void testRewritePort() throws Exception { | public void testRewritePort() throws Exception { | |||
HttpHost target = new HttpHost("thathost", 8080); // port should be copi ed | HttpHost target = new HttpHost("thathost", 8080); // port should be copi ed | |||
Assert.assertEquals("http://thathost:8080/stuff", URIUtils.rewriteURI( | Assert.assertEquals("http://thathost:8080/stuff", URIUtils.rewriteURI( | |||
URI.create("http://thishost:80/stuff#crap"), target, true).toStr ing()); | URI.create("http://thishost:80/stuff#crap"), target, DROP_FRAGME NT_AND_NORMALIZE).toString()); | |||
Assert.assertEquals("http://thathost:8080/stuff#crap", URIUtils.rewriteU RI( | Assert.assertEquals("http://thathost:8080/stuff#crap", URIUtils.rewriteU RI( | |||
URI.create("http://thishost:80/stuff#crap"), target, false).toSt ring()); | URI.create("http://thishost:80/stuff#crap"), target, NORMALIZE). toString()); | |||
target = new HttpHost("thathost", -1); // input port should be dropped | target = new HttpHost("thathost", -1); // input port should be dropped | |||
Assert.assertEquals("http://thathost/stuff", URIUtils.rewriteURI( | Assert.assertEquals("http://thathost/stuff", URIUtils.rewriteURI( | |||
URI.create("http://thishost:80/stuff#crap"), target, true).toStr ing()); | URI.create("http://thishost:80/stuff#crap"), target, DROP_FRAGME NT_AND_NORMALIZE).toString()); | |||
Assert.assertEquals("http://thathost/stuff#crap", URIUtils.rewriteURI( | Assert.assertEquals("http://thathost/stuff#crap", URIUtils.rewriteURI( | |||
URI.create("http://thishost:80/stuff#crap"), target, false).toSt ring()); | URI.create("http://thishost:80/stuff#crap"), target, NORMALIZE). toString()); | |||
} | } | |||
@Test | @Test | |||
public void testRewriteScheme() throws Exception { | public void testRewriteScheme() throws Exception { | |||
final HttpHost target = new HttpHost("thathost", -1, "file"); // scheme should be copied | final HttpHost target = new HttpHost("thathost", -1, "file"); // scheme should be copied | |||
Assert.assertEquals("file://thathost/stuff", URIUtils.rewriteURI( | Assert.assertEquals("file://thathost/stuff", URIUtils.rewriteURI( | |||
URI.create("http://thishost:80/stuff#crap"), target, true).toStr ing()); | URI.create("http://thishost:80/stuff#crap"), target, DROP_FRAGME NT_AND_NORMALIZE).toString()); | |||
} | } | |||
@Test | @Test | |||
public void testRewriteForRoute() throws Exception { | public void testRewriteForRoute() throws Exception { | |||
final HttpHost target1 = new HttpHost("foo", 80); | final HttpHost target1 = new HttpHost("foo", 80); | |||
final HttpHost target2 = new HttpHost("foo", 443, "https"); | final HttpHost target2 = new HttpHost("foo", 443, "https"); | |||
final HttpHost proxy = new HttpHost("bar", 8888); | final HttpHost proxy = new HttpHost("bar", 8888); | |||
// Direct route | // Direct route | |||
Assert.assertEquals(new URI("/test"), URIUtils | Assert.assertEquals(new URI("/test"), URIUtils | |||
.rewriteURIForRoute(new URI("http://foo/test"), new HttpRoute(ta rget1))); | .rewriteURIForRoute(new URI("http://foo/test"), new HttpRoute(ta rget1), true)); | |||
// Direct route | // Direct route | |||
Assert.assertEquals(new URI("/"), URIUtils | Assert.assertEquals(new URI("/"), URIUtils | |||
.rewriteURIForRoute(new URI(""), new HttpRoute(target1))); | .rewriteURIForRoute(new URI(""), new HttpRoute(target1), true)); | |||
// Via proxy | // Via proxy | |||
Assert.assertEquals(new URI("http://foo/test"), URIUtils | Assert.assertEquals(new URI("http://foo/test"), URIUtils | |||
.rewriteURIForRoute(new URI("http://foo/test"), new HttpRoute(ta rget1, proxy))); | .rewriteURIForRoute(new URI("http://foo/test"), new HttpRoute(ta rget1, proxy), true)); | |||
// Via proxy | // Via proxy | |||
Assert.assertEquals(new URI("http://foo:80/test"), URIUtils | Assert.assertEquals(new URI("http://foo:80/test"), URIUtils | |||
.rewriteURIForRoute(new URI("/test"), new HttpRoute(target1, pro xy))); | .rewriteURIForRoute(new URI("/test"), new HttpRoute(target1, pro xy), true)); | |||
// Via proxy tunnel | // Via proxy tunnel | |||
Assert.assertEquals(new URI("/test"), URIUtils | Assert.assertEquals(new URI("/test"), URIUtils | |||
.rewriteURIForRoute(new URI("https://foo:443/test"), new HttpRou te(target2, null, proxy, true))); | .rewriteURIForRoute(new URI("https://foo:443/test"), new HttpRou te(target2, null, proxy, true), true)); | |||
} | } | |||
@Test | @Test | |||
public void testNormalization() { | public void testNormalization() { | |||
Assert.assertEquals("example://a/b/c/%7Bfoo%7D", URIUtils.resolve(this.b aseURI, "eXAMPLE://a/./b/../b/%63/%7bfoo%7d").toString()); | Assert.assertEquals("example://a/b/c/%7Bfoo%7D", URIUtils.resolve(this.b aseURI, "eXAMPLE://a/./b/../b/%63/%7bfoo%7d").toString()); | |||
Assert.assertEquals("http://www.example.com/%3C", URIUtils.resolve(this. baseURI, "http://www.example.com/%3c").toString()); | Assert.assertEquals("http://www.example.com/%3C", URIUtils.resolve(this. baseURI, "http://www.example.com/%3c").toString()); | |||
Assert.assertEquals("http://www.example.com/", URIUtils.resolve(this.bas eURI, "HTTP://www.EXAMPLE.com/").toString()); | Assert.assertEquals("http://www.example.com/", URIUtils.resolve(this.bas eURI, "HTTP://www.EXAMPLE.com/").toString()); | |||
Assert.assertEquals("http://www.example.com/a/", URIUtils.resolve(this.b aseURI, "http://www.example.com/a%2f").toString()); | Assert.assertEquals("http://www.example.com/a%2F", URIUtils.resolve(this .baseURI, "http://www.example.com/a%2f").toString()); | |||
Assert.assertEquals("http://www.example.com/?q=%26", URIUtils.resolve(th is.baseURI, "http://www.example.com/?q=%26").toString()); | Assert.assertEquals("http://www.example.com/?q=%26", URIUtils.resolve(th is.baseURI, "http://www.example.com/?q=%26").toString()); | |||
Assert.assertEquals("http://www.example.com/%23?q=%26", URIUtils.resolve (this.baseURI, "http://www.example.com/%23?q=%26").toString()); | Assert.assertEquals("http://www.example.com/%23?q=%26", URIUtils.resolve (this.baseURI, "http://www.example.com/%23?q=%26").toString()); | |||
Assert.assertEquals("http://www.example.com/blah-(%20-blah-%20&%20-blah- %20)-blah/", | Assert.assertEquals("http://www.example.com/blah-(%20-blah-%20&%20-blah- %20)-blah/", | |||
URIUtils.resolve(this.baseURI, "http://www.example.com/blah-%28% 20-blah-%20%26%20-blah-%20%29-blah/").toString()); | URIUtils.resolve(this.baseURI, "http://www.example.com/blah-%28% 20-blah-%20%26%20-blah-%20%29-blah/").toString()); | |||
} | } | |||
@Test | @Test | |||
public void testResolve() { | public void testResolve() { | |||
Assert.assertEquals("g:h", URIUtils.resolve(this.baseURI, "g:h").toStrin g()); | Assert.assertEquals("g:h", URIUtils.resolve(this.baseURI, "g:h").toStrin g()); | |||
Assert.assertEquals("http://a/b/c/g", URIUtils.resolve(this.baseURI, "g" ).toString()); | Assert.assertEquals("http://a/b/c/g", URIUtils.resolve(this.baseURI, "g" ).toString()); | |||
End of changes. 17 change blocks. | ||||
16 lines changed or deleted | 19 lines changed or added |