diff --git a/tinyurl/src/main/java/com/tinyurl/service/UrlReachabilityCheckerImpl.java b/tinyurl/src/main/java/com/tinyurl/service/UrlReachabilityCheckerImpl.java index ef4e755..3d2611e 100644 --- a/tinyurl/src/main/java/com/tinyurl/service/UrlReachabilityCheckerImpl.java +++ b/tinyurl/src/main/java/com/tinyurl/service/UrlReachabilityCheckerImpl.java @@ -10,6 +10,7 @@ import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.net.http.HttpTimeoutException; +import javax.net.ssl.SSLException; import java.time.Duration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -47,7 +48,7 @@ public void check(String url) { } } catch (UrlUnreachableException e) { throw e; - } catch (UnknownHostException | ConnectException | HttpTimeoutException e) { + } catch (UnknownHostException | ConnectException | HttpTimeoutException | SSLException e) { throw new UrlUnreachableException("URL_UNREACHABLE"); } catch (Exception e) { log.warn("Reachability check failed for host {} — failing open: {}", URI.create(url).getHost(), e.getMessage()); diff --git a/tinyurl/src/test/java/com/tinyurl/service/UrlReachabilityCheckerImplTest.java b/tinyurl/src/test/java/com/tinyurl/service/UrlReachabilityCheckerImplTest.java index e8c5b65..bafeba9 100644 --- a/tinyurl/src/test/java/com/tinyurl/service/UrlReachabilityCheckerImplTest.java +++ b/tinyurl/src/test/java/com/tinyurl/service/UrlReachabilityCheckerImplTest.java @@ -13,6 +13,7 @@ import java.net.http.HttpHeaders; import java.net.http.HttpResponse; import java.net.http.HttpTimeoutException; +import javax.net.ssl.SSLException; import java.util.List; import java.util.Map; import org.junit.jupiter.api.BeforeEach; @@ -105,6 +106,12 @@ void shouldRejectOnConnectionRefused() throws Exception { assertThrows(UrlUnreachableException.class, () -> checker.check("https://example.com")); } + @Test + void shouldRejectOnSslError() throws Exception { + doThrow(new SSLException("certificate unknown")).when(httpClient).send(any(), any()); + assertThrows(UrlUnreachableException.class, () -> checker.check("https://example.com")); + } + @Test void shouldFollowRedirectAndAcceptOn200() throws Exception {