BAEL-12 end-to-end test case for auth proxy
This commit is contained in:
parent
70cc228f1e
commit
34bfeed011
|
@ -7,11 +7,15 @@ import org.apache.http.HttpHost;
|
||||||
import org.apache.http.HttpResponse;
|
import org.apache.http.HttpResponse;
|
||||||
import org.apache.http.auth.AuthScope;
|
import org.apache.http.auth.AuthScope;
|
||||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||||
|
import org.apache.http.client.AuthCache;
|
||||||
import org.apache.http.client.CredentialsProvider;
|
import org.apache.http.client.CredentialsProvider;
|
||||||
import org.apache.http.client.HttpClient;
|
import org.apache.http.client.HttpClient;
|
||||||
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.client.methods.HttpGet;
|
||||||
import org.apache.http.client.methods.HttpPost;
|
import org.apache.http.client.methods.HttpPost;
|
||||||
|
import org.apache.http.client.protocol.HttpClientContext;
|
||||||
import org.apache.http.entity.StringEntity;
|
import org.apache.http.entity.StringEntity;
|
||||||
|
import org.apache.http.impl.auth.BasicScheme;
|
||||||
|
import org.apache.http.impl.client.BasicAuthCache;
|
||||||
import org.apache.http.impl.client.BasicCredentialsProvider;
|
import org.apache.http.impl.client.BasicCredentialsProvider;
|
||||||
import org.apache.http.impl.client.HttpClients;
|
import org.apache.http.impl.client.HttpClients;
|
||||||
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
|
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
|
||||||
|
@ -103,20 +107,32 @@ public class HttpClientAdvancedConfiguration {
|
||||||
@Test
|
@Test
|
||||||
public void givenServerThatIsBehindAuthorizationProxy_whenClientSendRequest_shouldAuthorizeProperly() throws IOException {
|
public void givenServerThatIsBehindAuthorizationProxy_whenClientSendRequest_shouldAuthorizeProperly() throws IOException {
|
||||||
//given
|
//given
|
||||||
proxyMock.stubFor(get(urlMatching(".*"))
|
proxyMock.stubFor(get(urlMatching("/private"))
|
||||||
.willReturn(aResponse().proxiedFrom("http://localhost:8089/")));
|
.willReturn(aResponse().proxiedFrom("http://localhost:8089/")));
|
||||||
|
serviceMock.stubFor(get(urlEqualTo("/private"))
|
||||||
serviceMock.stubFor(get(urlEqualTo("/private/username_admin/secret_password"))
|
|
||||||
.willReturn(aResponse().withStatus(200)));
|
.willReturn(aResponse().withStatus(200)));
|
||||||
|
|
||||||
|
|
||||||
HttpHost proxy = new HttpHost("localhost", 8090);
|
HttpHost proxy = new HttpHost("localhost", 8090);
|
||||||
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
|
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
|
||||||
|
|
||||||
|
// Client credentials
|
||||||
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
|
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
|
||||||
credentialsProvider.setCredentials(new AuthScope(proxy),
|
credentialsProvider.setCredentials(new AuthScope(proxy),
|
||||||
new UsernamePasswordCredentials("username_admin", "secret_password"));
|
new UsernamePasswordCredentials("username_admin", "secret_password"));
|
||||||
|
|
||||||
|
|
||||||
|
// Create AuthCache instance
|
||||||
|
AuthCache authCache = new BasicAuthCache();
|
||||||
|
|
||||||
|
// Generate BASIC scheme object and add it to the local auth cache
|
||||||
|
BasicScheme basicAuth = new BasicScheme();
|
||||||
|
authCache.put(proxy, basicAuth);
|
||||||
|
HttpClientContext context = HttpClientContext.create();
|
||||||
|
context.setCredentialsProvider(credentialsProvider);
|
||||||
|
context.setAuthCache(authCache);
|
||||||
|
|
||||||
|
|
||||||
HttpClient httpclient = HttpClients.custom()
|
HttpClient httpclient = HttpClients.custom()
|
||||||
.setRoutePlanner(routePlanner)
|
.setRoutePlanner(routePlanner)
|
||||||
.setDefaultCredentialsProvider(credentialsProvider)
|
.setDefaultCredentialsProvider(credentialsProvider)
|
||||||
|
@ -124,13 +140,13 @@ public class HttpClientAdvancedConfiguration {
|
||||||
|
|
||||||
|
|
||||||
//when
|
//when
|
||||||
final HttpGet httpGet = new HttpGet("http://localhost:8089/private/username_admin/secret_password");
|
final HttpGet httpGet = new HttpGet("http://localhost:8089/private");
|
||||||
HttpResponse response = httpclient.execute(httpGet);
|
HttpResponse response = httpclient.execute(httpGet, context);
|
||||||
|
|
||||||
//then
|
//then
|
||||||
assertEquals(response.getStatusLine().getStatusCode(), 200);
|
assertEquals(response.getStatusLine().getStatusCode(), 200);
|
||||||
proxyMock.verify(getRequestedFor(urlEqualTo("/private/username_admin/secret_password")));
|
proxyMock.verify(getRequestedFor(urlEqualTo("/private")).withHeader("Authorization", containing("Basic")));
|
||||||
serviceMock.verify(getRequestedFor(urlEqualTo("/private/username_admin/secret_password")));
|
serviceMock.verify(getRequestedFor(urlEqualTo("/private")));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue