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:com.google.api.server.spi.auth.EndpointsPeerAuthenticatorTest.java

private static MockHttpServletRequest createRequest(String host, int port, String servletPath,
        String contextPath, String queryString) {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader("Host", host);
    request.setServerName(host);//from   ww w. j  ava  2 s.co m
    request.setServerPort(port);
    request.setServletPath(servletPath);
    request.setQueryString(queryString);
    request.setContextPath(contextPath);
    return request;
}

From source file:com.trenako.web.tags.html.HtmlTagTests.java

@Test
public void shouldRenderALink() {
    String html = "";

    html = a("link").href("http://localhost").title("title").build();

    assertEquals("<a href=\"http://localhost\" title=\"title\">link</a>", html);

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setContextPath("http://localhost:8080/app");

    html = a("link").href(request, "/path/name").build();
    assertEquals("<a href=\"http://localhost:8080/app/path/name\">link</a>", html);
}

From source file:ar.com.zauber.commons.web.proxy.impl.InmutableURLRequestMapperTest.java

/** @throws Exception on error */
public final void testAny() throws Exception {
    final URL url = new URL("http://127.0.0.1/foo/bar");
    final URLRequestMapper mapper = new InmutableURLRequestMapper(new InmutableURLResult(url));
    final String ctxPath = "/nexusaaa-0.0";
    final String servletContext = "/bin";

    final MockHttpServletRequest request = new MockHttpServletRequest("GET", ctxPath + servletContext + "/");
    request.setContextPath(ctxPath);
    request.setServletPath(servletContext);

    assertEquals(url, mapper.getProxiedURLFromRequest(request).getURL());
}

From source file:org.openmrs.web.servlet.LoginServletTest.java

/**
 * The servlet should send the user back to the login box if the user enters the wrong username
 * or password.//from  w ww .j  av  a 2 s .co m
 * 
 * @throws Exception
 */
@Test
public void shouldRedirectBackToLoginScreenOnBadUsernameAndPassword() throws Exception {
    LoginServlet loginServlet = new LoginServlet();
    MockHttpServletRequest request = new MockHttpServletRequest("POST", "/loginServlet");
    request.setContextPath("/somecontextpath");
    MockHttpServletResponse response = new MockHttpServletResponse();

    request.setParameter("uname", "some wrong username");
    request.setParameter("pw", "some wrong password");

    loginServlet.service(request, response);

    Assert.assertEquals("/somecontextpath/login.htm", response.getRedirectedUrl());
}

From source file:org.openmrs.web.servlet.LoginServletTest.java

/**
 * If a user logs in correctly, they should never be redirected back to the login screen because
 * this would cause confusion/* ww w . j ava 2  s.  com*/
 * 
 * @throws Exception
 */
@Test
public void shouldNotRedirectBackToLoginScreenWithCorrectUsernameAndPassword() throws Exception {
    // this test depends on being able to log in as "admin:test".
    Context.logout();
    Context.authenticate("admin", "test");
    Assert.assertTrue(Context.isAuthenticated());

    // do the test now
    LoginServlet loginServlet = new LoginServlet();
    MockHttpServletRequest request = new MockHttpServletRequest("POST", "/loginServlet");
    request.setContextPath("/somecontextpath");
    MockHttpServletResponse response = new MockHttpServletResponse();

    request.setParameter("uname", "admin");
    request.setParameter("pw", "test");

    loginServlet.service(request, response);

    Assert.assertNotSame("/somecontextpath/login.htm", response.getRedirectedUrl());
}

From source file:ar.com.zauber.commons.web.cache.impl.filter.matchers.AntRequestMatcherTest.java

/**
 * Test method for {@link AntRequestMatcher#matches(HttpServletRequest)}.
 *//*from w  w w . ja  va 2s.c  o m*/
@Test
public final void testMatches() {
    AntRequestMatcher matcher = new AntRequestMatcher("/servlet/path/{variable}");
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/context/servlet/path/pepe");
    request.setServletPath("/servlet");
    request.setContextPath("/context");
    MatchData matches = matcher.matches(request);
    assertThat(matches, notNullValue());
    assertThat((String) matches.get("variable"), equalTo("pepe"));
}

From source file:ar.com.zauber.commons.web.cache.impl.filter.matchers.AntRequestMatcherTest.java

/**
 * Test method for {@link AntRequestMatcher#matches(HttpServletRequest)}.
 *///w ww . j  a v  a 2 s .co m
@Test
public final void testMatchesDecodes() {
    AntRequestMatcher matcher = new AntRequestMatcher("/servlet/path/{variable}");
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/context/servlet/path/pepe%20parada");
    request.setServletPath("/servlet");
    request.setContextPath("/context");
    MatchData matches = matcher.matches(request);
    assertThat(matches, notNullValue());
    assertThat((String) matches.get("variable"), equalTo("pepe parada"));
}

From source file:org.openmrs.web.servlet.LoginServletTest.java

/**
 * The lockout value is set to five//w  w  w . j  a  va 2  s. co  m
 * 
 * @throws Exception
 */
@Test
public void shouldLockUserOutAfterFiveFailedLoginAttempts() throws Exception {
    // this test depends on being able to log in as "admin:test".
    Context.logout();
    Context.authenticate("admin", "test");
    Assert.assertTrue(Context.isAuthenticated());

    // do the test now
    LoginServlet loginServlet = new LoginServlet();

    for (int x = 1; x < 4; x++) {
        MockHttpServletRequest request = new MockHttpServletRequest("POST", "/loginServlet");
        request.setContextPath("/somecontextpath");
        MockHttpServletResponse response = new MockHttpServletResponse();

        // change the username everytime so that we're not  
        // accidentally testing against the API lockout
        request.setParameter("uname", "wrong username" + x);
        request.setParameter("pw", "wrong password");

        loginServlet.service(request, response);
    }

    // now attempting to log in the fifth time should fail 
    MockHttpServletRequest request = new MockHttpServletRequest("POST", "/loginServlet");
    request.setContextPath("/somecontextpath");
    MockHttpServletResponse response = new MockHttpServletResponse();
    request.setParameter("uname", "admin");
    request.setParameter("pw", "test");
    loginServlet.service(request, response);

    Assert.assertNotSame("/somecontextpath/login.htm", response.getRedirectedUrl());
}

From source file:org.jasig.cas.client.util.CasFilterTests.java

@Test
public void serverName() {
    final String serverNameWithoutSlash = "http://www.cnn.com";
    final String serverNameWithSlash = "http://www.cnn.com/";

    final TestCasFilter testCasFilter = new TestCasFilter();
    testCasFilter.setServerName(serverNameWithoutSlash);

    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    request.setContextPath("/cas");
    request.setRequestURI("/cas/test");

    assertTrue(testCasFilter.constructServiceUrl(request, response).startsWith("http://www.cnn.com/cas/test"));

    testCasFilter.setServerName(serverNameWithSlash);
    assertTrue(testCasFilter.constructServiceUrl(request, response).startsWith("http://www.cnn.com/cas/test"));
}

From source file:ar.com.zauber.commons.web.proxy.impl.PathBasedURLRequestMapperTest.java

/** @throws Exception on error */
public final void testResultDelegate() throws Exception {
    final PathBasedURLRequestMapper mapper = new PathBasedURLRequestMapper(
            new InmutableURLRequestMapper(new InmutableURLResult()));
    final MockServletContext ctx = new MockServletContext();

    final String ctxPath = "/nexusaaa-0.0";
    final String servletContext = "/bin";

    final MockHttpServletRequest request = new MockHttpServletRequest(ctx, "GET",
            ctxPath + servletContext + "/");
    request.setContextPath(ctxPath);
    request.setServletPath(servletContext);

    assertFalse(mapper.getProxiedURLFromRequest(request).hasResult());
}