public void testReadOpContext() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); request.setContextPath("/geoserver"); request.setRequestURI("/geoserver/hello"); request.setMethod("get"); Dispatcher dispatcher = new Dispatcher(); Request req = new Request(); req.httpRequest = request; dispatcher.init(req); Map map = dispatcher.readOpContext(req); assertEquals("hello", map.get("service")); request = new MockHttpServletRequest(); request.setContextPath("/geoserver"); request.setRequestURI("/geoserver/foobar/hello"); request.setMethod("get"); map = dispatcher.readOpContext(req); assertEquals("hello", map.get("service")); request = new MockHttpServletRequest(); request.setContextPath("/geoserver"); request.setRequestURI("/geoserver/foobar/hello/"); request.setMethod("get"); map = dispatcher.readOpContext(req); assertEquals("hello", map.get("service")); }
public void testReadContextAndPath() throws Exception { Dispatcher dispatcher = new Dispatcher(); MockHttpServletRequest request = new MockHttpServletRequest(); request.setContextPath("/geoserver"); request.setRequestURI("/geoserver/hello"); request.setMethod("get"); Request req = new Request(); req.httpRequest = request; dispatcher.init(req); assertNull(req.context); assertEquals("hello", req.path); request.setRequestURI("/geoserver/foo/hello"); dispatcher.init(req); assertEquals("foo", req.context); assertEquals("hello", req.path); request.setRequestURI("/geoserver/foo/baz/hello/"); dispatcher.init(req); assertEquals("foo/baz", req.context); assertEquals("hello", req.path); }
@Test public void testDoFilterOk() throws IOException, ServletException { req.setRequestURI("/img/blub.jpg"); me.doFilter(req, resp, chain); assertEquals(req, chain.getLastRequest()); assertEquals(resp, chain.getLastResponse()); }
/** * Tests mixed get/post situations for cases in which there is no kvp parser * * @throws Exception */ public void testHelloOperationMixed() throws Exception { URL url = getClass().getResource("applicationContextOnlyXml.xml"); FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext(url.toString()); Dispatcher dispatcher = (Dispatcher) context.getBean("dispatcher"); final String body = "<Hello service=\"hello\" message=\"Hello world!\" version=\"1.0.0\" />"; MockHttpServletRequest request = new MockHttpServletRequest() { String encoding; public int getServerPort() { return 8080; } public String getCharacterEncoding() { return encoding; } public void setCharacterEncoding(String encoding) { this.encoding = encoding; } public ServletInputStream getInputStream() throws IOException { final ServletInputStream stream = super.getInputStream(); return new ServletInputStream() { public int read() throws IOException { return stream.read(); } public int available() { return body.length(); } }; } }; request.setScheme("http"); request.setServerName("localhost"); request.setContextPath("/geoserver"); request.setMethod("POST"); request.setRequestURI("http://localhost/geoserver/ows"); request.setContentType("application/xml"); request.setBodyContent(body); MockHttpServletResponse response = new MockHttpServletResponse(); request.setupAddParameter("strict", "true"); dispatcher.handleRequest(request, response); assertEquals("Hello world!", response.getOutputStreamContent()); }
@SuppressWarnings("unchecked") @Test public void testDoFilterRedirectLogin() throws IOException, ServletException { req.setRequestURI("/admin/bla.jsp"); me.doFilter(req, resp, chain); assertNull(chain.getLastRequest()); assertNull(chain.getLastResponse()); assertFalse(resp.wasRedirectSent()); Map<String, MockRequestDispatcher> m = req.getRequestDispatcherMap(); assertEquals("/login.jsp", m.get(m.keySet().iterator().next()).getPath()); }
public void testDispatchWithNamespace() throws Exception { URL url = getClass().getResource("applicationContextNamespace.xml"); FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext(url.toString()); Dispatcher dispatcher = (Dispatcher) context.getBean("dispatcher"); MockHttpServletRequest request = new MockHttpServletRequest() { String encoding; public int getServerPort() { return 8080; } public String getCharacterEncoding() { return encoding; } public void setCharacterEncoding(String encoding) { this.encoding = encoding; } }; request.setScheme("http"); request.setServerName("localhost"); request.setContextPath("/geoserver"); request.setMethod("POST"); MockHttpServletResponse response = new MockHttpServletResponse(); request.setContentType("application/xml"); request.setBodyContent( "<h:Hello service='hello' message='Hello world!' xmlns:h='http://hello.org' />"); request.setRequestURI("http://localhost/geoserver/hello"); dispatcher.handleRequest(request, response); assertEquals("Hello world!", response.getOutputStreamContent()); request.setBodyContent( "<h:Hello service='hello' message='Hello world!' xmlns:h='http://hello.org/v2' />"); response = new MockHttpServletResponse(); dispatcher.handleRequest(request, response); assertEquals("Hello world!:V2", response.getOutputStreamContent()); }
public void testHttpErrorCodeException() throws Exception { URL url = getClass().getResource("applicationContext.xml"); FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext(url.toString()); Dispatcher dispatcher = (Dispatcher) context.getBean("dispatcher"); MockHttpServletRequest request = new MockHttpServletRequest() { String encoding; public int getServerPort() { return 8080; } public String getCharacterEncoding() { return encoding; } public void setCharacterEncoding(String encoding) { this.encoding = encoding; } }; request.setScheme("http"); request.setServerName("localhost"); request.setContextPath("/geoserver"); request.setMethod("GET"); CodeExpectingHttpServletResponse response = new CodeExpectingHttpServletResponse(new MockHttpServletResponse()); request.setupAddParameter("service", "hello"); request.setupAddParameter("request", "httpErrorCodeException"); request.setupAddParameter("version", "1.0.0"); request.setRequestURI( "http://localhost/geoserver/ows?service=hello&request=hello&message=HelloWorld"); request.setQueryString("service=hello&request=hello&message=HelloWorld"); dispatcher.handleRequest(request, response); assertEquals(HttpServletResponse.SC_NO_CONTENT, response.getStatusCode()); }
public void testNoErrorOn304ErrorCodeException() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); request.setContextPath("/geoserver"); request.setRequestURI("/geoserver/hello"); request.setMethod("get"); Dispatcher dispatcher = new Dispatcher(); Request req = new Request(); req.httpRequest = request; dispatcher.init(req); MockHttpServletResponse response = new MockHttpServletResponse(); req.setHttpResponse(response); RuntimeException error = new HttpErrorCodeException(304, "Not Modified"); dispatcher.exception(error, null, req); assertNull("Exception erroneously saved", req.error); }
public void testErrorSavedOnRequestOnNon304ErrorCodeException() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); request.setContextPath("/geoserver"); request.setRequestURI("/geoserver/hello"); request.setMethod("get"); Dispatcher dispatcher = new Dispatcher(); Request req = new Request(); req.httpRequest = request; dispatcher.init(req); MockHttpServletResponse response = new MockHttpServletResponse(); req.setHttpResponse(response); RuntimeException genericError = new HttpErrorCodeException(500, "Internal Server Error"); dispatcher.exception(genericError, null, req); assertEquals("Exception did not get saved", genericError, req.error); }
@SuppressWarnings("unchecked") @Test public void testDoFilterRedirectDeny() throws IOException, ServletException { req.setRequestURI("/admin/bla.jsp"); // getSession() returns null otherwise - violation of the spec req.setSession(new MockHttpSession()); SessionToken tok = new SessionToken("login", 1234, false); SecManager.getInstance(ctx).setSessionToken(req, tok); me.doFilter(req, resp, chain); assertNull(chain.getLastRequest()); assertNull(chain.getLastResponse()); assertFalse(resp.wasRedirectSent()); Map<String, MockRequestDispatcher> m = req.getRequestDispatcherMap(); assertEquals("/deny.do", m.get(m.keySet().iterator().next()).getPath()); }
public void testReadOpPost() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); request.setContextPath("/geoserver"); request.setRequestURI("/geoserver/hello"); request.setMethod("post"); String body = "<Hello service=\"hello\"/>"; MockServletInputStream input = new MockServletInputStream(body.getBytes()); Dispatcher dispatcher = new Dispatcher(); BufferedReader buffered = new BufferedReader(new InputStreamReader(input)); buffered.mark(2048); Map map = dispatcher.readOpPost(buffered); assertNotNull(map); assertEquals("Hello", map.get("request")); assertEquals("hello", map.get("service")); }