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);
  }
  /**
   * 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());
  }
  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());
  }
  @Before
  public void setUp() throws Exception {
    ctx = new MockServletContext();
    ctx.setInitParameter("security-config", "/WEB-INF/security-config.xml");
    ctx.setResourceAsStream(
        "/WEB-INF/security-config.xml", SecConfigTest.getSecurityConfigInputStream());

    MockFilterConfig conf = new MockFilterConfig();
    conf.setupServletContext(ctx);

    req = new MockHttpServletRequest();
    req.setContextPath("contextPath/");

    resp = new MockHttpServletResponse();
    chain = new MockFilterChain();

    me = new SecFilter();
    me.init(conf);
  }
  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);
  }
  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"));
  }
  public void testParseKVP() throws Exception {
    URL url = getClass().getResource("applicationContext.xml");

    FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext(url.toString());

    Dispatcher dispatcher = (Dispatcher) context.getBean("dispatcher");

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setContextPath("/geoserver");

    request.setupAddParameter("service", "hello");
    request.setupAddParameter("request", "Hello");
    request.setupAddParameter("message", "Hello world!");

    request.setQueryString("service=hello&request=hello&message=Hello World!");

    Request req = new Request();
    req.setHttpRequest(request);

    dispatcher.parseKVP(req);

    Message message = (Message) dispatcher.parseRequestKVP(Message.class, req);
    assertEquals(new Message("Hello world!"), message);
  }