Search in sources :

Example 11 with StreamingHttpServiceFilter

use of io.servicetalk.http.api.StreamingHttpServiceFilter in project servicetalk by apple.

the class BasicAuthHttpServiceFilterTest method testProxyAuthenticationRequired.

private static void testProxyAuthenticationRequired(StreamingHttpRequest request) throws Exception {
    StreamingHttpServiceFilter service = new BasicAuthHttpServiceFilter.Builder<>(CREDENTIALS_VERIFIER, REALM_VALUE).buildProxy().create(HELLO_WORLD_SERVICE);
    StreamingHttpResponse response = awaitIndefinitelyNonNull(service.handle(CONN_CTX, request, reqRespFactory));
    assertEquals(PROXY_AUTHENTICATION_REQUIRED, response.status());
    assertEquals("Basic realm=\"" + REALM_VALUE + '"', response.headers().get(PROXY_AUTHENTICATE));
    assertEquals(ZERO, response.headers().get(CONTENT_LENGTH));
    assertFalse(response.headers().contains(USER_ID_AC_HEADER_NAME));
    assertFalse(response.headers().contains(USER_ID_RC_HEADER_NAME));
}
Also used : StreamingHttpServiceFilter(io.servicetalk.http.api.StreamingHttpServiceFilter) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse)

Example 12 with StreamingHttpServiceFilter

use of io.servicetalk.http.api.StreamingHttpServiceFilter in project servicetalk by apple.

the class BasicAuthHttpServiceFilterTest method utf8.

@Test
void utf8() throws Exception {
    final CredentialsVerifier<BasicUserInfo> utf8CredentialsVerifier = new CredentialsVerifier<BasicUserInfo>() {

        @Override
        public Single<BasicUserInfo> apply(final String userId, final String password) {
            if ("пароль".equals(password)) {
                return succeeded(new BasicUserInfo(userId));
            }
            return failed(new AuthenticationException("Wrong password"));
        }

        @Override
        public Completable closeAsync() {
            return completed();
        }
    };
    StreamingHttpServiceFilter service = new BasicAuthHttpServiceFilter.Builder<>(utf8CredentialsVerifier, REALM_VALUE).userInfoAsyncContextKey(USER_INFO_AC_KEY).userInfoRequestContextKey(USER_INFO_RC_KEY).setCharsetUtf8(true).buildServer().create(HELLO_WORLD_SERVICE);
    StreamingHttpResponse response = awaitIndefinitelyNonNull(service.handle(CONN_CTX, reqRespFactory.get("/path"), reqRespFactory));
    assertEquals(UNAUTHORIZED, response.status());
    assertEquals("Basic realm=\"" + REALM_VALUE + "\", charset=\"UTF-8\"", response.headers().get(WWW_AUTHENTICATE));
    assertFalse(response.headers().contains(USER_ID_AC_HEADER_NAME));
    assertFalse(response.headers().contains(USER_ID_RC_HEADER_NAME));
    StreamingHttpRequest request = reqRespFactory.get("/path");
    request.headers().set(AUTHORIZATION, "Basic " + base64("userId:пароль", UTF_8));
    testAuthenticated(request, service);
}
Also used : CredentialsVerifier(io.servicetalk.http.utils.auth.BasicAuthHttpServiceFilter.CredentialsVerifier) StreamingHttpServiceFilter(io.servicetalk.http.api.StreamingHttpServiceFilter) CharSequences.newAsciiString(io.servicetalk.buffer.api.CharSequences.newAsciiString) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) Test(org.junit.jupiter.api.Test)

Example 13 with StreamingHttpServiceFilter

use of io.servicetalk.http.api.StreamingHttpServiceFilter in project servicetalk by apple.

the class BasicAuthHttpServiceFilterTest method authenticatedWithoutUserInfo.

@Test
void authenticatedWithoutUserInfo() throws Exception {
    StreamingHttpServiceFilter service = new BasicAuthHttpServiceFilter.Builder<>(CREDENTIALS_VERIFIER, REALM_VALUE).buildServer().create(HELLO_WORLD_SERVICE);
    StreamingHttpRequest request = reqRespFactory.get("/path");
    request.headers().set(AUTHORIZATION, "Basic " + base64("userId:password"));
    StreamingHttpResponse response = awaitIndefinitelyNonNull(service.handle(CONN_CTX, request, reqRespFactory));
    assertEquals(OK, response.status());
    assertFalse(response.headers().contains(USER_ID_AC_HEADER_NAME));
    assertFalse(response.headers().contains(USER_ID_RC_HEADER_NAME));
}
Also used : StreamingHttpServiceFilter(io.servicetalk.http.api.StreamingHttpServiceFilter) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) Test(org.junit.jupiter.api.Test)

Example 14 with StreamingHttpServiceFilter

use of io.servicetalk.http.api.StreamingHttpServiceFilter in project servicetalk by apple.

the class BasicAuthHttpServiceFilterTest method testUnauthorized.

private static void testUnauthorized(StreamingHttpRequest request) throws Exception {
    StreamingHttpServiceFilter service = new BasicAuthHttpServiceFilter.Builder<>(CREDENTIALS_VERIFIER, REALM_VALUE).buildServer().create(HELLO_WORLD_SERVICE);
    StreamingHttpResponse response = awaitIndefinitelyNonNull(service.handle(CONN_CTX, request, reqRespFactory));
    assertEquals(UNAUTHORIZED, response.status());
    assertEquals("Basic realm=\"" + REALM_VALUE + '"', response.headers().get(WWW_AUTHENTICATE));
    assertEquals(ZERO, response.headers().get(CONTENT_LENGTH));
    assertFalse(response.headers().contains(USER_ID_AC_HEADER_NAME));
    assertFalse(response.headers().contains(USER_ID_RC_HEADER_NAME));
}
Also used : StreamingHttpServiceFilter(io.servicetalk.http.api.StreamingHttpServiceFilter) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse)

Example 15 with StreamingHttpServiceFilter

use of io.servicetalk.http.api.StreamingHttpServiceFilter in project servicetalk by apple.

the class H2PriorKnowledgeFeatureParityTest method queryParams.

private void queryParams(final HttpRequestMethod method) throws Exception {
    final String qpName = "foo";
    InetSocketAddress serverAddress = bindHttpEchoServer(service -> new StreamingHttpServiceFilter(service) {

        @Override
        public Single<StreamingHttpResponse> handle(final HttpServiceContext ctx, final StreamingHttpRequest request, final StreamingHttpResponseFactory responseFactory) {
            return request.queryParameter(qpName) == null ? succeeded(responseFactory.badRequest()) : super.handle(ctx, request, responseFactory);
        }
    }, null);
    String responseBody = "hello world";
    try (BlockingHttpClient client = forSingleAddress(HostAndPort.of(serverAddress)).protocols(h2PriorKnowledge ? h2Default() : h1Default()).executionStrategy(clientExecutionStrategy).buildBlocking()) {
        HttpResponse response = client.request(client.newRequest(method, "/p").addQueryParameters(qpName, "bar")).payloadBody(responseBody, textSerializerUtf8());
        assertThat("Unexpected response status.", response.status(), equalTo(OK));
    }
}
Also used : StreamingHttpServiceFilter(io.servicetalk.http.api.StreamingHttpServiceFilter) Single(io.servicetalk.concurrent.api.Single) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) InetSocketAddress(java.net.InetSocketAddress) HttpServiceContext(io.servicetalk.http.api.HttpServiceContext) StreamingHttpResponseFactory(io.servicetalk.http.api.StreamingHttpResponseFactory) HttpResponse(io.servicetalk.http.api.HttpResponse) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) Matchers.emptyString(org.hamcrest.Matchers.emptyString) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest)

Aggregations

StreamingHttpServiceFilter (io.servicetalk.http.api.StreamingHttpServiceFilter)21 StreamingHttpRequest (io.servicetalk.http.api.StreamingHttpRequest)17 StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)17 HttpServiceContext (io.servicetalk.http.api.HttpServiceContext)15 StreamingHttpResponseFactory (io.servicetalk.http.api.StreamingHttpResponseFactory)15 Single (io.servicetalk.concurrent.api.Single)14 Buffer (io.servicetalk.buffer.api.Buffer)7 BlockingHttpClient (io.servicetalk.http.api.BlockingHttpClient)7 HttpResponse (io.servicetalk.http.api.HttpResponse)7 OK (io.servicetalk.http.api.HttpResponseStatus.OK)7 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)7 Test (org.junit.jupiter.api.Test)7 InetSocketAddress (java.net.InetSocketAddress)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 Single.succeeded (io.servicetalk.concurrent.api.Single.succeeded)5 HttpHeaders (io.servicetalk.http.api.HttpHeaders)5 StreamingHttpClient (io.servicetalk.http.api.StreamingHttpClient)5 ServerContext (io.servicetalk.transport.api.ServerContext)5 CharSequences.newAsciiString (io.servicetalk.buffer.api.CharSequences.newAsciiString)4 Publisher (io.servicetalk.concurrent.api.Publisher)4