Example usage for org.springframework.mock.web MockHttpServletRequest setPathInfo

List of usage examples for org.springframework.mock.web MockHttpServletRequest setPathInfo

Introduction

In this page you can find the example usage for org.springframework.mock.web MockHttpServletRequest setPathInfo.

Prototype

public void setPathInfo(@Nullable String pathInfo) 

Source Link

Usage

From source file:org.dataconservancy.deposit.status.SimpleStatusServletTest.java

private void doBasicRetrieval(String depositid, String mgrid) throws Exception {
    final String BASEURL = "http://example.org/webapp/depositServlet";
    final String MGR_ID_1 = "id_1";

    final String STATUS_CONTENT = "status content";
    final String STATUS_MIME = "text/foo";

    DepositDocumentServlet serv = new DepositStatusServlet();
    serv.setBaseURL(BASEURL);/*from w ww .  ja va  2  s  .c om*/

    List<StatusTestDepositManager> mgrs = getTestManagers(MGR_ID_1, mgrid);
    serv.setDepositManagers(mgrs);

    MockDepositInfo status = new MockDepositInfo(depositid, mgrid);
    MockDepositDocument statusDoc = new MockDepositDocument();
    statusDoc.setInputStream(IOUtils.toInputStream(STATUS_CONTENT));
    statusDoc.setMimeType(STATUS_MIME);
    status.setDepositStatus(statusDoc);

    mgrs.get(1).setDepositStatus(depositid, status);

    String url = serv.getURL(status);

    MockHttpServletRequest request = new MockHttpServletRequest("GET", url);
    request.setPathInfo(url.replace(BASEURL, ""));

    MockHttpServletResponse response = new MockHttpServletResponse();

    serv.doGet(request, response);

    assertEquals("Wrong response http code", HttpServletResponse.SC_OK, response.getStatus());
    assertEquals("Wrong response mime type", response.getContentType(), STATUS_MIME);
    assertEquals("Did not return correct content", STATUS_CONTENT, response.getContentAsString());
}

From source file:org.geogig.geoserver.functional.GeoServerTestSupport.java

/**
 * Issue a POST request to the provided URL with the given file passed as form data.
 *
 * @param resourceUri the url to issue the request to
 * @param formFieldName the form field name for the file to be posted
 * @param file the file to post//from  w  w  w  .j  av  a  2 s  .  c  o m
 *
 * @return the response to the request
 */
public MockHttpServletResponse postFile(String resourceUri, String formFieldName, File file) throws Exception {

    try (FileInputStream fis = new FileInputStream(file)) {
        MockMultipartFile mFile = new MockMultipartFile(formFieldName, fis);
        MockMultipartHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders
                .fileUpload(new URI(resourceUri)).file(mFile);

        MockHttpServletRequest request = requestBuilder.buildRequest(applicationContext.getServletContext());

        /**
         * Duplicated from GeoServerSystemTestSupport#createRequest to do the same work on the
         * MockMultipartHttpServletRequest
         */
        request.setScheme("http");
        request.setServerName("localhost");
        request.setServerPort(8080);
        request.setContextPath("/geoserver");
        request.setRequestURI(
                ResponseUtils.stripQueryString(ResponseUtils.appendPath("/geoserver/", resourceUri)));
        // request.setRequestURL(ResponseUtils.appendPath("http://localhost:8080/geoserver",
        // path ) );
        request.setQueryString(ResponseUtils.getQueryString(resourceUri));
        request.setRemoteAddr("127.0.0.1");
        request.setServletPath(ResponseUtils.makePathAbsolute(ResponseUtils.stripRemainingPath(resourceUri)));
        request.setPathInfo(ResponseUtils.makePathAbsolute(
                ResponseUtils.stripBeginningPath(ResponseUtils.stripQueryString(resourceUri))));
        request.addHeader("Host", "localhost:8080");

        // deal with authentication
        if (username != null) {
            String token = username + ":";
            if (password != null) {
                token += password;
            }
            request.addHeader("Authorization", "Basic " + new String(Base64.encodeBase64(token.getBytes())));
        }

        kvp(request, resourceUri);

        request.setUserPrincipal(null);
        /**
         * End duplication
         */

        return dispatch(request);
    }
}

From source file:org.geowebcache.service.wmts.WMTSRestTest.java

@Test
public void testGetCap() throws Exception {
    MockHttpServletRequest req = new MockHttpServletRequest();
    req.setPathInfo("geowebcache/service/wmts/rest/WMTSCapabilities.xml");
    MockHttpServletResponse resp = dispatch(req);

    assertEquals(200, resp.getStatus());
    assertEquals("text/xml", resp.getContentType());
    final Document doc = XMLUnit.buildTestDocument(resp.getContentAsString());
    assertXpathExists("//wmts:Contents/wmts:Layer", doc);
    assertXpathExists("//wmts:Contents/wmts:Layer[ows:Identifier='mockLayer']", doc);
    assertXpathEvaluatesTo("2", "count(//wmts:Contents/wmts:Layer/wmts:Style/ows:Identifier)", doc);
    assertXpathExists("//wmts:Contents/wmts:Layer/wmts:Style[ows:Identifier='style-a']", doc);
    assertXpathExists("//wmts:Contents/wmts:Layer/wmts:Style[ows:Identifier='style-b']/wmts:LegendURL"
            + "[@width='125'][@height='130'][@format='image/png']"
            + "[@minScaleDenominator='5000.0'][@maxScaleDenominator='10000.0']"
            + "[@xlink:href='https://some-url?some-parameter=value3&another-parameter=value4']", doc);
    assertXpathExists("//wmts:Contents/wmts:Layer/wmts:ResourceURL[@resourceType='tile']"
            + "[@format='image/jpeg']" + "[@template='http://localhost/service/wmts/rest/"
            + "mockLayer/{style}/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}?format=image/jpeg&time={time}&elevation={elevation}']",
            doc);/*from w w w.j a va 2  s . c o  m*/
    assertXpathExists("//wmts:Contents/wmts:Layer/wmts:ResourceURL[@resourceType='FeatureInfo']"
            + "[@format='text/plain']" + "[@template='http://localhost/service/wmts/rest"
            + "/mockLayer/{style}/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}/{J}/{I}?format=text/plain&time={time}&elevation={elevation}']",
            doc);
    assertXpathExists("//wmts:ServiceMetadataURL[@xlink:href='http://localhost/service/wmts/rest"
            + "/WMTSCapabilities.xml']", doc);
}

From source file:org.geowebcache.service.wmts.WMTSRestTest.java

@Test
public void testGetTileWithStyle() throws Exception {
    MockHttpServletRequest req = new MockHttpServletRequest();
    req.setPathInfo("geowebcache/service/wmts/rest/mockLayer/style-a/EPSG:4326/EPSG:4326:0/0/0");
    req.addParameter("format", "image/png");

    MockHttpServletResponse resp = dispatch(req);

    assertEquals(200, resp.getStatus());
    assertEquals("image/png", resp.getContentType());
    assertEquals("EPSG:4326", resp.getHeader("geowebcache-crs"));
    assertArrayEquals(getSampleTileContent().getContents(), resp.getContentAsByteArray());
}

From source file:org.geowebcache.service.wmts.WMTSRestTest.java

@Test
public void testGetTileWithoutStyle() throws Exception {
    MockHttpServletRequest req = new MockHttpServletRequest();
    req.setPathInfo("geowebcache/service/wmts/rest/mockLayer/EPSG:4326/EPSG:4326:0/0/0");
    req.addParameter("format", "image/png");

    final MockHttpServletResponse resp = dispatch(req);
    assertEquals(200, resp.getStatus());
}

From source file:org.geowebcache.service.wmts.WMTSRestTest.java

@Test
public void testGetTileWithEmptyStyle() throws Exception {
    MockHttpServletRequest req = new MockHttpServletRequest();
    req.setPathInfo("geowebcache/service/wmts/rest/mockLayer//EPSG:4326/EPSG:4326:0/0/0");
    req.addParameter("format", "image/png");

    final MockHttpServletResponse resp = dispatch(req);
    assertEquals(200, resp.getStatus());
}

From source file:org.geowebcache.service.wmts.WMTSRestTest.java

@Test
public void testGetInfoWithStyle() throws Exception {
    MockHttpServletRequest req = new MockHttpServletRequest();
    req.setPathInfo("geowebcache/service/wmts/rest/mockLayer/style-a/EPSG:4326/EPSG:4326:0/0/0/0/0");
    req.addParameter("format", "text/plain");

    final MockHttpServletResponse resp = dispatch(req);

    assertEquals(200, resp.getStatus());
    assertEquals("text/plain", resp.getContentType());
}

From source file:org.geowebcache.service.wmts.WMTSRestTest.java

@Test
public void testGetInfoWithoutStyle() throws Exception {
    MockHttpServletRequest req = new MockHttpServletRequest();
    req.setPathInfo("geowebcache/service/wmts/rest/mockLayer/EPSG:4326/EPSG:4326:0/0/0/0/0");
    req.addParameter("format", "text/plain");

    final MockHttpServletResponse resp = dispatch(req);

    assertEquals(200, resp.getStatus());
    assertEquals("text/plain", resp.getContentType());
}

From source file:org.geowebcache.service.wmts.WMTSRestTest.java

@Test
public void testOWSException() throws Exception {
    MockHttpServletRequest req = new MockHttpServletRequest();
    req.setPathInfo("geowebcache/service/wmts/rest/mockLayer/EPSG:4326/EPSG:4326:0/0/0/0/0");
    req.addParameter("format", "text/none");

    MockHttpServletResponse resp = dispatch(req);

    assertEquals(HttpStatus.SC_BAD_REQUEST, resp.getStatus());
    assertEquals("text/xml", resp.getContentType());

    Document doc = XMLUnit.buildTestDocument(resp.getContentAsString());
    assertXpathExists("//ows:ExceptionReport/ows:Exception[@exceptionCode='InvalidParameterValue']", doc);
}

From source file:org.osaf.cosmo.BaseMockServletTestCase.java

/**
 *//*  ww  w.  java 2  s .  co m*/
protected MockHttpServletRequest createMockRequest(String method, String pathInfo) {
    MockHttpServletRequest request = new MockHttpServletRequest(servletContext, method,
            getServletPath() + pathInfo);
    request.setServletPath(getServletPath());
    request.setPathInfo(pathInfo);
    request.addHeader("Host", request.getServerName() + ":" + request.getServerPort());
    return request;
}