NinjaServletContext extends AbstractContext { public void init(ServletContext servletContext, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) { this.servletContext = servletContext; this.httpServletRequest = httpServletRequest; this.httpServletResponse = httpServletResponse; enforceCorrectEncodingOfRequest(); String contextPath = httpServletRequest.getContextPath(); String requestPath = performGetRequestPath(); super.init(contextPath, requestPath); } @Inject  NinjaServletContext(
            BodyParserEngineManager bodyParserEngineManager,
            FlashScope flashScope,
            NinjaProperties ninjaProperties,
            ResultHandler resultHandler,
            Session session,
            Validation validation,
            Injector injector,
            ParamParsers paramParsers); void init(ServletContext servletContext,
            HttpServletRequest httpServletRequest,
            HttpServletResponse httpServletResponse); @Override String getMethod(); @Deprecated @Override String getRequestUri(); @Override String getHostname(); @Override String getParameter(String key); @Override List<String> getParameterValues(String name); @Override String getScheme(); @Override String getRealRemoteAddr(); @Override Object getAttribute(String name); @Override void setAttribute(String name, Object value); @Override Map<String, Object> getAttributes(); @Override FileItem getParameterAsFileItem(String key); @Override List<FileItem> getParameterAsFileItems(String key); @Override Map<String, List<FileItem>> getParameterFileItems(); @Override Map<String, String[]> getParameters(); @Override String getHeader(String name); @Override List<String> getHeaders(String name); @Override Map<String, List<String>> getHeaders(); @Override Cookie getCookie(String cookieName); @Override boolean hasCookie(String cookieName); @Override List<Cookie> getCookies(); @Override void addCookie(Cookie cookie); @Override boolean isAsync(); @Override void returnResultAsync(Result result); @Override void handleAsync(); @Override Result controllerReturned(); @Override InputStream getInputStream(); @Override BufferedReader getReader(); @Override String getRequestContentType(); @Override boolean isMultipart(); @Override FileItemIterator getFileItemIterator(); ServletContext getServletContext(); HttpServletRequest getHttpServletRequest(); HttpServletResponse getHttpServletResponse(); @Override void cleanup();  }
@Test public void testGetRemoteAddrReturnsDefaultRemoteAddr() { when(httpServletRequest.getRemoteAddr()).thenReturn("mockedRemoteAddr"); context.init(servletContext, httpServletRequest, httpServletResponse); assertEquals("mockedRemoteAddr", context.getRemoteAddr()); }
@Test public void testGetPathParameter() { context.init(servletContext, httpServletRequest, httpServletResponse); Map<String, String> parameterMap = Maps.newHashMap(); parameterMap.put("parameter", "parameter"); when(route.getPathParametersEncoded(Matchers.anyString())).thenReturn(parameterMap); context.setRoute(route); assertEquals(null, context.getPathParameter("parameter_not_set")); assertEquals("parameter", context.getPathParameter("parameter")); }
@Test public void testGetPathParameterDecodingWorks() { context.init(servletContext, httpServletRequest, httpServletResponse); Map<String, String> parameterMap = Maps.newHashMap(); parameterMap.put("parameter", "blue%2Fred%3Fand+green%E2%82%AC%2f"); when(route.getPathParametersEncoded(Matchers.anyString())).thenReturn(parameterMap); context.setRoute(route); assertEquals("blue/red?and+green€/", context.getPathParameter("parameter")); }
@Test public void testGetPathParameterAsInteger() { context.init(servletContext, httpServletRequest, httpServletResponse); Map<String, String> parameterMap = Maps.newHashMap(); parameterMap.put("parameter", "parameter"); when(route.getPathParametersEncoded(Matchers.anyString())).thenReturn(parameterMap); context.setRoute(route); assertEquals(null, context.getPathParameterAsInteger("parameter")); parameterMap.put("parameter", "1"); assertEquals(new Integer(1), context.getPathParameterAsInteger("parameter")); }
@Test public void testParseBodyJsonWorks() { when(httpServletRequest.getContentType()).thenReturn("application/json; charset=utf-8"); context.init(servletContext, httpServletRequest, httpServletResponse); when(bodyParserEngineManager.getBodyParserEngineForContentType("application/json")).thenReturn(bodyParserEngine); when(bodyParserEngine.invoke(context, Dummy.class)).thenReturn(new Dummy()); Object o = context.parseBody(Dummy.class); verify(bodyParserEngineManager).getBodyParserEngineForContentType("application/json"); assertTrue(o instanceof Dummy); }
@Test public void testParseBodyPostWorks() { when(httpServletRequest.getContentType()).thenReturn(ContentTypes.APPLICATION_POST_FORM); context.init(servletContext, httpServletRequest, httpServletResponse); when(bodyParserEngineManager.getBodyParserEngineForContentType(ContentTypes.APPLICATION_POST_FORM)).thenReturn(bodyParserEngine); Dummy dummy = new Dummy(); dummy.name = "post"; dummy.count = 245L; when(bodyParserEngine.invoke(context, Dummy.class)).thenReturn(dummy); Dummy o = context.parseBody(Dummy.class); verify(bodyParserEngineManager).getBodyParserEngineForContentType(ContentTypes.APPLICATION_POST_FORM); assertTrue(o instanceof Dummy); assertTrue(o.name.equals(dummy.name)); assertTrue(o.count.equals(dummy.count)); }
@Test public void testIsJsonWorks() { when(httpServletRequest.getContentType()).thenReturn(ContentTypes.APPLICATION_JSON); context.init(servletContext, httpServletRequest, httpServletResponse); assertTrue(context.isRequestJson()); }
@Test public void testIsXmlWorks() { when(httpServletRequest.getContentType()).thenReturn(ContentTypes.APPLICATION_XML); context.init(servletContext, httpServletRequest, httpServletResponse); assertTrue(context.isRequestXml()); }
@Test public void testParseBodyXmlWorks() { when(httpServletRequest.getContentType()).thenReturn("application/xml"); context.init(servletContext, httpServletRequest, httpServletResponse); when(bodyParserEngineManager.getBodyParserEngineForContentType("application/xml")).thenReturn(bodyParserEngine); when(bodyParserEngine.invoke(context, Dummy.class)).thenReturn(new Dummy()); Object o = context.parseBody(Dummy.class); verify(bodyParserEngineManager).getBodyParserEngineForContentType("application/xml"); assertTrue(o instanceof Dummy); }
@Test public void testParseBodyWithUnkownContentTypeWorks() { when(httpServletRequest.getContentType()).thenReturn(null); context.init(servletContext, httpServletRequest, httpServletResponse); Object o = context.parseBody(Dummy.class); assertNull(o); }
@Test public void testParseBodyWithUnknownRequestContentTypeWorks() { when(httpServletRequest.getContentType()).thenReturn("application/UNKNOWN"); context.init(servletContext, httpServletRequest, httpServletResponse); Object o = context.parseBody(Dummy.class); assertNull(o); }
@Test public void testInitEnforcingOfCorrectEncoding() throws Exception { context.init(servletContext, httpServletRequest, httpServletResponse); verify(httpServletRequest).setCharacterEncoding(NinjaConstant.UTF_8); }