Skip to content

Commit

Permalink
adding new method for creating HttpsUrlConnection in
Browse files Browse the repository at this point in the history
WalledGardenInternetObservingStrategy - solves #323
  • Loading branch information
pwittchen committed Aug 19, 2019
1 parent 81e0ac2 commit a033282
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
Expand Up @@ -30,6 +30,7 @@
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.HttpsURLConnection;

/**
* Walled Garden Strategy for monitoring connectivity with the Internet.
Expand Down Expand Up @@ -100,7 +101,11 @@ protected Boolean isConnected(final String host, final int port, final int timeo
final int httpResponse, final ErrorHandler errorHandler) {
HttpURLConnection urlConnection = null;
try {
urlConnection = createHttpUrlConnection(host, port, timeoutInMs);
if (host.startsWith(HTTPS_PROTOCOL)) {
urlConnection = createHttpsUrlConnection(host, port, timeoutInMs);
} else {
urlConnection = createHttpUrlConnection(host, port, timeoutInMs);
}
return urlConnection.getResponseCode() == httpResponse;
} catch (IOException e) {
errorHandler.handleError(e, "Could not establish connection with WalledGardenStrategy");
Expand All @@ -123,4 +128,16 @@ protected HttpURLConnection createHttpUrlConnection(final String host, final int
urlConnection.setUseCaches(false);
return urlConnection;
}

protected HttpsURLConnection createHttpsUrlConnection(final String host, final int port,
final int timeoutInMs) throws IOException {
URL initialUrl = new URL(host);
URL url = new URL(initialUrl.getProtocol(), initialUrl.getHost(), port, initialUrl.getFile());
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setConnectTimeout(timeoutInMs);
urlConnection.setReadTimeout(timeoutInMs);
urlConnection.setInstanceFollowRedirects(false);
urlConnection.setUseCaches(false);
return urlConnection;
}
}
Expand Up @@ -43,6 +43,7 @@
private static final int PORT = 80;
private static final int TIMEOUT_IN_MS = 30;
private static final int HTTP_RESPONSE = 204;
private static final String HOST_WITH_HTTP = "http://www.website.com";
private static final String HOST_WITH_HTTPS = "https://www.website.com";
private static final String HOST_WITHOUT_HTTPS = "www.website.com";

Expand Down Expand Up @@ -139,11 +140,43 @@ private String getHost() {
assertThat(connection.getUseCaches()).isFalse();
}

@Test public void shouldHandleAnExceptionWhileCreatingUrlConnection() throws IOException {
@Test public void shouldHandleAnExceptionWhileCreatingHttpUrlConnection() throws IOException {
// given
final String errorMsg = "Could not establish connection with WalledGardenStrategy";
final IOException givenException = new IOException(errorMsg);
when(strategy.createHttpUrlConnection(getHost(), PORT, TIMEOUT_IN_MS)).thenThrow(
when(strategy.createHttpUrlConnection(HOST_WITH_HTTP, PORT, TIMEOUT_IN_MS)).thenThrow(
givenException);

// when
strategy.isConnected(HOST_WITH_HTTP, PORT, TIMEOUT_IN_MS, HTTP_RESPONSE, errorHandler);

// then
verify(errorHandler).handleError(givenException, errorMsg);
}

@Test public void shouldCreateHttpsUrlConnection() throws IOException {
// given
final String parsedDefaultHost = "clients3.google.com";

// when
HttpURLConnection connection =
strategy.createHttpsUrlConnection(getHost(), PORT, TIMEOUT_IN_MS);

// then
assertThat(connection).isNotNull();
assertThat(connection.getURL().getHost()).isEqualTo(parsedDefaultHost);
assertThat(connection.getURL().getPort()).isEqualTo(PORT);
assertThat(connection.getConnectTimeout()).isEqualTo(TIMEOUT_IN_MS);
assertThat(connection.getReadTimeout()).isEqualTo(TIMEOUT_IN_MS);
assertThat(connection.getInstanceFollowRedirects()).isFalse();
assertThat(connection.getUseCaches()).isFalse();
}

@Test public void shouldHandleAnExceptionWhileCreatingHttpsUrlConnection() throws IOException {
// given
final String errorMsg = "Could not establish connection with WalledGardenStrategy";
final IOException givenException = new IOException(errorMsg);
when(strategy.createHttpsUrlConnection(getHost(), PORT, TIMEOUT_IN_MS)).thenThrow(
givenException);

// when
Expand Down

0 comments on commit a033282

Please sign in to comment.