SecuredHost implements Host { @Override @Loggable(value = Loggable.DEBUG, ignore = IOException.class) public Resource fetch(@NotNull final URI uri, @NotNull final Range range, @NotNull final Version version)throws IOException { final Resource res; if (this.isHidden(uri)) { res = this.secured(uri, range, version); } else { res = this.host.fetch(uri, range, version); } return res; } SecuredHost(@NotNull final Host hst,
        @NotNull final HttpRequest rqst); @Override @Loggable(value = Loggable.DEBUG, ignore = IOException.class) Resource fetch(@NotNull final URI uri, @NotNull final Range range,
        @NotNull final Version version); @Override boolean isHidden(@NotNull final URI uri); @Override boolean authorized(@NotNull final String user,
        @NotNull final String password); @Override void close(); @Override String syslog(); @Override Stats stats();  }
@Test public void requestsAuthorization() throws Exception { final String[] hosts = { "example.com", "maven.s3auth.com", }; for (final String name : hosts) { try { new SecuredHost( new HostMocker().mock(), HttpRequestMocker.toRequest( String.format("GET / HTTP/1.1\nHost: %s\n\n", name) ) ).fetch(URI.create("/"), Range.ENTIRE, Version.LATEST); Assert.fail("exception expected"); } catch (final HttpException ex) { MatcherAssert.assertThat( HttpResponseMocker.toString(ex.response()), Matchers.allOf( Matchers.containsString(HttpHeaders.WWW_AUTHENTICATE), Matchers.containsString( String.format("Basic realm=\"%s\"", name) ) ) ); } } }
@Test public void requestsAuthorizationWhenBrokenData() throws Exception { final String[] requests = { "GET / HTTP/1.1\nAuthorization: xxx\n\n", "GET / HTTP/1.1\nAuthorization: Basic a1b2c3==\n\n", "GET / HTTP/1.1\nAuthorization: Basic \n\n", }; for (final String http : requests) { try { new SecuredHost( new HostMocker().mock(), HttpRequestMocker.toRequest(http) ).fetch(URI.create("/test.html"), Range.ENTIRE, Version.LATEST); Assert.fail("exception expected, but didn't happen"); } catch (final HttpException ex) { MatcherAssert.assertThat( HttpResponseMocker.toString(ex.response()), Matchers.startsWith( String.format( "HTTP/1.1 %d ", HttpURLConnection.HTTP_BAD_REQUEST ) ) ); } } }
@Test public void reportsToStringWhenAuthorizationFails() throws Exception { try { new SecuredHost( new Host() { @Override public String toString() { return "hello, world!"; } @Override public boolean isHidden(final URI uri) { return true; } @Override public boolean authorized(final String user, final String pwd) { return false; } @Override public Resource fetch(final URI uri, final Range range, final Version version) { throw new UnsupportedOperationException(); } @Override public void close() { throw new UnsupportedOperationException(); } @Override public String syslog() { throw new UnsupportedOperationException(); } @Override public Stats stats() { throw new UnsupportedOperationException(); } }, HttpRequestMocker.toRequest( "GET / HTTP/1.1\nAuthorization: Basic dGVzdDp0ZXN0\n\n" ) ).fetch( URI.create("/test-request.html"), Range.ENTIRE, Version.LATEST ); Assert.fail("authorization failed expected, but didn't happen"); } catch (final HttpException ex) { MatcherAssert.assertThat( HttpResponseMocker.toString(ex.response()), Matchers.containsString("hello") ); } }