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:it.geosolutions.opensdi2.configurations.ConfigurationTest.java

@Test
public void nullOrEmptyInstanceIDTest() throws OSDIConfigurationException {
    MockHttpServletRequest req = new MockHttpServletRequest();
    MockModule mm = new MockModule();
    MockModule2 mm2 = new MockModule2();
    MockModule3 mm3 = new MockModule3();
    MockModule4 mm4 = new MockModule4();

    req.setPathInfo("moduleid/instanceid");
    mm.setDepot(depot);/*from  ww w .j  a v  a2  s  .  co m*/
    mm.loadConfiguration(req);

    req.setPathInfo("moduleid/instanceid");
    mm2.setDepot(depot);
    mm2.loadConfiguration(req);

    req.setPathInfo("moduleid/instanceid");
    mm3.setDepot(depot);
    boolean ex = false;
    try {
        mm3.loadConfiguration(req);
    } catch (IllegalArgumentException iae) {
        ex = true;
    }
    if (!ex) {
        fail();
    }

    req.setPathInfo("moduleid/instanceid");
    mm4.setDepot(depot);
    mm4.loadConfiguration(req);
}

From source file:org.soybeanMilk.test.unit.web.TestDispatchServlet.java

@Test
public void getRequestExecutableName_userRequest_urlPath() throws Exception {
    MockDispathServlet servlet = new MockDispathServlet(servletContext, servletInitParameters);
    initServlet(servlet);/*from  ww w. ja va 2s .  c o  m*/

    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();

    request.setMethod("POST");
    request.setContextPath(CONTEXT_PATH);

    request.setPathInfo("/test/test");
    request.setServletPath("");

    servlet.service(request, response);

    Assert.assertEquals(TestResolver.RESULT, (String) request.getAttribute("result"));
}

From source file:org.soybeanMilk.test.unit.web.TestDispatchServlet.java

@Test
public void getRequestExecutableName_userRequest_urlSuffix() throws Exception {
    MockDispathServlet servlet = new MockDispathServlet(servletContext, servletInitParameters);
    initServlet(servlet);//from w ww  .  j  a  va2 s.  c  o m

    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();

    request.setMethod("POST");
    request.setContextPath(CONTEXT_PATH);

    request.setPathInfo("/test/test.do");
    request.setServletPath("");

    servlet.service(request, response);

    Assert.assertEquals(TestResolver.RESULT, (String) request.getAttribute("result"));
}

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

private void servletPath(UriComponents uriComponents, MockHttpServletRequest result) {
    if ("".equals(result.getPathInfo())) {
        result.setPathInfo(null);
    }/*from   w w w. j  a v  a 2  s.co  m*/
    servletPath(result, uriComponents.getPath());
}

From source file:org.fao.geonet.api.records.formatters.FormatterApiIntegrationTest.java

@Test
public void testExec() throws Exception {
    final ListFormatters.FormatterDataResponse formatters = listService.exec(null, null, schema, false, false);
    for (ListFormatters.FormatterData formatter : formatters.getFormatters()) {
        MockHttpServletRequest request = new MockHttpServletRequest();
        request.getSession();//from w  w w . j a  va 2s.  c o  m
        request.setPathInfo("/eng/blahblah");
        MockHttpServletResponse response = new MockHttpServletResponse();
        final String srvAppContext = "srvAppContext";
        request.getServletContext().setAttribute(srvAppContext, applicationContext);
        JeevesDelegatingFilterProxy.setApplicationContextAttributeKey(srvAppContext);
        RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));

        formatService.exec("eng", "html", "" + id, null, formatter.getId(), "true", false, _100,
                new ServletWebRequest(request, response));

        final String view = response.getContentAsString();
        try {
            assertFalse(formatter.getSchema() + "/" + formatter.getId(), view.isEmpty());
        } catch (Throwable e) {
            e.printStackTrace();
            fail(formatter.getSchema() + " > " + formatter.getId());
        }
        try {
            response = new MockHttpServletResponse();
            formatService.exec("eng", "testpdf", "" + id, null, formatter.getId(), "true", false, _100,
                    new ServletWebRequest(request, response));
            //                Files.write(Paths.get("e:/tmp/view.pdf"), response.getContentAsByteArray());
            //                System.exit(0);
        } catch (Throwable t) {
            t.printStackTrace();
            fail(formatter.getSchema() + " > " + formatter.getId());
        }
    }
}

From source file:com.liferay.document.library.webdav.test.BaseWebDAVTestCase.java

public Tuple service(String method, String path, Map<String, String> headers, byte[] data) {

    WebDAVServlet webDAVServlet = new WebDAVServlet();

    String requestURI = _CONTEXT_PATH + _SERVLET_PATH + _PATH_INFO_PREFACE + path;

    MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest(method, requestURI);

    mockHttpServletRequest.setContextPath(_CONTEXT_PATH);
    mockHttpServletRequest.setServletPath(_SERVLET_PATH);
    mockHttpServletRequest.setPathInfo(_PATH_INFO_PREFACE + path);

    try {/*from   w w  w  .j  av a2s  .  c o m*/
        mockHttpServletRequest.setRemoteUser(String.valueOf(TestPropsValues.getUserId()));
    } catch (Exception e) {
        Assert.fail("User ID cannot be initialized");
    }

    if (headers == null) {
        headers = new HashMap<>();
    }

    headers.put(HttpHeaders.USER_AGENT, getUserAgent());

    try {
        throw new Exception();
    } catch (Exception e) {
        StackTraceElement[] stackTraceElements = e.getStackTrace();

        for (StackTraceElement stackTraceElement : stackTraceElements) {
            String methodName = stackTraceElement.getMethodName();

            if (methodName.equals("setUp") || methodName.equals("tearDown") || methodName.startsWith("test")) {

                String testName = StringUtil.extractLast(stackTraceElement.getClassName(), CharPool.PERIOD);

                testName = StringUtil.removeSubstrings(testName, "WebDAV", "Test");

                headers.put("X-Litmus", testName + ": (" + stackTraceElement.getMethodName() + ":"
                        + stackTraceElement.getLineNumber() + ")");

                break;
            }
        }
    }

    if (data != null) {
        mockHttpServletRequest.setContent(data);

        String contentType = headers.remove(HttpHeaders.CONTENT_TYPE);

        if (contentType != null) {
            mockHttpServletRequest.setContentType(contentType);
        } else {
            mockHttpServletRequest.setContentType(ContentTypes.TEXT_PLAIN);
        }
    }

    for (Map.Entry<String, String> entry : headers.entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();

        mockHttpServletRequest.addHeader(key, value);
    }

    try {
        MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();

        webDAVServlet.service(mockHttpServletRequest, mockHttpServletResponse);

        int statusCode = mockHttpServletResponse.getStatus();
        byte[] responseBody = mockHttpServletResponse.getContentAsByteArray();

        Map<String, String> responseHeaders = new HashMap<>();

        for (String name : mockHttpServletResponse.getHeaderNames()) {
            responseHeaders.put(name, mockHttpServletResponse.getHeader(name));
        }

        return new Tuple(statusCode, responseBody, responseHeaders);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:com.doitnext.http.router.RestRouterServletTest.java

private void setUpRequest(Object[] testCase, MockHttpServletRequest request) {
    String httpMethod = (String) testCase[0];
    String pathPrefix = (String) testCase[1];
    String pathInfo = (String) testCase[2];
    String queryString = (String) testCase[3];
    String parts[] = queryString.split("&");
    String acceptHeader = (String) testCase[4];
    String contentTypeHeader = (String) testCase[5];

    request.setServletPath("");
    request.setContextPath(pathPrefix);//  w w  w  . j a va  2  s  .  c  om
    request.setPathInfo(pathInfo);
    request.setMethod(httpMethod);
    request.setQueryString(queryString);
    for (String part : parts) {
        String pieces[] = part.split("=");
        if (pieces.length > 1)
            request.addParameter(pieces[0], pieces[1]);
    }
    if (acceptHeader != null)
        request.addHeader("Accept", acceptHeader);
    if (contentTypeHeader != null)
        request.setContentType(contentTypeHeader);
    HttpMethod mthd = HttpMethod.valueOf(httpMethod);
    if (mthd == HttpMethod.POST || mthd == HttpMethod.PUT) {

    }
}

From source file:org.fao.geonet.api.records.formatters.FormatterApiIntegrationTest.java

@Test
public void testExecXslt() throws Exception {
    final ServletContext context = _applicationContext.getBean(ServletContext.class);
    MockHttpServletRequest request = new MockHttpServletRequest(context, "GET",
            "http://localhost:8080/geonetwork/srv/eng/md.formatter");
    request.getSession();//from   w  w w.  j  a v a 2  s .  com
    request.setPathInfo("/eng/md.formatter");

    final String applicationContextAttributeKey = "srv";
    request.getServletContext().setAttribute(applicationContextAttributeKey, _applicationContext);
    ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);

    RequestContextHolder.setRequestAttributes(requestAttributes);
    final String formatterName = "xsl-test-formatter";
    final URL testFormatterViewFile = FormatterApiIntegrationTest.class
            .getResource(formatterName + "/view.xsl");
    final Path testFormatter = IO.toPath(testFormatterViewFile.toURI()).getParent();
    final Path formatterDir = this.dataDirectory.getFormatterDir();
    Files.deleteIfExists(formatterDir.resolve("functions.xsl"));
    IO.copyDirectoryOrFile(testFormatter, formatterDir.resolve(formatterName), false);
    IO.copyDirectoryOrFile(testFormatter.getParent().resolve("functions.xsl"), formatterDir, true);
    JeevesDelegatingFilterProxy.setApplicationContextAttributeKey(applicationContextAttributeKey);

    final MockHttpServletResponse response = new MockHttpServletResponse();
    formatService.exec("eng", "html", "" + id, null, formatterName, "true", false, _100,
            new ServletWebRequest(request, response));
    final String viewXml = response.getContentAsString();
    final Element view = Xml.loadString(viewXml, false);
    assertEqualsText("fromFunction", view, "*//p");
    assertEqualsText("Title", view, "*//div[@class='tr']");
}

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

private void initMockHttpRequest(MockHttpServletRequest req, URI uri) {

    req.setRequestURI(uri.toString());/*from  www.jav a2  s. com*/
    req.setPathInfo(uri.getPath());
    req.setRemotePort(uri.getPort());
    req.setRemoteHost(uri.getHost());
    req.setScheme(uri.getScheme());
    req.setContextPath("/");
}

From source file:org.fenixedu.bennu.oauth.OAuthServletTest.java

@Test
public void getServiceAccessTokenWithWrongGrantTypeTest() {
    MockHttpServletRequest req = new MockHttpServletRequest();
    MockHttpServletResponse res = new MockHttpServletResponse();

    req.addParameter("client_id", serviceApplication.getExternalId());
    req.addParameter("client_secret", serviceApplication.getSecret());
    req.addParameter("grant_type", "authorization_code");
    req.setMethod("POST");
    req.setPathInfo("/access_token");

    try {/*  w  w w . ja v a  2  s . c o  m*/
        oauthServlet.service(req, res);

        Assert.assertEquals("must return status BAD_REQUEST", 400, res.getStatus());

    } catch (ServletException | IOException e) {
        Assert.fail(e.getMessage());
    }
}