Search in sources :

Example 6 with ServletRequestContext

use of io.crnk.servlet.internal.ServletRequestContext in project crnk-framework by crnk-project.

the class ServletRequestContextTest method testBaseUrlForInvalidServletPath.

@Test
public void testBaseUrlForInvalidServletPath() {
    MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
    request.setMethod("GET");
    request.setContextPath("");
    // invalid => Spring Boot setup as it seems
    request.setServletPath("/api/tasks/");
    request.setPathInfo(null);
    request.setRequestURI("/api/tasks/");
    request.setContentType(HttpHeaders.JSONAPI_CONTENT_TYPE);
    request.setServerName("test");
    request.setServerPort(1234);
    ServletRequestContext context = new ServletRequestContext(servletContext, request, response, "/api");
    Assert.assertEquals("http://test:1234/api", context.getBaseUrl());
    Assert.assertEquals("/tasks/", context.getPath());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestContext(io.crnk.servlet.internal.ServletRequestContext) Test(org.junit.Test)

Example 7 with ServletRequestContext

use of io.crnk.servlet.internal.ServletRequestContext in project crnk-framework by crnk-project.

the class ServletRequestContextTest method testGetUrl.

@Test
public void testGetUrl() {
    MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
    request.setMethod("GET");
    request.setContextPath("");
    request.setServletPath("/api");
    request.setPathInfo("/tasks/");
    request.setRequestURI("/api/tasks/");
    request.setContentType(HttpHeaders.JSONAPI_CONTENT_TYPE);
    request.addHeader("Accept", "*/*");
    request.setServerName("test");
    request.setServerPort(1234);
    ServletRequestContext context = new ServletRequestContext(servletContext, request, response, "/api");
    Assert.assertEquals("http://test:1234/api", context.getBaseUrl());
    Assert.assertEquals("/tasks/", context.getPath());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestContext(io.crnk.servlet.internal.ServletRequestContext) Test(org.junit.Test)

Example 8 with ServletRequestContext

use of io.crnk.servlet.internal.ServletRequestContext in project crnk-framework by crnk-project.

the class ServletRequestContextTest method testParameter.

@Test
public void testParameter() {
    MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
    request.addParameter("include[test]", "a,b,c");
    ServletRequestContext context = new ServletRequestContext(servletContext, request, response, null);
    Map<String, Set<String>> parameters = context.getRequestParameters();
    Assert.assertEquals(1, parameters.size());
    Set<String> values = parameters.get("include[test]");
    Assert.assertEquals(1, values.size());
    Assert.assertEquals("a,b,c", values.iterator().next());
}
Also used : Set(java.util.Set) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestContext(io.crnk.servlet.internal.ServletRequestContext) Test(org.junit.Test)

Example 9 with ServletRequestContext

use of io.crnk.servlet.internal.ServletRequestContext in project crnk-framework by crnk-project.

the class ServletModuleTest method testSecurityProviderInstalled.

@Test
public void testSecurityProviderInstalled() {
    HttpRequestContextProvider provider = new HttpRequestContextProvider();
    ServletModule module = new ServletModule(provider);
    CrnkBoot boot = new CrnkBoot();
    boot.addModule(module);
    boot.boot();
    SecurityProvider securityProvider = boot.getModuleRegistry().getSecurityProvider();
    ServletContext servletContext = Mockito.mock(ServletContext.class);
    MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
    MockHttpServletResponse response = new MockHttpServletResponse();
    request.addUserRole("guest");
    request.addUserRole("admin");
    provider.onRequestStarted(new HttpRequestContextBaseAdapter(new ServletRequestContext(servletContext, request, response, "api", HttpHeaders.DEFAULT_CHARSET)));
    Assert.assertFalse(securityProvider.isUserInRole("doesNotExist"));
    Assert.assertTrue(securityProvider.isUserInRole("guest"));
    Assert.assertTrue(securityProvider.isUserInRole("admin"));
}
Also used : HttpRequestContextBaseAdapter(io.crnk.core.engine.internal.http.HttpRequestContextBaseAdapter) CrnkBoot(io.crnk.core.boot.CrnkBoot) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) SecurityProvider(io.crnk.core.engine.security.SecurityProvider) ServletContext(javax.servlet.ServletContext) ServletRequestContext(io.crnk.servlet.internal.ServletRequestContext) ServletModule(io.crnk.servlet.internal.ServletModule) HttpRequestContextProvider(io.crnk.core.engine.http.HttpRequestContextProvider) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 10 with ServletRequestContext

use of io.crnk.servlet.internal.ServletRequestContext in project crnk-framework by crnk-project.

the class CrnkFilter method doFilter.

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
    if (req instanceof HttpServletRequest && res instanceof HttpServletResponse) {
        ServletContext servletContext = filterConfig.getServletContext();
        ServletRequestContext context = new ServletRequestContext(servletContext, (HttpServletRequest) req, (HttpServletResponse) res, boot.getWebPathPrefix(), defaultCharacterEncoding);
        RequestDispatcher requestDispatcher = boot.getRequestDispatcher();
        requestDispatcher.process(context);
        if (!context.checkAbort()) {
            chain.doFilter(req, res);
        }
    } else {
        chain.doFilter(req, res);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletRequestContext(io.crnk.servlet.internal.ServletRequestContext) RequestDispatcher(io.crnk.core.engine.dispatcher.RequestDispatcher)

Aggregations

ServletRequestContext (io.crnk.servlet.internal.ServletRequestContext)10 Test (org.junit.Test)7 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)5 RequestDispatcher (io.crnk.core.engine.dispatcher.RequestDispatcher)3 ServletContext (javax.servlet.ServletContext)3 HttpRequestContextBaseAdapter (io.crnk.core.engine.internal.http.HttpRequestContextBaseAdapter)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)2 CrnkBoot (io.crnk.core.boot.CrnkBoot)1 HttpRequestContextProvider (io.crnk.core.engine.http.HttpRequestContextProvider)1 SecurityProvider (io.crnk.core.engine.security.SecurityProvider)1 ServletModule (io.crnk.servlet.internal.ServletModule)1 Set (java.util.Set)1