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));
}
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);
}
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));
}
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));
}
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));
}
}
Aggregations