forked from apache/httpcomponents-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reworked proxy configuration; more test cases
git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1429930 13f79535-47bb-0310-9956-ffa450edef68
- Loading branch information
Showing
11 changed files
with
208 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
httpclient/src/main/java/org/apache/http/impl/conn/DefaultProxyRoutePlanner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* ==================================================================== | ||
* 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.impl.conn; | ||
|
||
import org.apache.http.HttpException; | ||
import org.apache.http.HttpHost; | ||
import org.apache.http.HttpRequest; | ||
import org.apache.http.annotation.Immutable; | ||
import org.apache.http.conn.SchemePortResolver; | ||
import org.apache.http.conn.routing.HttpRoutePlanner; | ||
import org.apache.http.protocol.HttpContext; | ||
import org.apache.http.util.Args; | ||
|
||
/** | ||
* Implementation of an {@link HttpRoutePlanner} that routes requests through | ||
* a default proxy. | ||
* | ||
* @since 4.3 | ||
*/ | ||
@Immutable | ||
public class DefaultProxyRoutePlanner extends DefaultRoutePlanner { | ||
|
||
private final HttpHost proxy; | ||
|
||
public DefaultProxyRoutePlanner(final HttpHost proxy, final SchemePortResolver schemePortResolver) { | ||
super(schemePortResolver); | ||
this.proxy = Args.notNull(proxy, "Proxy host"); | ||
} | ||
|
||
@Override | ||
protected HttpHost determineProxy( | ||
final HttpHost target, | ||
final HttpRequest request, | ||
final HttpContext context) throws HttpException { | ||
return proxy; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
httpclient/src/test/java/org/apache/http/impl/conn/TestDefaultProxyRoutePlanner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* ==================================================================== | ||
* 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.impl.conn; | ||
|
||
import org.apache.http.HttpHost; | ||
import org.apache.http.HttpRequest; | ||
import org.apache.http.HttpVersion; | ||
import org.apache.http.client.config.RequestConfig; | ||
import org.apache.http.client.protocol.HttpClientContext; | ||
import org.apache.http.conn.SchemePortResolver; | ||
import org.apache.http.conn.routing.HttpRoute; | ||
import org.apache.http.message.BasicHttpRequest; | ||
import org.apache.http.protocol.BasicHttpContext; | ||
import org.apache.http.protocol.HttpContext; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
|
||
/** | ||
* Tests for {@link DefaultProxyRoutePlanner}. | ||
*/ | ||
public class TestDefaultProxyRoutePlanner { | ||
|
||
private HttpHost defaultProxy; | ||
private SchemePortResolver schemePortResolver; | ||
private DefaultProxyRoutePlanner routePlanner; | ||
|
||
@Before | ||
public void setup() { | ||
defaultProxy = new HttpHost("default.proxy.host", 8888); | ||
schemePortResolver = Mockito.mock(SchemePortResolver.class); | ||
routePlanner = new DefaultProxyRoutePlanner(defaultProxy, | ||
schemePortResolver); | ||
} | ||
|
||
@Test | ||
public void testDefaultProxyDirect() throws Exception { | ||
HttpHost target = new HttpHost("somehost", 80, "http"); | ||
HttpRequest request = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1); | ||
|
||
HttpContext context = new BasicHttpContext(); | ||
HttpRoute route = routePlanner.determineRoute(target, request, context); | ||
|
||
Assert.assertEquals(target, route.getTargetHost()); | ||
Assert.assertEquals(defaultProxy, route.getProxyHost()); | ||
Assert.assertEquals(2, route.getHopCount()); | ||
Assert.assertFalse(route.isSecure()); | ||
} | ||
|
||
@Test | ||
public void testViaProxy() throws Exception { | ||
HttpHost target = new HttpHost("somehost", 80, "http"); | ||
HttpHost proxy = new HttpHost("custom.proxy.host", 8080); | ||
HttpRequest request = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1); | ||
|
||
HttpClientContext context = HttpClientContext.create(); | ||
context.setRequestConfig(RequestConfig.custom().setProxy(proxy).build()); | ||
HttpRoute route = routePlanner.determineRoute(target, request, context); | ||
|
||
Assert.assertEquals(target, route.getTargetHost()); | ||
Assert.assertEquals(proxy, route.getProxyHost()); | ||
Assert.assertEquals(2, route.getHopCount()); | ||
Assert.assertFalse(route.isSecure()); | ||
} | ||
|
||
} |
Oops, something went wrong.