Search in sources :

Example 16 with HttpRequestProcessorImpl

use of io.crnk.core.engine.internal.http.HttpRequestProcessorImpl in project crnk-framework by crnk-project.

the class JsonApiFormatTest method testRequestSubDirectory.

@Test
public void testRequestSubDirectory() throws IOException {
    ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class);
    ArgumentCaptor<byte[]> responseCaptor = ArgumentCaptor.forClass(byte[].class);
    HttpRequestContextBase requestContextBase = Mockito.mock(HttpRequestContextBase.class);
    Mockito.when(requestContextBase.getMethod()).thenReturn("GET");
    Mockito.when(requestContextBase.getPath()).thenReturn("/meta/");
    HttpRequestProcessorImpl requestDispatcher = boot.getRequestDispatcher();
    requestDispatcher.process(requestContextBase);
    Mockito.verify(requestContextBase, Mockito.times(1)).setResponse(statusCaptor.capture(), responseCaptor.capture());
    String expectedContentType = HomeModule.JSON_CONTENT_TYPE;
    Mockito.verify(requestContextBase, Mockito.times(1)).setResponseHeader("Content-Type", expectedContentType);
    Assert.assertEquals(200, (int) statusCaptor.getValue());
    String json = new String(responseCaptor.getValue());
    JsonNode response = boot.getObjectMapper().reader().readTree(json);
    JsonNode linksNode = response.get("links");
    JsonNode resourceNode = linksNode.get("resource");
    Assert.assertEquals("http://localhost/meta/resource", resourceNode.asText());
    Assert.assertNull(linksNode.get("meta"));
    Assert.assertNotNull(linksNode.get("element"));
    Assert.assertNotNull(linksNode.get("attribute"));
}
Also used : HttpRequestContextBase(io.crnk.core.engine.http.HttpRequestContextBase) JsonNode(com.fasterxml.jackson.databind.JsonNode) HttpRequestProcessorImpl(io.crnk.core.engine.internal.http.HttpRequestProcessorImpl) Test(org.junit.Test)

Example 17 with HttpRequestProcessorImpl

use of io.crnk.core.engine.internal.http.HttpRequestProcessorImpl in project crnk-framework by crnk-project.

the class JsonApiFormatTest method testJsonApiReturned.

private void testJsonApiReturned(boolean anyRequest) throws IOException {
    ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class);
    ArgumentCaptor<byte[]> responseCaptor = ArgumentCaptor.forClass(byte[].class);
    HttpRequestContextBase requestContextBase = Mockito.mock(HttpRequestContextBase.class);
    Mockito.when(requestContextBase.getMethod()).thenReturn("GET");
    Mockito.when(requestContextBase.getPath()).thenReturn("/");
    Mockito.when(requestContextBase.getRequestHeader("Accept")).thenReturn(anyRequest ? "*" : HttpHeaders.JSONAPI_CONTENT_TYPE);
    HttpRequestProcessorImpl requestDispatcher = boot.getRequestDispatcher();
    requestDispatcher.process(requestContextBase);
    Mockito.verify(requestContextBase, Mockito.times(1)).setResponse(statusCaptor.capture(), responseCaptor.capture());
    String expectedContentType = anyRequest ? HomeModule.JSON_CONTENT_TYPE : HttpHeaders.JSONAPI_CONTENT_TYPE;
    Mockito.verify(requestContextBase, Mockito.times(1)).setResponseHeader("Content-Type", expectedContentType);
    Assert.assertEquals(200, (int) statusCaptor.getValue());
    String json = new String(responseCaptor.getValue());
    JsonNode response = boot.getObjectMapper().reader().readTree(json);
    JsonNode resourcesNode = response.get("links");
    JsonNode tasksNode = resourcesNode.get("tasks");
    Assert.assertEquals("http://localhost/tasks", tasksNode.asText());
    Assert.assertEquals("http://localhost/meta/", resourcesNode.get("meta").asText());
    Assert.assertNull(resourcesNode.get("meta/resource"));
    Assert.assertNull(resourcesNode.get("meta/attribute"));
}
Also used : HttpRequestContextBase(io.crnk.core.engine.http.HttpRequestContextBase) JsonNode(com.fasterxml.jackson.databind.JsonNode) HttpRequestProcessorImpl(io.crnk.core.engine.internal.http.HttpRequestProcessorImpl)

Aggregations

HttpRequestProcessorImpl (io.crnk.core.engine.internal.http.HttpRequestProcessorImpl)17 Test (org.junit.Test)12 QuerySpecAdapterBuilder (io.crnk.core.queryspec.internal.QuerySpecAdapterBuilder)9 ControllerRegistry (io.crnk.core.engine.internal.dispatcher.ControllerRegistry)8 DefaultQuerySpecDeserializer (io.crnk.core.queryspec.DefaultQuerySpecDeserializer)8 RequestDispatcher (io.crnk.core.engine.dispatcher.RequestDispatcher)6 HttpRequestContextBase (io.crnk.core.engine.http.HttpRequestContextBase)6 ExceptionMapperRegistryTest (io.crnk.core.engine.internal.exception.ExceptionMapperRegistryTest)6 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 Document (io.crnk.core.engine.document.Document)4 HashMap (java.util.HashMap)4 Set (java.util.Set)4 Response (io.crnk.core.engine.dispatcher.Response)3 CollectionGet (io.crnk.core.engine.internal.dispatcher.controller.CollectionGet)3 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)3 QueryAdapterBuilder (io.crnk.core.engine.query.QueryAdapterBuilder)3 ConstantServiceUrlProvider (io.crnk.core.engine.url.ConstantServiceUrlProvider)3 RepositoryMethodParameterProvider (io.crnk.legacy.internal.RepositoryMethodParameterProvider)3 Matchers.anyString (org.mockito.Matchers.anyString)3