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