@Before public void setUp() throws Exception { dao = new DummyMonitorDAO(); filter = new MonitorFilter(new Monitor(dao), new MonitorRequestFilter()); chain = new MockFilterChain( new HttpServlet() { @Override public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { req.getInputStream().read(new byte[LONG_BODY_SIZE]); res.getOutputStream().write(new byte[0]); } }); filter.monitor.config.props.put( "maxBodySize", Integer.toString( MAX_BODY_SIZE)); // Ensure the configured property is correct for the tests }
private void testRemoteUser(Object principal) throws Exception { Authentication authentication = new TestingAuthenticationToken(principal, null); SecurityContextHolder.getContext().setAuthentication(authentication); HttpServletRequest req = request("POST", "/bar/foo", "78.56.34.12", null, null); filter.doFilter(req, response(), chain); RequestData data = dao.getLast(); assertEquals("username", data.getRemoteUser()); SecurityContextHolder.getContext().setAuthentication(null); }
filter.doFilter(req, response(), chain);
@Before public void setUp() throws Exception { dao = new DummyMonitorDAO(); filter = new MonitorFilter(new Monitor(dao), new MonitorRequestFilter()); chain = new MockFilterChain(); chain.setServlet(new HttpServlet() { @Override public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { req.getInputStream().read(new byte[LONG_BODY_SIZE]); res.getOutputStream().write(new byte[0]); } }); filter.monitor.config.props.put("maxBodySize", Integer.toString(MAX_BODY_SIZE)); // Ensure the configured property is correct for the tests }
filter.doFilter(req, response(), chain);
filter.doFilter(req, response(), chain);
@Test public void testWithLongBody() throws Exception { chain = new MockFilterChain( new HttpServlet() { @Override public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { req.getInputStream().read(new byte[LONG_BODY_SIZE]); res.getOutputStream().write("hello".getBytes()); } }); StringBuilder b = new StringBuilder(); for (int i = 0; i < MAX_BODY_SIZE; i++) { b.append('b'); } String wanted_body = b.toString(); for (int i = MAX_BODY_SIZE; i < LONG_BODY_SIZE; i++) { b.append('b'); } String given_body = b.toString(); HttpServletRequest req = request("POST", "/bar/foo", "78.56.34.12", given_body, null); filter.doFilter(req, response(), chain); RequestData data = dao.getLast(); assertEquals( wanted_body, new String(data.getBody())); // Should be trimmed to the maximum length assertEquals( LONG_BODY_SIZE, data.getBodyContentLength()); // Should be the full length, not the trimmed one }
public void testSimple() throws Exception { HttpServletRequest req = request("GET", "/foo/bar", "12.34.56.78", null, null); filter.doFilter(req, response(), chain); RequestData data = dao.getLast(); assertEquals("GET", data.getHttpMethod()); assertEquals("/foo/bar", data.getPath()); assertEquals("12.34.56.78", data.getRemoteAddr()); assertNull(data.getHttpReferer()); }
public void testReferer() throws Exception { HttpServletRequest req = request("GET", "/foo/bar", "12.34.56.78", null, "http://testhost/testpath"); filter.doFilter(req, response(), chain); RequestData data = dao.getLast(); assertEquals("GET", data.getHttpMethod()); assertEquals("/foo/bar", data.getPath()); assertEquals("12.34.56.78", data.getRemoteAddr()); assertEquals("http://testhost/testpath", data.getHttpReferer()); } public void testReferrer() throws Exception {
public void testReferrer() throws Exception { // "Referrer" was misspelled in the HTTP spec, check if it works with the "correct" // spelling. MockHttpServletRequest req = request("POST", "/bar/foo", "78.56.34.12", null, null); ((MockHttpServletRequest)req).setHeader("Referrer", "http://testhost/testpath"); filter.doFilter(req, response(), chain); RequestData data = dao.getLast(); assertEquals("POST", data.getHttpMethod()); assertEquals("/bar/foo", data.getPath()); assertEquals("78.56.34.12", data.getRemoteAddr()); assertEquals("http://testhost/testpath", data.getHttpReferer()); }
@Test public void testSimple() throws Exception { HttpServletRequest req = request("GET", "/foo/bar", "12.34.56.78", null, null); filter.doFilter(req, response(), chain); RequestData data = dao.getLast(); assertEquals("GET", data.getHttpMethod()); assertEquals("/foo/bar", data.getPath()); assertEquals("12.34.56.78", data.getRemoteAddr()); assertNull(data.getHttpReferer()); }
@Test public void testReferer() throws Exception { HttpServletRequest req = request("GET", "/foo/bar", "12.34.56.78", null, "http://testhost/testpath"); filter.doFilter(req, response(), chain); RequestData data = dao.getLast(); assertEquals("GET", data.getHttpMethod()); assertEquals("/foo/bar", data.getPath()); assertEquals("12.34.56.78", data.getRemoteAddr()); assertEquals("http://testhost/testpath", data.getHttpReferer()); }
public void testWithBody() throws Exception { chain.setServlet(new HttpServlet() { @Override public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { req.getInputStream().read(new byte[LONG_BODY_SIZE]); res.getOutputStream().write("hello".getBytes()); } }); HttpServletRequest req = request("POST", "/bar/foo", "78.56.34.12", "baz", null); filter.doFilter(req, response(), chain); RequestData data = dao.getLast(); assertEquals("POST", data.getHttpMethod()); assertEquals("/bar/foo", data.getPath()); assertEquals("78.56.34.12", data.getRemoteAddr()); assertNull(data.getHttpReferer()); assertEquals(new String(data.getBody()), "baz"); assertEquals(3, data.getBodyContentLength()); assertEquals(5, data.getResponseLength()); } public void testWithLongBody() throws Exception {
@Test public void testWithBody() throws Exception { chain = new MockFilterChain( new HttpServlet() { @Override public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { req.getInputStream().read(new byte[LONG_BODY_SIZE]); res.getOutputStream().write("hello".getBytes()); } }); HttpServletRequest req = request("POST", "/bar/foo", "78.56.34.12", "baz", null); filter.doFilter(req, response(), chain); RequestData data = dao.getLast(); assertEquals("POST", data.getHttpMethod()); assertEquals("/bar/foo", data.getPath()); assertEquals("78.56.34.12", data.getRemoteAddr()); assertNull(data.getHttpReferer()); assertEquals(new String(data.getBody()), "baz"); assertEquals(3, data.getBodyContentLength()); assertEquals(5, data.getResponseLength()); }
@Test public void testGWCHeaders() throws Exception { MockHttpServletRequest req = request("POST", "/bar/foo", "78.56.34.12", null, null); MockHttpServletResponse response = response(); String cacheResult = "Miss"; response.addHeader(MonitorFilter.GEOWEBCACHE_CACHE_RESULT, cacheResult); String missReason = "Wrong planet alignment"; response.addHeader(MonitorFilter.GEOWEBCACHE_MISS_REASON, missReason); filter.doFilter(req, response, chain); RequestData data = dao.getLast(); assertEquals("POST", data.getHttpMethod()); assertEquals("/bar/foo", data.getPath()); assertEquals("78.56.34.12", data.getRemoteAddr()); assertEquals(cacheResult, data.getCacheResult()); assertEquals(missReason, data.getMissReason()); }
@Test public void testReferrer() throws Exception { // "Referrer" was misspelled in the HTTP spec, check if it works with the "correct" // spelling. MockHttpServletRequest req = request("POST", "/bar/foo", "78.56.34.12", null, null); ((MockHttpServletRequest) req).addHeader("Referrer", "http://testhost/testpath"); filter.doFilter(req, response(), chain); RequestData data = dao.getLast(); assertEquals("POST", data.getHttpMethod()); assertEquals("/bar/foo", data.getPath()); assertEquals("78.56.34.12", data.getRemoteAddr()); assertEquals("http://testhost/testpath", data.getHttpReferer()); assertNull(data.getCacheResult()); assertNull(data.getMissReason()); }