NetworkBlogUrlValidator implements BlogUrlValidator { @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) static Single<String> checkGhostBlog(@NonNull String blogUrl, @NonNull OkHttpClient client) { final String adminPagePath = "/ghost/"; String adminPageUrl = NetworkUtils.makeAbsoluteUrl(blogUrl, adminPagePath); return checkUrl(adminPageUrl, client).flatMap(response -> { if (response.isSuccessful()) { String potentiallyRedirectedUrl = response.request().url().toString(); String finalBlogUrl = potentiallyRedirectedUrl.replaceFirst(adminPagePath + "?$", ""); return Single.just(finalBlogUrl); } else if (response.code() == HttpURLConnection.HTTP_NOT_FOUND) { return Single.error(new UrlNotFoundException(blogUrl)); } else { return Single.error(new RuntimeException("Response code " + response.code() + " on requesting admin page at " + adminPageUrl)); } }); } NetworkBlogUrlValidator(@NonNull OkHttpClient httpClient); @Override Observable<String> validate(@NonNull String blogUrl);  }
@Test public void checkGhostBlog_simpleHttps() throws IOException, NoSuchAlgorithmException { String blogUrl = HTTPS + server.getHostName() + ":" + server.getPort(); server.useHttps(Helpers.LOCALHOST_SOCKET_FACTORY, false); server.enqueue(new MockResponse()); OkHttpClient httpClient = Helpers.getProdHttpClient(); assertThat(checkGhostBlog(blogUrl, httpClient), is(blogUrl)); }
@Test public void checkGhostBlog_simpleHttp() throws IOException, NoSuchAlgorithmException { String blogUrl = HTTP + server.getHostName() + ":" + server.getPort(); server.enqueue(new MockResponse()); OkHttpClient httpClient = Helpers.getProdHttpClient(); assertThat(checkGhostBlog(blogUrl, httpClient), is(blogUrl)); }
@Test public void checkGhostBlog_404() throws IOException { String blogUrl = HTTPS + server.getHostName() + ":" + server.getPort() + "/THIS_DOESNT_EXIST"; server.useHttps(Helpers.LOCALHOST_SOCKET_FACTORY, false); server.enqueue(new MockResponse().setResponseCode(404)); OkHttpClient httpClient = Helpers.getProdHttpClient(); try { checkGhostBlog(blogUrl, httpClient); fail("Test did not throw exception as expected!"); } catch (Exception e) { assertThat(e, instanceOf(UrlNotFoundException.class)); } }
@Test public void checkGhostBlog_trailingSlash() throws IOException { String blogUrl = HTTPS + server.getHostName() + ":" + server.getPort() + "/"; server.useHttps(Helpers.LOCALHOST_SOCKET_FACTORY, false); server.enqueue(new MockResponse()); OkHttpClient httpClient = Helpers.getProdHttpClient(); assertThat(checkGhostBlog(blogUrl, httpClient), isOneOf(blogUrl, blogUrl.replaceFirst("/$", ""))); }
@Test public void checkGhostBlog_httpToHttpsRedirect() throws IOException, NoSuchAlgorithmException { Func1<HttpUrl, HttpUrl> toHttps = (httpUrl) -> httpUrl.newBuilder().scheme("https").build(); String httpUrl = HTTP + server.getHostName() + ":" + server.getPort(); String httpsUrl = toHttps.call(HttpUrl.parse(httpUrl)).toString(); server.useHttps(Helpers.LOCALHOST_SOCKET_FACTORY, false); server.enqueue(new MockResponse()); OkHttpClient httpClient = Helpers.getProdHttpClient(); httpClient = httpClient.newBuilder().addInterceptor(chain -> { if (chain.request().isHttps()) { throw new IllegalStateException("This test is supposed to make a vanilla HTTP request!"); } String httpsRequestUrl = toHttps.call(chain.request().url()).toString(); return new Response.Builder() .protocol(Protocol.HTTP_1_1) .request(chain.request().newBuilder().url(httpsRequestUrl).build()) .code(200).message("OK") .body(ResponseBody.create(MediaType.parse("text/plain"), "")) .build(); }).build(); assertThat(checkGhostBlog(httpUrl, httpClient), urlMatches(httpsUrl)); }
@Test public void checkGhostBlog_underSubFolder() throws IOException { String blogUrl = HTTPS + server.getHostName() + ":" + server.getPort() + "/blog"; server.useHttps(Helpers.LOCALHOST_SOCKET_FACTORY, false); server.enqueue(new MockResponse()); OkHttpClient httpClient = Helpers.getProdHttpClient(); assertThat(checkGhostBlog(blogUrl, httpClient), is(blogUrl)); }
@Test public void checkGhostBlog_underSubDomain() throws IOException { String blogUrl = HTTPS + "blog." + server.getHostName() + ":" + server.getPort(); server.useHttps(Helpers.LOCALHOST_SOCKET_FACTORY, false); server.enqueue(new MockResponse()); OkHttpClient httpClient = Helpers.getProdHttpClient(); httpClient = httpClient.newBuilder().addInterceptor(chain -> { return new Response.Builder() .protocol(Protocol.HTTP_1_1) .request(chain.request()) .code(200).message("OK") .body(ResponseBody.create(MediaType.parse("text/plain"), "")) .build(); }).build(); assertThat(checkGhostBlog(blogUrl, httpClient), is(blogUrl)); }