Skip to content

Commit 3518c9b

Browse files
committed
refactor: use default request object instead of null
1 parent 42dd4c6 commit 3518c9b

2 files changed

Lines changed: 18 additions & 22 deletions

File tree

src/main/java/com/factset/sdk/utils/authentication/ConfidentialClient.java

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public ConfidentialClient(final Configuration config)
102102
Objects.requireNonNull(config, "Configuration object must not be null");
103103
this.config = config;
104104
LOGGER.debug("Finished initialising configuration");
105-
this.requestOptions = null;
105+
this.requestOptions = new RequestOptions.RequestOptionsBuilder().build();
106106

107107
this.requestProviderMetadata();
108108
}
@@ -192,18 +192,15 @@ private void requestProviderMetadata() throws AuthServerMetadataContentException
192192
InputStream stream;
193193

194194
try {
195-
if (this.requestOptions == null) stream = wellKnownURL.openStream();
196-
else {
197-
HttpURLConnection conn = (HttpURLConnection) wellKnownURL.openConnection(this.requestOptions.getProxy());
198-
if (conn instanceof HttpsURLConnection) {
199-
HttpsURLConnection sslConn = (HttpsURLConnection) conn;
200-
sslConn.setHostnameVerifier(this.requestOptions.getHostnameVerifier());
201-
sslConn.setSSLSocketFactory(this.requestOptions.getSslSocketFactory());
202-
}
203-
204-
stream = conn.getInputStream();
195+
HttpURLConnection conn = (HttpURLConnection) wellKnownURL.openConnection(this.requestOptions.getProxy());
196+
if (conn instanceof HttpsURLConnection) {
197+
HttpsURLConnection sslConn = (HttpsURLConnection) conn;
198+
sslConn.setHostnameVerifier(this.requestOptions.getHostnameVerifier());
199+
sslConn.setSSLSocketFactory(this.requestOptions.getSslSocketFactory());
205200
}
206201

202+
stream = conn.getInputStream();
203+
207204
final String providerInfo = IOUtils.readInputStreamToString(stream);
208205
this.providerMetadata = OIDCProviderMetadata.parse(providerInfo);
209206
} catch (final ParseException e) {
@@ -236,11 +233,10 @@ private String fetchAccessToken() throws AccessTokenException, SigningJwsExcepti
236233
final TokenRequest tokenRequest = this.tokenRequestBuilder.signedJwt(signedJwt).build();
237234

238235
final HTTPRequest httpRequest = tokenRequest.toHTTPRequest();
239-
if (requestOptions != null) {
240-
httpRequest.setProxy(this.requestOptions.getProxy());
241-
httpRequest.setHostnameVerifier(this.requestOptions.getHostnameVerifier());
242-
httpRequest.setSSLSocketFactory(this.requestOptions.getSslSocketFactory());
243-
}
236+
httpRequest.setProxy(this.requestOptions.getProxy());
237+
httpRequest.setHostnameVerifier(this.requestOptions.getHostnameVerifier());
238+
httpRequest.setSSLSocketFactory(this.requestOptions.getSslSocketFactory());
239+
244240
logTokenRequest(httpRequest);
245241

246242
final HTTPResponse res = httpRequest.send();

src/test/java/com/factset/sdk/utils/authentication/ConfidentialClientTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@
99
import org.junit.jupiter.api.Test;
1010

1111
import java.io.File;
12-
import java.io.FileInputStream;
1312
import java.io.IOException;
14-
import java.net.URI;
15-
import java.net.URISyntaxException;
16-
import java.net.URL;
13+
import java.net.*;
14+
import java.nio.file.Files;
1715
import java.nio.file.Path;
1816
import java.nio.file.Paths;
1917
import java.util.List;
@@ -327,7 +325,9 @@ private static URL getUrlMockResponse(String stringFile) throws IOException {
327325
final File file = new File(String.valueOf(Paths.get(String.valueOf(pathToResources), stringFile)));
328326

329327
URL mockedUrl = mock(URL.class);
330-
when(mockedUrl.openStream()).thenReturn(new FileInputStream(file));
328+
HttpURLConnection mockedConn = mock(HttpURLConnection.class);
329+
when(mockedUrl.openConnection(any(Proxy.class))).thenReturn(mockedConn);
330+
when(mockedConn.getInputStream()).thenReturn(Files.newInputStream(file.toPath()));
331331

332332
return mockedUrl;
333333
}
@@ -343,7 +343,7 @@ private static Configuration getConfigSpyMockedResponse(String urlResponse, Stri
343343

344344
private static Configuration getConfigSpyThrowsIOException(String configFile) throws IOException, ConfigurationException {
345345
URL mockedUrl = mock(URL.class);
346-
when(mockedUrl.openStream()).thenThrow(IOException.class);
346+
when(mockedUrl.openConnection(any(Proxy.class))).thenThrow(IOException.class);
347347
Configuration configuration = new Configuration(String.valueOf(Paths.get(pathToResources.toString(), configFile)));
348348
Configuration configurationSpy = spy(configuration);
349349
when(configurationSpy.getWellKnownUrl()).thenReturn(mockedUrl).thenCallRealMethod();

0 commit comments

Comments
 (0)