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:org.openmrs.module.referenceapplication.page.controller.LoginPageControllerTest.java

/**
 * @see LoginPageController#get(org.openmrs.ui.framework.page.PageModel,
 *      org.openmrs.ui.framework.UiUtils, org.openmrs.ui.framework.page.PageRequest, String,
 *      org.openmrs.api.LocationService,
 *      org.openmrs.module.appframework.service.AppFrameworkService)
 *//*w ww. j  a  va2  s .  c  om*/
@Test
@Verifies(value = "should set redirectUrl in the page model if any was specified in the session", method = "get(PageModel,UiUtils,PageRequest)")
public void get_shouldSetRedirectUrlInThePageModelIfAnyWasSpecifiedInTheSession() throws Exception {
    when(Context.isAuthenticated()).thenReturn(false);

    String redirectUrl = TEST_CONTEXT_PATH + "/referenceapplication/patient.page";
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setContextPath(TEST_CONTEXT_PATH);
    PageRequest pageRequest = createPageRequest(request, null);
    HttpSession httpSession = new MockHttpSession();
    httpSession.setAttribute(SESSION_ATTRIBUTE_REDIRECT_URL, redirectUrl);
    request.setSession(httpSession);

    PageModel pageModel = new PageModel();
    new LoginPageController().get(pageModel, uiUtils, pageRequest, null, null, appFrameworkService);

    assertEquals(redirectUrl, pageModel.get(REQUEST_PARAMETER_NAME_REDIRECT_URL));
}

From source file:org.ambraproject.wombat.service.remote.SolrSearchApiTest.java

@Test
public void testAddArticleLinks() throws IOException {
    SolrSearchApi solrSearchApiForTest = new SearchApiForAddArticleLinksTest();
    Map<String, List<Map>> searchResults = new HashMap<>();
    List<Map> docs = new ArrayList<>(1);
    Map doc = new HashMap();
    List<String> crossPubbedJournals = new ArrayList<>(1);
    crossPubbedJournals.add("journal1Key");
    doc.put("id", "12345");
    doc.put("eissn", "123");
    docs.add(doc);//from w ww. j a v  a  2  s.com
    searchResults.put("docs", docs);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setContextPath("someContextPath");
    Site site = MockSiteUtil.getByUniqueJournalKey(siteSet, "journal2Key");

    Map<?, ?> actual = solrSearchApiForTest.addArticleLinks(searchResults, request, site, siteSet);
    List<Map> actualDocs = (List) actual.get("docs");
    assertEquals(actualDocs.size(), 1);
    Map actualDoc = (Map) actualDocs.get(0);
    assertEquals(actualDoc.get("id"), "12345");
    assertTrue(actualDoc.get("link").toString().endsWith("someContextPath/site1/article?id=12345"));
}

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

@Test
public void getRequestExecutableName_include_urlPath_noServletPath() throws Exception {
    MockDispathServlet servlet = new MockDispathServlet(servletContext, servletInitParameters);
    initServlet(servlet);/*  w w w  .j  a  v  a  2  s. c o  m*/

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

    request.setMethod("POST");
    request.setContextPath(CONTEXT_PATH);
    request.setAttribute(AbstractTargetHandler.INCLUDE_PATH_INFO_ATTRIBUTE, "/test/test");

    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_endBackslash() throws Exception {
    MockDispathServlet servlet = new MockDispathServlet(servletContext, servletInitParameters);
    initServlet(servlet);/* ww  w .  j a  va  2  s  .  c  om*/

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

    request.setMethod("POST");
    request.setContextPath(CONTEXT_PATH);
    request.setAttribute(AbstractTargetHandler.INCLUDE_PATH_INFO_ATTRIBUTE, "/test/test/");

    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_include_urlPath_withServletPath() throws Exception {
    MockDispathServlet servlet = new MockDispathServlet(servletContext, servletInitParameters);
    initServlet(servlet);/*  www.j  a  v a2  s .  c o  m*/

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

    request.setMethod("POST");
    request.setContextPath(CONTEXT_PATH);
    request.setAttribute(AbstractTargetHandler.INCLUDE_PATH_INFO_ATTRIBUTE, "/test");
    request.setAttribute(AbstractTargetHandler.INCLUDE_SERVLET_PATH_ATTRIBUTE, "/test");

    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_urlPath() throws Exception {
    MockDispathServlet servlet = new MockDispathServlet(servletContext, servletInitParameters);
    initServlet(servlet);//from ww w . ja  v  a  2  s.co 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);// w  w w  .  jav  a2s .c  om

    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: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 ww.j  a  v a 2  s. c om*/
        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:org.soybeanMilk.test.unit.web.TestDispatchServlet.java

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

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

        request.setMethod("POST");
        request.setContextPath(CONTEXT_PATH);
        request.setAttribute(AbstractTargetHandler.INCLUDE_SERVLET_PATH_ATTRIBUTE, "/test/test.do");

        servlet.service(request, response);

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

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);
    request.setPathInfo(pathInfo);//from w  w w. j  av  a 2s.  co  m
    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) {

    }
}