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());
}
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());
}
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());
}
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"));
}
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);
}
}
Aggregations