Search in sources :

Example 1 with ContainerRequestContext

use of jakarta.ws.rs.container.ContainerRequestContext in project OpenGrok by OpenGrok.

the class CorsFilterTest method testBoth.

private void testBoth(String origin, List<Object> headerValue) {
    CorsFilter filter = new CorsFilter();
    ContainerRequestContext request = mock(ContainerRequestContext.class);
    when(request.getHeaderString(CORS_REQUEST_HEADER)).thenReturn(origin);
    ContainerResponseContext response = mock(ContainerResponseContext.class);
    MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
    when(response.getHeaders()).thenReturn(headers);
    filter.filter(request, response);
    assertEquals(headerValue, headers.get(ALLOW_CORS_HEADER));
}
Also used : CorsFilter(org.opengrok.web.api.v1.filter.CorsFilter) MultivaluedHashMap(jakarta.ws.rs.core.MultivaluedHashMap) ContainerRequestContext(jakarta.ws.rs.container.ContainerRequestContext) ContainerResponseContext(jakarta.ws.rs.container.ContainerResponseContext)

Example 2 with ContainerRequestContext

use of jakarta.ws.rs.container.ContainerRequestContext in project OpenGrok by OpenGrok.

the class IncomingFilterTest method nonLocalhostTestWithTokenChange.

@Test
public void nonLocalhostTestWithTokenChange() throws Exception {
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    String token = "foobar";
    Map<String, String> headers = new TreeMap<>();
    final String authHeaderValue = IncomingFilter.BEARER + token;
    headers.put(HttpHeaders.AUTHORIZATION, authHeaderValue);
    assertTrue(env.getAuthenticationTokens().isEmpty());
    IncomingFilter filter = mockWithRemoteAddress("192.168.1.1", headers, true);
    ContainerRequestContext context = mockContainerRequestContext("test");
    ArgumentCaptor<Response> captor = ArgumentCaptor.forClass(Response.class);
    // No tokens configured.
    filter.filter(context);
    verify(context).abortWith(captor.capture());
    // Setting tokens without refreshing configuration should have no effect.
    Set<String> tokens = new HashSet<>();
    tokens.add(token);
    env.setAuthenticationTokens(tokens);
    filter.filter(context);
    verify(context, times(2)).abortWith(captor.capture());
    // The request should pass only after applyConfig().
    env.applyConfig(false, CommandTimeoutType.RESTFUL);
    context = mockContainerRequestContext("test");
    filter.filter(context);
    verify(context, never()).abortWith(captor.capture());
}
Also used : Response(jakarta.ws.rs.core.Response) RuntimeEnvironment(org.opengrok.indexer.configuration.RuntimeEnvironment) ContainerRequestContext(jakarta.ws.rs.container.ContainerRequestContext) TreeMap(java.util.TreeMap) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 3 with ContainerRequestContext

use of jakarta.ws.rs.container.ContainerRequestContext in project OpenGrok by OpenGrok.

the class IncomingFilterTest method nonLocalhostTestWithToken.

private void nonLocalhostTestWithToken(boolean allowed, String token) throws Exception {
    Map<String, String> headers = new TreeMap<>();
    final String authHeaderValue = IncomingFilter.BEARER + token;
    headers.put(HttpHeaders.AUTHORIZATION, authHeaderValue);
    IncomingFilter filter = mockWithRemoteAddress("192.168.1.1", headers, true);
    ContainerRequestContext context = mockContainerRequestContext("test");
    ArgumentCaptor<Response> captor = ArgumentCaptor.forClass(Response.class);
    filter.filter(context);
    if (allowed) {
        verify(context, never()).abortWith(captor.capture());
    } else {
        verify(context).abortWith(captor.capture());
    }
}
Also used : Response(jakarta.ws.rs.core.Response) ContainerRequestContext(jakarta.ws.rs.container.ContainerRequestContext) TreeMap(java.util.TreeMap)

Example 4 with ContainerRequestContext

use of jakarta.ws.rs.container.ContainerRequestContext in project OpenGrok by OpenGrok.

the class IncomingFilterTest method nonLocalhostTestWithoutToken.

@Test
public void nonLocalhostTestWithoutToken() throws Exception {
    IncomingFilter filter = mockWithRemoteAddress("192.168.1.1");
    ContainerRequestContext context = mockContainerRequestContext("test");
    ArgumentCaptor<Response> captor = ArgumentCaptor.forClass(Response.class);
    filter.filter(context);
    verify(context).abortWith(captor.capture());
    assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), captor.getValue().getStatus());
}
Also used : Response(jakarta.ws.rs.core.Response) ContainerRequestContext(jakarta.ws.rs.container.ContainerRequestContext) Test(org.junit.jupiter.api.Test)

Example 5 with ContainerRequestContext

use of jakarta.ws.rs.container.ContainerRequestContext in project OpenGrok by OpenGrok.

the class IncomingFilterTest method mockContainerRequestContext.

private ContainerRequestContext mockContainerRequestContext(final String path) {
    ContainerRequestContext context = mock(ContainerRequestContext.class);
    UriInfo info = mock(UriInfo.class);
    when(info.getPath()).thenReturn(path);
    when(context.getUriInfo()).thenReturn(info);
    return context;
}
Also used : ContainerRequestContext(jakarta.ws.rs.container.ContainerRequestContext) UriInfo(jakarta.ws.rs.core.UriInfo)

Aggregations

ContainerRequestContext (jakarta.ws.rs.container.ContainerRequestContext)8 Response (jakarta.ws.rs.core.Response)6 Test (org.junit.jupiter.api.Test)4 TreeMap (java.util.TreeMap)3 ContainerResponseContext (jakarta.ws.rs.container.ContainerResponseContext)1 MultivaluedHashMap (jakarta.ws.rs.core.MultivaluedHashMap)1 UriInfo (jakarta.ws.rs.core.UriInfo)1 HashSet (java.util.HashSet)1 RuntimeEnvironment (org.opengrok.indexer.configuration.RuntimeEnvironment)1 CorsFilter (org.opengrok.web.api.v1.filter.CorsFilter)1