-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
further improvements to proxied digest auth
- some unit tests still missing - also fix tests after adding route
- Loading branch information
1 parent
4c13311
commit 29ca520
Showing
5 changed files
with
262 additions
and
14 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
194 changes: 194 additions & 0 deletions
194
src/test/java/com/burgstaller/okhttp/ProxyAuthenticationManualTest.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,194 @@ | ||
package com.burgstaller.okhttp; | ||
|
||
import com.burgstaller.okhttp.basic.BasicAuthenticator; | ||
import com.burgstaller.okhttp.digest.Credentials; | ||
import com.burgstaller.okhttp.digest.DigestAuthenticator; | ||
|
||
import org.junit.Before; | ||
import org.junit.Ignore; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.net.InetSocketAddress; | ||
import java.net.Proxy; | ||
|
||
import okhttp3.Authenticator; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
import okhttp3.logging.HttpLoggingInterceptor; | ||
import okhttp3.logging.HttpLoggingInterceptor.Level; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.fail; | ||
|
||
/** | ||
* Tests Proxy authentication. This test can only be run with a properly configured proxy server. | ||
* To use this test set up a proxy server with the given credentials which can be provided via | ||
* System properties or environment variables: | ||
* <dl> | ||
* <dt>PROXY_HOST</dt><dd>The hostname of the proxy server</dd> | ||
* <dt>PROXY_PORT</dt><dd>The TCPIP port number of the proxy server</dd> | ||
* <dt>PROXY_PASSWORD</dt><dd>The proxy server password for the users 'okhttp_basic' and | ||
* 'okhttp_digest'</dd> | ||
* </dl> | ||
*/ | ||
@Ignore | ||
public class ProxyAuthenticationManualTest { | ||
|
||
private static final HttpLoggingInterceptor.Logger LOGGER = new HttpLoggingInterceptor.Logger() { | ||
@Override | ||
public void log(String message) { | ||
System.out.println(message); | ||
} | ||
}; | ||
private static final HttpLoggingInterceptor LOGGING_INTERCEPTOR = new HttpLoggingInterceptor(LOGGER); | ||
private static final String AUTH_BASIC_USERNAME = "okhttp_basic"; | ||
private static final String AUTH_DIGEST_USERNAME = "okhttp_digest"; | ||
|
||
static { | ||
LOGGING_INTERCEPTOR.setLevel(Level.HEADERS); | ||
} | ||
|
||
private Proxy proxy; | ||
private String authPass = "allCorrect@auth"; | ||
|
||
@Before | ||
public void setupProxy() { | ||
|
||
String proxyAddress = System.getenv("PROXY_HOST"); | ||
if (proxyAddress == null) { | ||
proxyAddress = System.getProperty("PROXY_HOST", "localhost"); | ||
} | ||
String proxyPortString = System.getenv("PROXY_PORT"); | ||
if (proxyPortString == null) { | ||
proxyPortString = System.getProperty("PROXY_PORT", "8080"); | ||
} | ||
authPass = System.getenv("PROXY_PASSWORD"); | ||
if (authPass == null) { | ||
authPass = System.getProperty("PROXY_PASSWORD", "password"); | ||
} | ||
int proxyPort = Integer.valueOf(proxyPortString); | ||
proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyAddress, proxyPort)); | ||
} | ||
|
||
@Test | ||
public void testConnection_WithoutProxy_Expect200() throws IOException { | ||
final OkHttpClient client = new OkHttpClient.Builder() | ||
.build(); | ||
final Request request = new Request.Builder() | ||
.url("https://www.google.com/favicon.ico") | ||
.build(); | ||
Response response = client.newCall(request).execute(); | ||
assertEquals(200, response.code()); | ||
} | ||
|
||
@Test | ||
public void testConnection_WithProxyButNoAuth_ExpectAuthException() throws IOException { | ||
final OkHttpClient client = givenHttpClientWithoutAuth(); | ||
final Request request = new Request.Builder() | ||
.url("https://www.google.com/favicon.ico") | ||
.build(); | ||
try { | ||
client.newCall(request).execute(); | ||
fail("this call should fail with an exception"); | ||
} catch (IOException e) { | ||
assertEquals("Failed to authenticate with proxy", e.getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testConnection_WithProxyBasicAuthWithoutTunnel_Expect200() throws IOException { | ||
final BasicAuthenticator authenticator = new BasicAuthenticator( | ||
new Credentials(AUTH_BASIC_USERNAME, authPass)); | ||
|
||
final OkHttpClient client = givenHttpClientWithProxyAuth(authenticator); | ||
final Request request = new Request.Builder() | ||
.url("http://edition.cnn.com") | ||
.build(); | ||
Response response = client.newCall(request).execute(); | ||
assertEquals(200, response.code()); | ||
} | ||
|
||
@Test | ||
public void testConnection_WithProxyBasicAuthWithTunnel_Expect200() throws IOException { | ||
final BasicAuthenticator authenticator = new BasicAuthenticator( | ||
new Credentials(AUTH_BASIC_USERNAME, authPass)); | ||
|
||
final OkHttpClient client = givenHttpClientWithProxyAuth(authenticator); | ||
final Request request = new Request.Builder() | ||
.url("https://www.google.com/favicon.ico") | ||
.build(); | ||
Response response = client.newCall(request).execute(); | ||
assertEquals(200, response.code()); | ||
} | ||
|
||
|
||
@Test | ||
public void testConnection_WithProxyBasicAuthWithNotAllowedSites_Expect403() throws IOException { | ||
final BasicAuthenticator authenticator = new BasicAuthenticator( | ||
new Credentials(AUTH_BASIC_USERNAME, authPass)); | ||
|
||
final OkHttpClient client = givenHttpClientWithProxyAuth(authenticator); | ||
final Request request = new Request.Builder() | ||
.url("http://www.youtube.com") | ||
.build(); | ||
Response response = client.newCall(request).execute(); | ||
assertEquals(403, response.code()); | ||
} | ||
|
||
@Test | ||
public void testConnection_WithProxyDigestAuthWithoutTunnel_Expect200() throws IOException { | ||
final DigestAuthenticator authenticator = new DigestAuthenticator( | ||
new Credentials(AUTH_DIGEST_USERNAME, authPass)); | ||
|
||
final OkHttpClient client = givenHttpClientWithProxyAuth(authenticator); | ||
final Request request = new Request.Builder() | ||
.url("http://edition.cnn.com") | ||
.build(); | ||
Response response = client.newCall(request).execute(); | ||
assertEquals(200, response.code()); | ||
} | ||
|
||
@Test | ||
public void testConnection_WithProxyDigestAuthWithTunnel_Expect200() throws IOException { | ||
final DigestAuthenticator authenticator = new DigestAuthenticator( | ||
new Credentials(AUTH_DIGEST_USERNAME, authPass)); | ||
|
||
final OkHttpClient client = givenHttpClientWithProxyAuth(authenticator); | ||
final Request request = new Request.Builder() | ||
.url("https://www.google.com/favicon.ico") | ||
.build(); | ||
Response response = client.newCall(request).execute(); | ||
assertEquals(200, response.code()); | ||
} | ||
|
||
@Test | ||
public void testConnection_WithProxyDigestAuthWithNotAllowdSites_Expect403() throws IOException { | ||
final DigestAuthenticator authenticator = new DigestAuthenticator( | ||
new Credentials(AUTH_DIGEST_USERNAME, authPass)); | ||
|
||
final OkHttpClient client = givenHttpClientWithProxyAuth(authenticator); | ||
final Request request = new Request.Builder() | ||
.url("http://www.youtube.com") | ||
.build(); | ||
Response response = client.newCall(request).execute(); | ||
assertEquals(403, response.code()); | ||
} | ||
|
||
|
||
private OkHttpClient givenHttpClientWithProxyAuth(Authenticator authenticator) { | ||
return new OkHttpClient.Builder() | ||
.proxy(proxy) | ||
.proxyAuthenticator(authenticator) | ||
.addNetworkInterceptor(LOGGING_INTERCEPTOR) | ||
.build(); | ||
} | ||
|
||
private OkHttpClient givenHttpClientWithoutAuth() { | ||
return new OkHttpClient.Builder() | ||
.proxy(proxy) | ||
.addNetworkInterceptor(LOGGING_INTERCEPTOR) | ||
.build(); | ||
} | ||
} |
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