Search in sources :

Example 1 with HttpRequestContext

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

the class UIModuleTest method processorNotUsedForNonBrowsePath.

@Test
public void processorNotUsedForNonBrowsePath() throws IOException {
    UIHttpRequestProcessor processor = new UIHttpRequestProcessor(new UIModuleConfig());
    HttpRequestContext context = Mockito.mock(HttpRequestContext.class);
    Mockito.when(context.getMethod()).thenReturn("POST");
    Mockito.when(context.getPath()).thenReturn("somethingDifferent/index.html");
    processor.process(context);
    Mockito.verify(context, Mockito.times(0)).setResponse(Mockito.anyInt(), Mockito.any(byte[].class));
}
Also used : UIHttpRequestProcessor(io.crnk.ui.internal.UIHttpRequestProcessor) HttpRequestContext(io.crnk.core.engine.http.HttpRequestContext) Test(org.junit.Test)

Example 2 with HttpRequestContext

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

the class UIModuleTest method processorReturnsFile.

@Test
public void processorReturnsFile() throws IOException {
    UIHttpRequestProcessor processor = new UIHttpRequestProcessor(new UIModuleConfig());
    HttpRequestContext context = Mockito.mock(HttpRequestContext.class);
    Mockito.when(context.getPath()).thenReturn("browse/index.html");
    Mockito.when(context.getMethod()).thenReturn("GET");
    processor.process(context);
    Mockito.verify(context, Mockito.times(1)).setResponse(Mockito.eq(200), Mockito.any(byte[].class));
    Mockito.verify(context, Mockito.times(1)).setContentType(Mockito.eq("text/html"));
}
Also used : UIHttpRequestProcessor(io.crnk.ui.internal.UIHttpRequestProcessor) HttpRequestContext(io.crnk.core.engine.http.HttpRequestContext) Test(org.junit.Test)

Example 3 with HttpRequestContext

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

the class ResourceFilterDirectoryImpl method getCache.

private Map<Object, FilterBehavior> getCache(HttpMethod method) {
    String key = ResourceFilterDirectoryImpl.class.getSimpleName() + method;
    HttpRequestContext requestContext = requestContextProvider.getRequestContext();
    if (requestContext == null) {
        // e.g. testing
        return new HashMap<>();
    }
    Map<Object, FilterBehavior> cache = (Map<Object, FilterBehavior>) requestContext.getRequestAttribute(key);
    if (cache == null) {
        cache = new HashMap<>();
        requestContext.setRequestAttribute(key, cache);
    }
    return cache;
}
Also used : HashMap(java.util.HashMap) HttpRequestContext(io.crnk.core.engine.http.HttpRequestContext) FilterBehavior(io.crnk.core.engine.filter.FilterBehavior) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with HttpRequestContext

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

the class ResourceFieldContributorTest method checkInclusionUponRequest.

@Test
public void checkInclusionUponRequest() {
    TaskRepository repo = new TaskRepository();
    Task task = new Task();
    task.setId(1L);
    task.setName("someTask");
    repo.save(task);
    HttpRequestProcessorImpl requestDispatcher = boot.getRequestDispatcher();
    HttpRequestContextProvider httpRequestContextProvider = boot.getModuleRegistry().getHttpRequestContextProvider();
    HttpRequestContext request = Mockito.mock(HttpRequestContext.class);
    httpRequestContextProvider.onRequestStarted(request);
    Map<String, Set<String>> parameters = new HashMap<>();
    parameters.put("include", Sets.newHashSet("contributedProject"));
    Response response = requestDispatcher.dispatchRequest("tasks", "GET", parameters, null, null);
    Document document = response.getDocument();
    Resource resource = document.getCollectionData().get().get(0);
    Assert.assertEquals("1", resource.getId());
    Assert.assertEquals("tasks", resource.getType());
    Assert.assertEquals("someTask", resource.getAttributes().get("name").asText());
    Relationship relationship = resource.getRelationships().get("contributedProject");
    Assert.assertNotNull(relationship);
    ResourceIdentifier relationshipId = relationship.getSingleData().get();
    Assert.assertEquals("projects", relationshipId.getType());
    Assert.assertEquals("11", relationshipId.getId());
    List<Resource> included = document.getIncluded();
    Assert.assertEquals(1, included.size());
    Resource includedResource = included.get(0);
    Assert.assertEquals("projects", includedResource.getType());
    Assert.assertEquals("11", includedResource.getId());
    Assert.assertEquals("someProject", includedResource.getAttributes().get("name").asText());
}
Also used : Task(io.crnk.core.mock.models.Task) Set(java.util.Set) HashMap(java.util.HashMap) ProjectToTaskRepository(io.crnk.core.mock.repository.ProjectToTaskRepository) TaskRepository(io.crnk.core.mock.repository.TaskRepository) Resource(io.crnk.core.engine.document.Resource) Document(io.crnk.core.engine.document.Document) HttpRequestProcessorImpl(io.crnk.core.engine.internal.http.HttpRequestProcessorImpl) Response(io.crnk.core.engine.dispatcher.Response) ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) Relationship(io.crnk.core.engine.document.Relationship) HttpRequestContext(io.crnk.core.engine.http.HttpRequestContext) HttpRequestContextProvider(io.crnk.core.engine.http.HttpRequestContextProvider) Test(org.junit.Test)

Example 5 with HttpRequestContext

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

the class UIModuleTest method processorNotUsedForPost.

@Test
public void processorNotUsedForPost() throws IOException {
    UIHttpRequestProcessor processor = new UIHttpRequestProcessor(new UIModuleConfig());
    HttpRequestContext context = Mockito.mock(HttpRequestContext.class);
    Mockito.when(context.getMethod()).thenReturn("POST");
    Mockito.when(context.getPath()).thenReturn("browse/index.html");
    processor.process(context);
    Mockito.verify(context, Mockito.times(0)).setResponse(Mockito.anyInt(), Mockito.any(byte[].class));
}
Also used : UIHttpRequestProcessor(io.crnk.ui.internal.UIHttpRequestProcessor) HttpRequestContext(io.crnk.core.engine.http.HttpRequestContext) Test(org.junit.Test)

Aggregations

HttpRequestContext (io.crnk.core.engine.http.HttpRequestContext)9 Test (org.junit.Test)5 UIHttpRequestProcessor (io.crnk.ui.internal.UIHttpRequestProcessor)4 Document (io.crnk.core.engine.document.Document)2 FilterBehavior (io.crnk.core.engine.filter.FilterBehavior)2 HttpRequestContextProvider (io.crnk.core.engine.http.HttpRequestContextProvider)2 HashMap (java.util.HashMap)2 Set (java.util.Set)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 CrnkBoot (io.crnk.core.boot.CrnkBoot)1 Response (io.crnk.core.engine.dispatcher.Response)1 Relationship (io.crnk.core.engine.document.Relationship)1 Resource (io.crnk.core.engine.document.Resource)1 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)1 HttpHeaders (io.crnk.core.engine.http.HttpHeaders)1 HttpMethod (io.crnk.core.engine.http.HttpMethod)1 HttpRequestProcessor (io.crnk.core.engine.http.HttpRequestProcessor)1 RepositoryInformation (io.crnk.core.engine.information.repository.RepositoryInformation)1 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)1