Search in sources :

Example 1 with HttpHeaders

use of org.projectnessie.client.http.HttpHeaders in project nessie by projectnessie.

the class TestAwsAuthProvider method checkAuth.

void checkAuth(NessieAuthentication authentication, String expectedRegion, String expectedKeyId) {
    assertThat(authentication).isInstanceOf(HttpAuthentication.class);
    HttpAuthentication httpAuthentication = (HttpAuthentication) authentication;
    // Intercept the call to HttpClient.register(RequestFilter) and extract the RequestFilter for
    // our test
    RequestFilter[] authFilter = new RequestFilter[1];
    HttpClient.Builder client = Mockito.mock(HttpClient.Builder.class);
    Mockito.doAnswer(invocationOnMock -> {
        Object[] args = invocationOnMock.getArguments();
        if (args.length == 1 && args[0] instanceof RequestFilter && args[0].getClass().getSimpleName().equalsIgnoreCase("AwsHttpAuthenticationFilter")) {
            authFilter[0] = (RequestFilter) args[0];
        }
        return null;
    }).when(client).addRequestFilter(Mockito.any());
    httpAuthentication.applyToHttpClient(client);
    // Check that the registered RequestFilter works as expected (sets the right HTTP headers)
    assertThat(authFilter[0]).isInstanceOf(RequestFilter.class);
    HttpHeaders headers = new HttpHeaders();
    RequestContext context = new RequestContext(headers, URI.create("http://localhost/"), Method.GET, null);
    authFilter[0].filter(context);
    assertThat(headers.asMap()).containsKey("Authorization").containsKey("X-Amz-Date").extracting("Authorization", InstanceOfAssertFactories.iterable(String.class)).first(InstanceOfAssertFactories.STRING).containsSequence(expectedRegion).containsSequence(expectedKeyId).containsSequence("Signature=").containsSequence("Credential=");
}
Also used : HttpHeaders(org.projectnessie.client.http.HttpHeaders) HttpAuthentication(org.projectnessie.client.http.HttpAuthentication) HttpClient(org.projectnessie.client.http.HttpClient) RequestContext(org.projectnessie.client.http.RequestContext) RequestFilter(org.projectnessie.client.http.RequestFilter)

Example 2 with HttpHeaders

use of org.projectnessie.client.http.HttpHeaders in project nessie by projectnessie.

the class TestBearerAuthenticationProvider method checkAuth.

void checkAuth(NessieAuthentication authentication) {
    assertThat(authentication).isInstanceOf(HttpAuthentication.class);
    HttpAuthentication httpAuthentication = (HttpAuthentication) authentication;
    // Intercept the call to HttpClient.register(RequestFilter) and extract the RequestFilter for
    // our test
    RequestFilter[] authFilter = new RequestFilter[1];
    HttpClient.Builder client = Mockito.mock(HttpClient.Builder.class);
    Mockito.doAnswer(invocationOnMock -> {
        Object[] args = invocationOnMock.getArguments();
        if (args.length == 1 && args[0] instanceof RequestFilter) {
            authFilter[0] = (RequestFilter) args[0];
        }
        return null;
    }).when(client).addRequestFilter(Mockito.any());
    httpAuthentication.applyToHttpClient(client);
    // Check that the registered RequestFilter works as expected (sets the right HTTP header)
    assertThat(authFilter[0]).isInstanceOf(RequestFilter.class);
    HttpHeaders headers = new HttpHeaders();
    RequestContext context = new RequestContext(headers, null, null, null);
    authFilter[0].filter(context);
    assertThat(headers.asMap()).containsKey("Authorization").extracting("Authorization", InstanceOfAssertFactories.iterable(String.class)).containsExactly("Bearer token123");
}
Also used : HttpHeaders(org.projectnessie.client.http.HttpHeaders) HttpAuthentication(org.projectnessie.client.http.HttpAuthentication) HttpClient(org.projectnessie.client.http.HttpClient) RequestContext(org.projectnessie.client.http.RequestContext) RequestFilter(org.projectnessie.client.http.RequestFilter)

Example 3 with HttpHeaders

use of org.projectnessie.client.http.HttpHeaders in project nessie by projectnessie.

the class TestBasicAuthProvider method checkAuth.

void checkAuth(NessieAuthentication authentication) {
    assertThat(authentication).isInstanceOf(HttpAuthentication.class);
    HttpAuthentication httpAuthentication = (HttpAuthentication) authentication;
    // Intercept the call to HttpClient.register(RequestFilter) and extract the RequestFilter for
    // our test
    RequestFilter[] authFilter = new RequestFilter[1];
    HttpClient.Builder client = Mockito.mock(HttpClient.Builder.class);
    Mockito.doAnswer(invocationOnMock -> {
        Object[] args = invocationOnMock.getArguments();
        if (args.length == 1 && args[0] instanceof RequestFilter) {
            authFilter[0] = (RequestFilter) args[0];
        }
        return null;
    }).when(client).addRequestFilter(Mockito.any());
    httpAuthentication.applyToHttpClient(client);
    // Check that the registered RequestFilter works as expected (sets the right HTTP header)
    assertThat(authFilter[0]).isInstanceOf(RequestFilter.class);
    HttpHeaders headers = new HttpHeaders();
    RequestContext context = new RequestContext(headers, null, null, null);
    authFilter[0].filter(context);
    assertThat(headers.asMap()).containsKey("Authorization").extracting("Authorization", InstanceOfAssertFactories.iterable(String.class)).containsExactly("Basic QWxhZGRpbjpPcGVuU2VzYW1l");
}
Also used : HttpHeaders(org.projectnessie.client.http.HttpHeaders) HttpAuthentication(org.projectnessie.client.http.HttpAuthentication) HttpClient(org.projectnessie.client.http.HttpClient) RequestContext(org.projectnessie.client.http.RequestContext) RequestFilter(org.projectnessie.client.http.RequestFilter)

Aggregations

HttpAuthentication (org.projectnessie.client.http.HttpAuthentication)3 HttpClient (org.projectnessie.client.http.HttpClient)3 HttpHeaders (org.projectnessie.client.http.HttpHeaders)3 RequestContext (org.projectnessie.client.http.RequestContext)3 RequestFilter (org.projectnessie.client.http.RequestFilter)3