Search in sources :

Example 1 with HttpRequestContextBase

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

the class HomeResourceFilteringTest method checkResponse.

private void checkResponse(boolean filtered) 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("*");
    HttpRequestProcessorImpl requestDispatcher = boot.getRequestDispatcher();
    requestDispatcher.process(requestContextBase);
    Mockito.verify(requestContextBase, Mockito.times(1)).setResponse(statusCaptor.capture(), responseCaptor.capture());
    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");
    if (filtered) {
        Assert.assertNull(tasksNode);
    } else {
        Assert.assertEquals("http://localhost/tasks", tasksNode.asText());
    }
}
Also used : HttpRequestContextBase(io.crnk.core.engine.http.HttpRequestContextBase) JsonNode(com.fasterxml.jackson.databind.JsonNode) HttpRequestProcessorImpl(io.crnk.core.engine.internal.http.HttpRequestProcessorImpl)

Example 2 with HttpRequestContextBase

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

the class JsonHomeFormatTest method testHomeJsonReturned.

private void testHomeJsonReturned(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 ? "*" : HomeModule.JSON_HOME_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 : HomeModule.JSON_HOME_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("resources");
    JsonNode usersNode = resourcesNode.get("tag:tasks");
    Assert.assertEquals("tasks", usersNode.get("href").asText());
}
Also used : HttpRequestContextBase(io.crnk.core.engine.http.HttpRequestContextBase) JsonNode(com.fasterxml.jackson.databind.JsonNode) HttpRequestProcessorImpl(io.crnk.core.engine.internal.http.HttpRequestProcessorImpl)

Example 3 with HttpRequestContextBase

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

the class HomeModuleTest method testNonRootRequestNotTouchedForDifferentUrl.

@Test
public void testNonRootRequestNotTouchedForDifferentUrl() throws IOException {
    HttpRequestContextBase requestContextBase = Mockito.mock(HttpRequestContextBase.class);
    // create json api request
    Mockito.when(requestContextBase.getMethod()).thenReturn("GET");
    Mockito.when(requestContextBase.getPath()).thenReturn("/doesNotExists");
    Mockito.when(requestContextBase.getRequestHeader("Accept")).thenReturn("*");
    // execute
    HttpRequestProcessorImpl requestDispatcher = boot.getRequestDispatcher();
    requestDispatcher.process(requestContextBase);
    Mockito.verify(requestContextBase, Mockito.times(0)).setResponse(Mockito.anyInt(), (byte[]) Mockito.anyObject());
}
Also used : HttpRequestContextBase(io.crnk.core.engine.http.HttpRequestContextBase) HttpRequestProcessorImpl(io.crnk.core.engine.internal.http.HttpRequestProcessorImpl) Test(org.junit.Test)

Example 4 with HttpRequestContextBase

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

the class HomeModuleTest method testNonRootRequestNotTouchedForDifferentContentType.

@Test
public void testNonRootRequestNotTouchedForDifferentContentType() throws IOException {
    HttpRequestContextBase requestContextBase = Mockito.mock(HttpRequestContextBase.class);
    Mockito.when(requestContextBase.getMethod()).thenReturn("GET");
    Mockito.when(requestContextBase.getPath()).thenReturn("/");
    Mockito.when(requestContextBase.getRequestHeader("Accept")).thenReturn("application/doesNotExists");
    HttpRequestProcessorImpl requestDispatcher = boot.getRequestDispatcher();
    requestDispatcher.process(requestContextBase);
    Mockito.verify(requestContextBase, Mockito.times(0)).setResponse(Mockito.anyInt(), (byte[]) Mockito.anyObject());
}
Also used : HttpRequestContextBase(io.crnk.core.engine.http.HttpRequestContextBase) HttpRequestProcessorImpl(io.crnk.core.engine.internal.http.HttpRequestProcessorImpl) Test(org.junit.Test)

Example 5 with HttpRequestContextBase

use of io.crnk.core.engine.http.HttpRequestContextBase 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)

Aggregations

HttpRequestContextBase (io.crnk.core.engine.http.HttpRequestContextBase)6 HttpRequestProcessorImpl (io.crnk.core.engine.internal.http.HttpRequestProcessorImpl)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 Test (org.junit.Test)3