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

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

Introduction

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

Prototype

public void setContextPath(String contextPath) 

Source Link

Usage

From source file:fragment.web.AuthenticationControllerTest.java

@Test
public void testPasswordResetRequestSubmitUnverifiedUser() throws Exception {
    asRoot();/*from   ww  w  .j a v a  2 s  . c  o  m*/
    User user = createTestUserInTenant(getDefaultTenant());
    eventListener.clear();
    asAnonymous();
    MockHttpServletRequest request = getRequestTemplate(HttpMethod.GET, "/portal/reset_password");
    request.setServletPath("/portal");
    request.setContextPath("/portal");
    request.setLocalPort(8080);
    String view = controller.requestReset(user.getUsername(), request, new ModelMap());
    Assert.assertEquals("auth.request_reset_success", view);
    Assert.assertEquals(0, eventListener.getEvents().size());
}

From source file:fragment.web.AuthenticationControllerTest.java

@Test
public void testPasswordResetRequestSubmit() throws Exception {
    User user = userDAO.find(2L);/*  w w  w  .ja v  a 2s  .c o m*/
    MockHttpServletRequest request = getRequestTemplate(HttpMethod.GET, "/portal/reset_password");
    request.setServletPath("/portal");
    request.setContextPath("/portal");
    request.setLocalPort(8080);
    String view = controller.requestReset(user.getUsername(), request, new ModelMap());
    Assert.assertEquals("auth.request_reset_success", view);
    Assert.assertEquals(1, eventListener.getEvents().size());
    PortalEvent event = eventListener.getEvents().get(0);
    Assert.assertTrue(event.getPayload() instanceof PasswordResetRequest);
    Assert.assertEquals(user.getUsername(), ((PasswordResetRequest) event.getPayload()).getUsername());
    Assert.assertEquals(user, event.getSource());
}

From source file:org.opennms.netmgt.ncs.rest.AbstractSpringJerseyRestTestCase.java

protected MockHttpServletRequest createRequest(final String requestType, final String urlPath) {
    final MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), requestType,
            contextPath + urlPath) {//from  w  ww  . jav a 2 s  .  c om

        @Override
        public void setContentType(final String contentType) {
            super.setContentType(contentType);
            super.addHeader("Content-Type", contentType);
        }

    };
    request.setContextPath(contextPath);
    return request;
}

From source file:org.wrml.server.WrmlServletTest.java

private void initMockHttpRequest(MockHttpServletRequest req, URI uri) {

    req.setRequestURI(uri.toString());//  www .  j a va 2s  .  c om
    req.setPathInfo(uri.getPath());
    req.setRemotePort(uri.getPort());
    req.setRemoteHost(uri.getHost());
    req.setScheme(uri.getScheme());
    req.setContextPath("/");
}

From source file:fr.xebia.servlet.filter.XForwardedFilterTest.java

@Test
public void testToAbsoluteInResponse() {
    // PREPARE/*from w w w.ja  v a  2s .co m*/
    XForwardedFilter xFilter = new XForwardedFilter();
    MockHttpServletRequest request = new MockHttpServletRequest();
    HttpServletResponse mockResponse = new MockHttpServletResponse();
    XForwardedResponse response = xFilter.new XForwardedResponse(mockResponse, request);
    request.setScheme("http");
    request.setServerName("localhost");
    request.setServerPort(80);
    request.setContextPath("/context");
    request.setRequestURI("/context/dir/test");

    // TEST and VERIFY
    assertEquals("relative uri", "http://localhost/context/dir/relativeURI",
            response.toAbsolute("relativeURI"));
    assertEquals("relative to host uri", "http://localhost/relativeURI", response.toAbsolute("/relativeURI"));
    assertEquals("relative to context root uri", "http://localhost/context/relativeURI",
            response.toAbsolute(request.getContextPath() + "/relativeURI"));
    assertEquals("absolute uri", "https://server/othercontext/uri",
            response.toAbsolute("https://server/othercontext/uri"));
}

From source file:com.boundlessgeo.geoserver.AppIntegrationTest.java

@Test
public void testImportInfo() throws IOException {
    Catalog catalog = getCatalog();//from  ww  w .j  av a  2  s  .c o m
    StoreInfo targetStore = catalog.getStoreByName("sf", "sf", StoreInfo.class);
    assertNotNull(targetStore);

    ImportController ctrl = new ImportController(getGeoServer(), applicationContext);

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setContextPath("/geoserver");
    request.setRequestURI("/geoserver/hello");
    request.setMethod("post");

    JSONObj obj = ctrl.info("sf");

    assertEquals("sf", obj.str("workspace"));
    assertNotNull(obj.get("spaceAvailable"));
    assertEquals(obj.get("spaceAvailable"), obj.get("tmpSpace"));
}

From source file:com.boundlessgeo.geoserver.AppIntegrationTest.java

@Test
public void testImportGeoJSONintoDb() throws IOException, Exception {
    Catalog catalog = getCatalog();/*from  w w w  . ja  v a2  s .c o m*/
    StoreInfo targetStore = catalog.getStoreByName("sf", "sf", StoreInfo.class);
    assertNotNull(targetStore);

    ImportController ctrl = new ImportController(getGeoServer(), applicationContext);

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setContextPath("/geoserver");
    request.setRequestURI("/geoserver/hello");
    request.setMethod("post");

    createMultiPartFormContent(request, "form-data; name=\"upload\"; filename=\"point.json\"",
            "application/json", IOUtils.toByteArray(getClass().getResourceAsStream("point.json")));

    JSONObj result = ctrl.importFile("sf", "sf", request);
    Long id = Long.parseLong(result.str("id"));

    //Wait for the import to complete
    result = pollImport(ctrl, "gs", id, "pending", request);
    assertNotNull(result);
    result = ctrl.update("gs", id, getUpdateTasks(result), request);
    result = pollImport(ctrl, "gs", id, "complete", request);
    assertNotNull(result);

    assertEquals(1, result.array("tasks").size());
    JSONObj obj = result.array("tasks").object(0);
    assertEquals("COMPLETE", obj.get("status"));

    assertEquals("sf", obj.object("layer").str("workspace"));
    assertEquals("point", obj.object("layer").str("name"));

    LayerInfo l = catalog.getLayerByName("sf:point");
    assertNotNull(l);
    FeatureTypeInfo f = (FeatureTypeInfo) l.getResource();
    assertEquals(targetStore, f.getStore());

    // ensure style in workspace
    StyleInfo s = l.getDefaultStyle();
    assertNotNull(s.getWorkspace());
}

From source file:com.boundlessgeo.geoserver.AppIntegrationTest.java

@Test
public void testListAttributes() throws Exception {
    Catalog cat = getCatalog();/*from   www  .  j a  va 2s  .  c  om*/

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setContextPath("/geoserver");
    request.setRequestURI("/geoserver/hello");
    request.setMethod("get");

    StoreController ctrl = new StoreController(getGeoServer());
    JSONObj obj = ctrl.attributes("sf", "sf", "PrimitiveGeoFeature", 10, request);
    JSONArr attributes = obj.object("schema").array("attributes");
    JSONArr values = obj.array("values");

    //Page through actual features and ensure values match what is returned
    StoreInfo store = cat.getStoreByName("sf", "sf", StoreInfo.class);
    DataAccess data = ((DataStoreInfo) store).getDataStore(new NullProgressListener());
    FeatureSource source = data.getFeatureSource(new NameImpl("PrimitiveGeoFeature"));

    Query query = new Query(Query.ALL);
    query.setMaxFeatures(10);
    FeatureIterator features = source.getFeatures(query).features();
    int featureIndex = 0;
    while (features.hasNext()) {
        Feature feature = features.next();
        JSONArr featureJSON = (JSONArr) values.at(featureIndex);
        Property[] properties = feature.getProperties().toArray(new Property[feature.getProperties().size()]);

        for (int i = 0; i < attributes.size(); i++) {

            //Verify the schema matches the feature
            JSONObj attribute = attributes.object(i);
            assertEquals(attribute.get("type"),
                    properties[i].getDescriptor().getType().getBinding().getSimpleName());
            assertEquals(attribute.get("name"), properties[i].getDescriptor().getName().getLocalPart());

            //Verify the value matches the feature
            assertEquals(featureJSON.str(i),
                    properties[i].getValue() == null ? null : properties[i].getValue().toString());
        }
        featureIndex++;
    }
}

From source file:org.cateproject.test.functional.mockmvc.HtmlUnitRequestBuilder.java

private void contextPath(MockHttpServletRequest result, UriComponents uriComponents) {
    /*if (contextPath == null) {
       List<String> pathSegments = uriComponents.getPathSegments();
       if (pathSegments.isEmpty()) {//  w  w  w. j  a  v a  2s . c  om
    result.setContextPath("");
       }
       else {
    result.setContextPath("/" + pathSegments.get(0));
       }
    }
    else {
       if (!uriComponents.getPath().startsWith(contextPath)) {
    throw new IllegalArgumentException(uriComponents.getPath() + " should start with contextPath "
          + contextPath);
       }
       result.setContextPath(contextPath);
    }*/
    result.setContextPath("");
}

From source file:com.boundlessgeo.geoserver.AppIntegrationTest.java

@Test
public void testIconsUploadDelete() throws Exception {
    Catalog catalog = getCatalog();//www  . ja  va2 s.  c  o m
    IconController ctrl = new IconController(getGeoServer());

    // test upload
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setContextPath("/geoserver");
    request.setRequestURI("/geoserver/api/icons/cite");
    request.setMethod("post");

    createMultiPartFormContent(request, "form-data; name=\"icon\"; filename=\"STYLE.PROPERTIES\"",
            "text/x-java-properties", "square=LINESTRING((0 0,0 1,1 1,1 0,0 0))".getBytes());

    JSONArr arr = ctrl.create("cite", request);
    assertEquals(1, arr.size());

    Resource r = catalog.getResourceLoader().get("workspaces/cite/styles/STYLE.PROPERTIES");
    assertEquals("created", Resource.Type.RESOURCE, r.getType());

    // test delete
    MockHttpServletRequestBuilder delete = delete("/api/icons/cite/icon.png");
    ctrl.delete("cite", "STYLE.PROPERTIES");

    r = catalog.getResourceLoader().get("workspaces/cite/styles/STYLE.PROPERTIES");
    assertEquals("deleted", Resource.Type.UNDEFINED, r.getType());

    //Global style directory

    // test upload
    request = new MockHttpServletRequest();
    request.setContextPath("/geoserver");
    request.setRequestURI("/geoserver/api/icons");
    request.setMethod("post");

    createMultiPartFormContent(request, "form-data; name=\"icon\"; filename=\"STYLE.PROPERTIES\"",
            "text/x-java-properties", "square=LINESTRING((0 0,0 1,1 1,1 0,0 0))".getBytes());

    arr = ctrl.create(request);
    assertEquals(1, arr.size());

    r = catalog.getResourceLoader().get("styles/STYLE.PROPERTIES");
    assertEquals("created", Resource.Type.RESOURCE, r.getType());

    // test delete
    delete = delete("/api/icons/icon.png");
    ctrl.delete("STYLE.PROPERTIES");

    r = catalog.getResourceLoader().get("styles/STYLE.PROPERTIES");
    assertEquals("deleted", Resource.Type.UNDEFINED, r.getType());
}