1
1
package com .truelayer .java .http ;
2
2
3
+ import static com .truelayer .java .Constants .HeaderNames .PROXY_AUTHORIZATION ;
3
4
import static com .truelayer .java .TestUtils .getClientCredentials ;
4
5
import static org .junit .jupiter .api .Assertions .*;
5
6
import static org .mockito .Mockito .mock ;
6
7
import static org .mockito .Mockito .when ;
7
8
8
9
import com .truelayer .java .ConnectionPoolOptions ;
10
+ import com .truelayer .java .ProxyConfiguration ;
9
11
import com .truelayer .java .TestUtils ;
10
12
import com .truelayer .java .auth .AuthenticationHandler ;
11
13
import com .truelayer .java .auth .IAuthenticationHandler ;
17
19
import com .truelayer .java .http .interceptors .logging .HttpLoggingInterceptor ;
18
20
import com .truelayer .java .versioninfo .LibraryInfoLoader ;
19
21
import com .truelayer .java .versioninfo .VersionInfo ;
22
+ import java .net .Proxy ;
20
23
import java .net .URI ;
21
24
import java .time .Clock ;
22
25
import java .time .Duration ;
23
26
import java .util .concurrent .ExecutorService ;
24
27
import java .util .concurrent .Executors ;
25
28
import java .util .function .Consumer ;
26
- import okhttp3 .OkHttpClient ;
29
+ import lombok .SneakyThrows ;
30
+ import okhttp3 .*;
27
31
import org .junit .jupiter .api .DisplayName ;
28
32
import org .junit .jupiter .api .Test ;
29
33
@@ -39,7 +43,7 @@ public void shouldCreateABaseAuthApiClient() {
39
43
40
44
OkHttpClient baseApiClient = getOkHttpClientFactory ()
41
45
.buildBaseApiClient (
42
- null , ConnectionPoolOptions .builder ().build (), customExecutor , customLogMessageConsumer );
46
+ null , ConnectionPoolOptions .builder ().build (), customExecutor , customLogMessageConsumer , null );
43
47
44
48
assertNotNull (baseApiClient );
45
49
assertTrue (
@@ -78,7 +82,8 @@ public void shouldCreateABaseAuthApiClientWithCustomCallTimeout() {
78
82
customTimeout ,
79
83
ConnectionPoolOptions .builder ().build (),
80
84
customExecutor ,
81
- customLogMessageConsumer );
85
+ customLogMessageConsumer ,
86
+ null );
82
87
83
88
assertNotNull (baseApiClient );
84
89
assertEquals (0 , baseApiClient .connectTimeoutMillis (), "Unexpected connect timeout configured" );
@@ -87,6 +92,80 @@ public void shouldCreateABaseAuthApiClientWithCustomCallTimeout() {
87
92
assertEquals (customTimeout .toMillis (), baseApiClient .callTimeoutMillis (), "Unexpected call timeout configured" );
88
93
}
89
94
95
+ @ SneakyThrows
96
+ @ Test
97
+ @ DisplayName ("It should build a Base API client with custom unauthenticated proxy configuration" )
98
+ public void shouldCreateABaseAuthApiClientWithCustomProxyConfigNoAuthentication () {
99
+ ProxyConfiguration customProxyConfig =
100
+ ProxyConfiguration .builder ().hostname ("127.0.0.1" ).port (9999 ).build ();
101
+
102
+ OkHttpClient baseApiClient = getOkHttpClientFactory ()
103
+ .buildBaseApiClient (null , ConnectionPoolOptions .builder ().build (), null , null , customProxyConfig );
104
+
105
+ assertNotNull (baseApiClient );
106
+ Proxy configuredProxy = baseApiClient .proxy ();
107
+ assertNotNull (configuredProxy , "proxy not configured" );
108
+ assertEquals (Proxy .Type .HTTP , configuredProxy .type (), "unexpected proxy type configured" );
109
+ assertTrue (
110
+ configuredProxy
111
+ .address ()
112
+ .toString ()
113
+ .endsWith (customProxyConfig .hostname () + ":" + customProxyConfig .port ()),
114
+ "unexpected proxy address found" );
115
+ Authenticator proxyAuthenticator = baseApiClient .proxyAuthenticator ();
116
+ // make sure the authenticator is the default used by OkHttp.
117
+ assertInstanceOf (Authenticator .NONE .getClass (), proxyAuthenticator );
118
+ }
119
+
120
+ @ SneakyThrows
121
+ @ Test
122
+ @ DisplayName ("It should build a Base API client with custom proxy configuration with authentication" )
123
+ public void shouldCreateABaseAuthApiClientWithCustomProxyConfigWithAuthentication () {
124
+ ProxyConfiguration customProxyConfig = ProxyConfiguration .builder ()
125
+ .hostname ("127.0.0.1" )
126
+ .port (9999 )
127
+ .credentials (ProxyConfiguration .Credentials .builder ()
128
+ .username ("john" )
129
+ .password ("doe" )
130
+ .build ())
131
+ .build ();
132
+
133
+ OkHttpClient baseApiClient = getOkHttpClientFactory ()
134
+ .buildBaseApiClient (null , ConnectionPoolOptions .builder ().build (), null , null , customProxyConfig );
135
+
136
+ assertNotNull (baseApiClient );
137
+ Proxy configuredProxy = baseApiClient .proxy ();
138
+ assertNotNull (configuredProxy , "proxy not configured" );
139
+ assertEquals (Proxy .Type .HTTP , configuredProxy .type (), "unexpected proxy type configured" );
140
+ assertTrue (
141
+ configuredProxy
142
+ .address ()
143
+ .toString ()
144
+ .endsWith (customProxyConfig .hostname () + ":" + customProxyConfig .port ()),
145
+ "unexpected proxy address found" );
146
+ Authenticator proxyAuthenticator = baseApiClient .proxyAuthenticator ();
147
+ assertNotNull (proxyAuthenticator , "proxy authenticator not configured" );
148
+ Request testRequest = proxyAuthenticator .authenticate (
149
+ null ,
150
+ new Response .Builder ()
151
+ .protocol (Protocol .HTTP_2 )
152
+ .code (200 )
153
+ .message ("A response" )
154
+ .request (new Request .Builder ()
155
+ .url ("https://localhost/test" )
156
+ .get ()
157
+ .build ())
158
+ .build ());
159
+ assertNotNull (testRequest , "invalid request generated" );
160
+ String expectedProxyAuthHeader = Credentials .basic (
161
+ customProxyConfig .credentials ().username (),
162
+ customProxyConfig .credentials ().password ());
163
+ assertEquals (
164
+ expectedProxyAuthHeader ,
165
+ testRequest .header (PROXY_AUTHORIZATION ),
166
+ "unexpected Proxy-Authentication header found" );
167
+ }
168
+
90
169
@ Test
91
170
@ DisplayName ("It should build an Auth API client" )
92
171
public void shouldCreateAnAuthApiClient () {
@@ -99,7 +178,8 @@ public void shouldCreateAnAuthApiClient() {
99
178
customTimeout ,
100
179
ConnectionPoolOptions .builder ().build (),
101
180
customExecutor ,
102
- customLogMessageConsumer );
181
+ customLogMessageConsumer ,
182
+ null );
103
183
104
184
OkHttpClient authClient = getOkHttpClientFactory ().buildAuthApiClient (baseApiClient , getClientCredentials ());
105
185
@@ -121,7 +201,7 @@ public void shouldCreateAnAuthApiClient() {
121
201
@ Test
122
202
@ DisplayName ("It should build a Payments API client" )
123
203
public void shouldThrowCredentialsMissingException () {
124
- OkHttpClient baseHttpClient = getOkHttpClientFactory ().buildBaseApiClient (null , null , null , null );
204
+ OkHttpClient baseHttpClient = getOkHttpClientFactory ().buildBaseApiClient (null , null , null , null , null );
125
205
OkHttpClient authClient = getOkHttpClientFactory ().buildAuthApiClient (baseHttpClient , getClientCredentials ());
126
206
127
207
IAuthenticationHandler authenticationHandler = AuthenticationHandler .New ()
0 commit comments