Example usage for org.springframework.mock.web MockHttpServletResponse getStatus

List of usage examples for org.springframework.mock.web MockHttpServletResponse getStatus

Introduction

In this page you can find the example usage for org.springframework.mock.web MockHttpServletResponse getStatus.

Prototype

@Override
    public int getStatus() 

Source Link

Usage

From source file:org.geoserver.catalog.rest.StyleTest.java

@Test
public void testGetWrongStyle() throws Exception {
    // Parameters for the request
    String ws = "gs";
    String style = "foooooo";
    // Request path
    String requestPath = "/rest/styles/" + style + ".html";
    String requestPath2 = "/rest/workspaces/" + ws + "/styles/" + style + ".html";
    // Exception path
    String exception = "No such style: " + style;
    String exception2 = "No such style " + style + " in workspace " + ws;

    // CASE 1: No workspace set

    // First request should thrown an exception
    MockHttpServletResponse response = getAsServletResponse(requestPath);
    assertEquals(404, response.getStatus());
    assertTrue(response.getContentAsString().contains(exception));

    // Same request with ?quietOnNotFound should not throw an exception
    response = getAsServletResponse(requestPath + "?quietOnNotFound=true");
    assertEquals(404, response.getStatus());
    assertFalse(response.getContentAsString().contains(exception));
    // No exception thrown
    assertTrue(response.getContentAsString().isEmpty());

    // CASE 2: workspace set

    // First request should thrown an exception
    response = getAsServletResponse(requestPath2);
    assertEquals(404, response.getStatus());
    assertTrue(response.getContentAsString().contains(exception2));

    // Same request with ?quietOnNotFound should not throw an exception
    response = getAsServletResponse(requestPath2 + "?quietOnNotFound=true");
    assertEquals(404, response.getStatus());
    assertFalse(response.getContentAsString().contains(exception2));
    // No exception thrown
    assertTrue(response.getContentAsString().isEmpty());
}

From source file:org.geoserver.catalog.rest.StyleTest.java

@Test
public void testGetFromWorkspace() throws Exception {
    MockHttpServletResponse resp = getAsServletResponse("/rest/workspaces/gs/styles/foo.xml");
    assertEquals(404, resp.getStatus());

    addStyleToWorkspace("foo");

    resp = getAsServletResponse("/rest/workspaces/gs/styles/foo.xml");
    assertEquals(200, resp.getStatus());

    Document dom = getAsDOM("/rest/workspaces/gs/styles/foo.xml");
    assertXpathEvaluatesTo("foo", "/style/name", dom);
    assertXpathEvaluatesTo("gs", "/style/workspace/name", dom);
}

From source file:org.geoserver.catalog.rest.StyleTest.java

@Test
public void testPostAsSLD() throws Exception {
    String xml = newSLDXML();/*from w  w  w  .  j a va2 s. c  o m*/

    MockHttpServletResponse response = postAsServletResponse("/rest/styles", xml, SLDHandler.MIMETYPE_10);
    assertEquals(201, response.getStatus());
    assertNotNull(response.getHeader("Location"));
    assertTrue(response.getHeader("Location").endsWith("/styles/foo"));

    assertNotNull(catalog.getStyleByName("foo"));
}

From source file:org.geoserver.catalog.rest.StyleTest.java

@Test
public void testPostExternalEntityAsSLD() throws Exception {
    String xml = IOUtils.toString(TestData.class.getResource("externalEntities.sld"), "UTF-8");

    MockHttpServletResponse response = postAsServletResponse("/rest/styles", xml, SLDHandler.MIMETYPE_10);
    assertEquals(500, response.getStatus());
    String message = response.getContentAsString();
    assertThat(message, containsString("Entity resolution disallowed"));
    assertThat(message, containsString("/this/file/does/not/exist"));
}

From source file:org.geoserver.catalog.rest.StyleTest.java

@Test
public void testPostAsSLDToWorkspace() throws Exception {
    assertNull(catalog.getStyleByName("gs", "foo"));

    String xml = newSLDXML();//from   w  w w  .ja v  a2 s  .co m

    MockHttpServletResponse response = postAsServletResponse("/rest/workspaces/gs/styles", xml,
            SLDHandler.MIMETYPE_10);
    assertEquals(201, response.getStatus());
    assertNotNull(response.getHeader("Location"));
    assertTrue(response.getHeader("Location").endsWith("/workspaces/gs/styles/foo"));

    assertNotNull(catalog.getStyleByName("gs", "foo"));

    GeoServerResourceLoader rl = getResourceLoader();
    assertNotNull(rl.find("workspaces", "gs", "styles", "foo.sld"));
}

From source file:org.geoserver.catalog.rest.StyleTest.java

@Test
public void testPostAsSLDWithName() throws Exception {
    String xml = newSLDXML();//from w w  w  . j a  v a2s.  c o  m

    MockHttpServletResponse response = postAsServletResponse("/rest/styles?name=bar", xml,
            SLDHandler.MIMETYPE_10);
    assertEquals(201, response.getStatus());
    assertNotNull(response.getHeader("Location"));
    assertTrue(response.getHeader("Location").endsWith("/styles/bar"));

    assertNotNull(catalog.getStyleByName("bar"));
}

From source file:org.geoserver.catalog.rest.StyleTest.java

@Test
public void testPostToWorkspace() throws Exception {
    Catalog cat = getCatalog();/*from w  w  w  .  j  a  va 2s .  c  om*/
    assertNull(cat.getStyleByName("gs", "foo"));

    String xml = "<style>" + "<name>foo</name>" + "<filename>foo.sld</filename>" + "</style>";
    MockHttpServletResponse response = postAsServletResponse("/rest/workspaces/gs/styles", xml);
    assertEquals(201, response.getStatus());
    assertNotNull(cat.getStyleByName("gs", "foo"));
}

From source file:org.geoserver.catalog.rest.StyleTest.java

@Test
public void testPut() throws Exception {
    StyleInfo style = catalog.getStyleByName("Ponds");
    assertEquals("Ponds.sld", style.getFilename());

    String xml = "<style>" + "<name>Ponds</name>" + "<filename>Forests.sld</filename>" + "</style>";

    MockHttpServletResponse response = putAsServletResponse("/rest/styles/Ponds", xml.getBytes(), "text/xml");
    assertEquals(200, response.getStatus());

    style = catalog.getStyleByName("Ponds");
    assertEquals("Forests.sld", style.getFilename());
}

From source file:org.geoserver.catalog.rest.StyleTest.java

@Test
public void testPutAsSLD() throws Exception {
    String xml = newSLDXML();/* w ww .j a v a  2  s .  c om*/

    MockHttpServletResponse response = putAsServletResponse("/rest/styles/Ponds", xml, SLDHandler.MIMETYPE_10);
    assertEquals(200, response.getStatus());

    Style s = catalog.getStyleByName("Ponds").getStyle();
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    new StyleFormat(SLDHandler.MIMETYPE_10, SLDHandler.VERSION_10, false, new SLDHandler(), null,
            getCatalog().getResourcePool().getEntityResolver()).write(s, out);

    xml = new String(out.toByteArray());
    assertTrue(xml.contains("<sld:Name>foo</sld:Name>"));
}

From source file:org.geoserver.catalog.rest.StyleTest.java

@Test
public void testPutToWorkspace() throws Exception {
    testPostToWorkspace();/* w  w  w .  ja v  a2s  .c o m*/

    Catalog cat = getCatalog();
    assertEquals("foo.sld", cat.getStyleByName("gs", "foo").getFilename());

    String xml = "<style>" + "<filename>bar.sld</filename>" + "</style>";

    MockHttpServletResponse response = putAsServletResponse("/rest/workspaces/gs/styles/foo", xml,
            "application/xml");
    assertEquals(200, response.getStatus());
    assertEquals("bar.sld", cat.getStyleByName("gs", "foo").getFilename());
}