use of io.crnk.core.engine.internal.http.HttpRequestProcessorImpl 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());
}
}
use of io.crnk.core.engine.internal.http.HttpRequestProcessorImpl 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());
}
use of io.crnk.core.engine.internal.http.HttpRequestProcessorImpl in project crnk-framework by crnk-project.
the class HttpRequestProcessorImplTest method onGivenPathAndRequestTypeControllerShouldHandleRequest.
@Test
public void onGivenPathAndRequestTypeControllerShouldHandleRequest() throws Exception {
// GIVEN
String path = "/tasks/";
String requestType = "GET";
ControllerRegistry controllerRegistry = new ControllerRegistry(null);
CollectionGet controller = mock(CollectionGet.class);
controllerRegistry.addController(controller);
QuerySpecAdapterBuilder queryAdapterBuilder = new QuerySpecAdapterBuilder(new DefaultQuerySpecDeserializer(), moduleRegistry);
RequestDispatcher sut = new HttpRequestProcessorImpl(moduleRegistry, controllerRegistry, null, queryAdapterBuilder);
// WHEN
when(controller.isAcceptable(any(JsonPath.class), eq(requestType))).thenCallRealMethod();
Map<String, Set<String>> parameters = new HashMap<>();
sut.dispatchRequest(path, requestType, parameters, null, null);
// THEN
verify(controller, times(1)).handle(any(JsonPath.class), any(QueryAdapter.class), any(RepositoryMethodParameterProvider.class), any(Document.class));
}
use of io.crnk.core.engine.internal.http.HttpRequestProcessorImpl in project crnk-framework by crnk-project.
the class HttpRequestProcessorImplTest method checkProcess.
@Test
public void checkProcess() throws IOException {
HttpRequestContextBase requestContextBase = Mockito.mock(HttpRequestContextBase.class);
HttpRequestContextBaseAdapter requestContext = new HttpRequestContextBaseAdapter(requestContextBase);
Mockito.when(requestContextBase.getMethod()).thenReturn("GET");
Mockito.when(requestContextBase.getPath()).thenReturn("/tasks/");
Mockito.when(requestContextBase.getRequestHeader("Accept")).thenReturn("*");
ControllerRegistry controllerRegistry = new ControllerRegistry(null);
CollectionGet controller = mock(CollectionGet.class);
when(controller.isAcceptable(any(JsonPath.class), eq("GET"))).thenCallRealMethod();
controllerRegistry.addController(controller);
QuerySpecAdapterBuilder queryAdapterBuilder = new QuerySpecAdapterBuilder(new DefaultQuerySpecDeserializer(), moduleRegistry);
RequestDispatcher sut = new HttpRequestProcessorImpl(moduleRegistry, controllerRegistry, null, queryAdapterBuilder);
sut.process(requestContext);
verify(controller, times(1)).handle(any(JsonPath.class), any(QueryAdapter.class), any(RepositoryMethodParameterProvider.class), any(Document.class));
}
use of io.crnk.core.engine.internal.http.HttpRequestProcessorImpl in project crnk-framework by crnk-project.
the class HttpRequestProcessorImplTest method shouldMapExceptionToErrorResponseIfMapperIsAvailable.
@Test
public void shouldMapExceptionToErrorResponseIfMapperIsAvailable() throws Exception {
ControllerRegistry controllerRegistry = mock(ControllerRegistry.class);
// noinspection unchecked
when(controllerRegistry.getController(any(JsonPath.class), anyString())).thenThrow(IllegalStateException.class);
QuerySpecAdapterBuilder queryAdapterBuilder = new QuerySpecAdapterBuilder(new DefaultQuerySpecDeserializer(), moduleRegistry);
RequestDispatcher requestDispatcher = new HttpRequestProcessorImpl(moduleRegistry, controllerRegistry, ExceptionMapperRegistryTest.exceptionMapperRegistry, queryAdapterBuilder);
Response response = requestDispatcher.dispatchRequest("tasks", null, null, null, null);
assertThat(response).isNotNull();
assertThat(response.getHttpStatus()).isEqualTo(HttpStatus.BAD_REQUEST_400);
}
Aggregations