Search in sources :

Example 6 with HttpHeaders

use of javax.ws.rs.core.HttpHeaders in project graylog2-server by Graylog2.

the class RestToolsTest method buildEndpointUriReturnsHeaderValueIfHeaderIsPresent.

@Test
public void buildEndpointUriReturnsHeaderValueIfHeaderIsPresent() throws Exception {
    final HttpHeaders httpHeaders = mock(javax.ws.rs.core.HttpHeaders.class);
    when(httpHeaders.getRequestHeader(anyString())).thenReturn(ImmutableList.of("http://header.example.com"));
    final URI endpointUri = URI.create("http://graylog.example.com");
    assertThat(RestTools.buildEndpointUri(httpHeaders, endpointUri)).isEqualTo("http://header.example.com");
}
Also used : HttpHeaders(javax.ws.rs.core.HttpHeaders) URI(java.net.URI) Test(org.junit.Test)

Example 7 with HttpHeaders

use of javax.ws.rs.core.HttpHeaders in project graylog2-server by Graylog2.

the class RestToolsTest method buildEndpointUriReturnsDefaultUriIfHeaderIsMissing.

@Test
public void buildEndpointUriReturnsDefaultUriIfHeaderIsMissing() throws Exception {
    final HttpHeaders httpHeaders = mock(HttpHeaders.class);
    when(httpHeaders.getRequestHeader(anyString())).thenReturn(ImmutableList.of());
    final URI endpointUri = URI.create("http://graylog.example.com");
    assertThat(RestTools.buildEndpointUri(httpHeaders, endpointUri)).isEqualTo(endpointUri.toString());
}
Also used : HttpHeaders(javax.ws.rs.core.HttpHeaders) URI(java.net.URI) Test(org.junit.Test)

Example 8 with HttpHeaders

use of javax.ws.rs.core.HttpHeaders in project graylog2-server by Graylog2.

the class RestToolsTest method buildEndpointUriReturnsDefaultUriIfHeaderIsEmpty.

@Test
public void buildEndpointUriReturnsDefaultUriIfHeaderIsEmpty() throws Exception {
    final HttpHeaders httpHeaders = mock(HttpHeaders.class);
    when(httpHeaders.getRequestHeader(anyString())).thenReturn(ImmutableList.of(""));
    final URI endpointUri = URI.create("http://graylog.example.com");
    assertThat(RestTools.buildEndpointUri(httpHeaders, endpointUri)).isEqualTo(endpointUri.toString());
}
Also used : HttpHeaders(javax.ws.rs.core.HttpHeaders) URI(java.net.URI) Test(org.junit.Test)

Example 9 with HttpHeaders

use of javax.ws.rs.core.HttpHeaders in project OpenAM by OpenRock.

the class RestAuthCallbackHandlerManagerTest method shouldHandleCallbacksIntoJsonIfAtLeastOneCannotBeDoneInternally.

@Test
public void shouldHandleCallbacksIntoJsonIfAtLeastOneCannotBeDoneInternally() throws RestAuthResponseException, RestAuthException {
    //Given
    HttpHeaders headers = mock(HttpHeaders.class);
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    Callback callback1 = mock(Callback.class);
    Callback callback2 = mock(Callback.class);
    Callback[] callbacks = new Callback[] { callback1, callback2 };
    RestAuthCallbackHandler restAuthCallbackHandler1 = mock(RestAuthCallbackHandler.class);
    RestAuthCallbackHandler restAuthCallbackHandler2 = mock(RestAuthCallbackHandler.class);
    JsonValue jsonCallback1 = new JsonValue(new LinkedHashMap<String, String>());
    jsonCallback1.put("KEY1", "VALUE1");
    JsonValue jsonCallback2 = new JsonValue(new LinkedHashMap<String, String>());
    jsonCallback2.put("KEY2", "VALUE2");
    given(restAuthCallbackHandlerFactory.getRestAuthCallbackHandler(Matchers.<Class<? extends Callback>>anyObject())).willReturn(restAuthCallbackHandler1).willReturn(restAuthCallbackHandler2).willReturn(restAuthCallbackHandler1).willReturn(restAuthCallbackHandler2);
    given(restAuthCallbackHandler1.updateCallbackFromRequest(request, response, callback1)).willReturn(true);
    given(restAuthCallbackHandler2.updateCallbackFromRequest(request, response, callback2)).willReturn(false);
    given(restAuthCallbackHandler1.convertToJson(callback1, 1)).willReturn(jsonCallback1);
    given(restAuthCallbackHandler2.convertToJson(callback2, 2)).willReturn(jsonCallback2);
    //When
    JsonValue jsonCallbacks = restAuthCallbackHandlerManager.handleCallbacks(request, response, callbacks);
    //Then
    verify(restAuthCallbackHandler1).updateCallbackFromRequest(request, response, callback1);
    verify(restAuthCallbackHandler2).updateCallbackFromRequest(request, response, callback2);
    verify(restAuthCallbackHandler1).convertToJson(callback1, 1);
    verify(restAuthCallbackHandler2).convertToJson(callback2, 2);
    assertEquals(jsonCallbacks.size(), 2);
    assertEquals(jsonCallbacks.get(0).get("KEY1").asString(), "VALUE1");
    assertEquals(jsonCallbacks.get(1).get("KEY2").asString(), "VALUE2");
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpHeaders(javax.ws.rs.core.HttpHeaders) Callback(javax.security.auth.callback.Callback) JsonValue(org.forgerock.json.JsonValue) HttpServletResponse(javax.servlet.http.HttpServletResponse) BeforeClass(org.testng.annotations.BeforeClass) RestAuthCallbackHandler(org.forgerock.openam.core.rest.authn.callbackhandlers.RestAuthCallbackHandler) Test(org.testng.annotations.Test)

Example 10 with HttpHeaders

use of javax.ws.rs.core.HttpHeaders in project ddf by codice.

the class TestRestEndpoint method assertExceptionThrown.

protected void assertExceptionThrown(Class<? extends Throwable> klass) throws IngestException, SourceUnavailableException, URISyntaxException {
    CatalogFramework framework = mock(CatalogFramework.class);
    when(framework.create(isA(CreateRequest.class))).thenThrow(klass);
    when(framework.create(isA(CreateStorageRequest.class))).thenThrow(klass);
    HttpHeaders headers = createHeaders(Arrays.asList(MediaType.APPLICATION_JSON));
    RESTEndpoint rest = new RESTEndpoint(framework);
    addMatchingService(rest, Arrays.asList(getSimpleTransformer()));
    UriInfo info = givenUriInfo(SAMPLE_ID);
    try {
        rest.addDocument(headers, info, mock(HttpServletRequest.class), mock(MultipartBody.class), null, new ByteArrayInputStream("".getBytes()));
        fail();
    } catch (ServerErrorException e) {
        if (klass.getName().equals(SourceUnavailableException.class.getName())) {
            assertThat(e.getResponse().getStatus(), equalTo(INTERNAL_SERVER_ERROR));
        } else if (klass.getName().equals(IngestException.class.getName())) {
            assertThat(e.getResponse().getStatus(), equalTo(BAD_REQUEST));
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpHeaders(javax.ws.rs.core.HttpHeaders) ByteArrayInputStream(java.io.ByteArrayInputStream) CreateRequest(ddf.catalog.operation.CreateRequest) MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) CatalogFramework(ddf.catalog.CatalogFramework) IngestException(ddf.catalog.source.IngestException) UriInfo(javax.ws.rs.core.UriInfo) CreateStorageRequest(ddf.catalog.content.operation.CreateStorageRequest)

Aggregations

HttpHeaders (javax.ws.rs.core.HttpHeaders)12 HttpServletRequest (javax.servlet.http.HttpServletRequest)7 Test (org.junit.Test)7 UriInfo (javax.ws.rs.core.UriInfo)5 CatalogFramework (ddf.catalog.CatalogFramework)4 URI (java.net.URI)4 MultipartBody (org.apache.cxf.jaxrs.ext.multipart.MultipartBody)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 QueryResponse (ddf.catalog.operation.QueryResponse)2 SourceInfoResponse (ddf.catalog.operation.SourceInfoResponse)2 Callback (javax.security.auth.callback.Callback)2 Response (javax.ws.rs.core.Response)2 JsonValue (org.forgerock.json.JsonValue)2 RestAuthCallbackHandler (org.forgerock.openam.core.rest.authn.callbackhandlers.RestAuthCallbackHandler)2 BeforeClass (org.testng.annotations.BeforeClass)2 Test (org.testng.annotations.Test)2 CreateStorageRequest (ddf.catalog.content.operation.CreateStorageRequest)1 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)1 CreateRequest (ddf.catalog.operation.CreateRequest)1