Search in sources :

Example 46 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 47 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 48 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 49 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)

Example 50 with HttpHeaders

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

the class TestRestEndpoint method testAddDocumentNullMessage.

@Test(expected = ServerErrorException.class)
public void testAddDocumentNullMessage() {
    CatalogFramework framework = mock(CatalogFramework.class);
    RESTEndpoint rest = new RESTEndpoint(framework);
    HttpHeaders headers = mock(HttpHeaders.class);
    rest.addDocument(headers, mock(UriInfo.class), mock(HttpServletRequest.class), mock(MultipartBody.class), null, null);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpHeaders(javax.ws.rs.core.HttpHeaders) MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) CatalogFramework(ddf.catalog.CatalogFramework) UriInfo(javax.ws.rs.core.UriInfo) Test(org.junit.Test)

Aggregations

HttpHeaders (javax.ws.rs.core.HttpHeaders)87 Test (org.junit.Test)57 Message (org.apache.cxf.message.Message)31 HttpServletRequest (javax.servlet.http.HttpServletRequest)13 Optional (java.util.Optional)11 CatalogFramework (ddf.catalog.CatalogFramework)10 MultipartBody (org.apache.cxf.jaxrs.ext.multipart.MultipartBody)10 JsonObject (com.google.gson.JsonObject)9 Locale (java.util.Locale)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 Response (javax.ws.rs.core.Response)8 UriInfo (javax.ws.rs.core.UriInfo)8 IOException (java.io.IOException)7 WebApplicationException (javax.ws.rs.WebApplicationException)7 MediaType (javax.ws.rs.core.MediaType)7 RequestInfo (com.liferay.apio.architect.request.RequestInfo)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 Conditions (com.liferay.apio.architect.test.util.json.Conditions)5 OutputStream (java.io.OutputStream)5 Annotation (java.lang.annotation.Annotation)5