Skip to content

Commit 34bfeed

Browse files
author
Tomasz Lelek
committed
BAEL-12 end-to-end test case for auth proxy
1 parent 70cc228 commit 34bfeed

1 file changed

Lines changed: 23 additions & 7 deletions

File tree

httpclient/src/test/java/org/baeldung/httpclient/advancedconfig/HttpClientAdvancedConfiguration.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@
77
import org.apache.http.HttpResponse;
88
import org.apache.http.auth.AuthScope;
99
import org.apache.http.auth.UsernamePasswordCredentials;
10+
import org.apache.http.client.AuthCache;
1011
import org.apache.http.client.CredentialsProvider;
1112
import org.apache.http.client.HttpClient;
1213
import org.apache.http.client.methods.HttpGet;
1314
import org.apache.http.client.methods.HttpPost;
15+
import org.apache.http.client.protocol.HttpClientContext;
1416
import org.apache.http.entity.StringEntity;
17+
import org.apache.http.impl.auth.BasicScheme;
18+
import org.apache.http.impl.client.BasicAuthCache;
1519
import org.apache.http.impl.client.BasicCredentialsProvider;
1620
import org.apache.http.impl.client.HttpClients;
1721
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
@@ -103,34 +107,46 @@ public void givenServerThatIsBehindProxy_whenClientIsConfiguredToSendRequestViaP
103107
@Test
104108
public void givenServerThatIsBehindAuthorizationProxy_whenClientSendRequest_shouldAuthorizeProperly() throws IOException {
105109
//given
106-
proxyMock.stubFor(get(urlMatching(".*"))
110+
proxyMock.stubFor(get(urlMatching("/private"))
107111
.willReturn(aResponse().proxiedFrom("http://localhost:8089/")));
108-
109-
serviceMock.stubFor(get(urlEqualTo("/private/username_admin/secret_password"))
112+
serviceMock.stubFor(get(urlEqualTo("/private"))
110113
.willReturn(aResponse().withStatus(200)));
111114

112115

113116
HttpHost proxy = new HttpHost("localhost", 8090);
114117
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
115118

119+
// Client credentials
116120
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
117121
credentialsProvider.setCredentials(new AuthScope(proxy),
118122
new UsernamePasswordCredentials("username_admin", "secret_password"));
119123

124+
125+
// Create AuthCache instance
126+
AuthCache authCache = new BasicAuthCache();
127+
128+
// Generate BASIC scheme object and add it to the local auth cache
129+
BasicScheme basicAuth = new BasicScheme();
130+
authCache.put(proxy, basicAuth);
131+
HttpClientContext context = HttpClientContext.create();
132+
context.setCredentialsProvider(credentialsProvider);
133+
context.setAuthCache(authCache);
134+
135+
120136
HttpClient httpclient = HttpClients.custom()
121137
.setRoutePlanner(routePlanner)
122138
.setDefaultCredentialsProvider(credentialsProvider)
123139
.build();
124140

125141

126142
//when
127-
final HttpGet httpGet = new HttpGet("http://localhost:8089/private/username_admin/secret_password");
128-
HttpResponse response = httpclient.execute(httpGet);
143+
final HttpGet httpGet = new HttpGet("http://localhost:8089/private");
144+
HttpResponse response = httpclient.execute(httpGet, context);
129145

130146
//then
131147
assertEquals(response.getStatusLine().getStatusCode(), 200);
132-
proxyMock.verify(getRequestedFor(urlEqualTo("/private/username_admin/secret_password")));
133-
serviceMock.verify(getRequestedFor(urlEqualTo("/private/username_admin/secret_password")));
148+
proxyMock.verify(getRequestedFor(urlEqualTo("/private")).withHeader("Authorization", containing("Basic")));
149+
serviceMock.verify(getRequestedFor(urlEqualTo("/private")));
134150
}
135151

136152

0 commit comments

Comments
 (0)