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:ar.com.zauber.commons.web.proxy.impl.PathBasedURLRequestMapperTest.java

/** @throws Exception on error */
public final void testServletContext() throws Exception {
    final String ctxPath = "/nexusaaa-0.0";
    final String servletContext = "/bin";

    final PathBasedURLRequestMapper mapper = new PathBasedURLRequestMapper(
            new InmutableURLRequestMapper(new InmutableURLResult(new URL(base))));
    mapper.setStripContextPath(true);/*from   ww w .j  a va 2  s . c o  m*/
    mapper.setStripServletPath(true);

    final MockServletContext ctx = new MockServletContext();

    final MockHttpServletRequest request = new MockHttpServletRequest(ctx, "GET",
            ctxPath + servletContext + "/");
    request.setContextPath(ctxPath);
    request.setServletPath(servletContext);
    assertEquals(new URL(base + "/"), mapper.getProxiedURLFromRequest(request).getURL());
}

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

/** @throws Exception on error */
public final void testNoServletContext() throws Exception {
    final String ctxPath = "/nexusaaa-0.0";
    final String servletContext = "/bin";

    final PathBasedURLRequestMapper mapper = new PathBasedURLRequestMapper(
            new InmutableURLRequestMapper(new InmutableURLResult(new URL(base))));
    mapper.setStripContextPath(false);//w w w  . j  a  va 2s.  c  o  m
    mapper.setStripServletPath(false);

    final MockServletContext ctx = new MockServletContext();

    final MockHttpServletRequest request = new MockHttpServletRequest(ctx, "GET",
            ctxPath + servletContext + "/");
    request.setContextPath(ctxPath);
    request.setServletPath(servletContext);
    assertEquals(new URL(base + ctxPath + servletContext + "/"),
            mapper.getProxiedURLFromRequest(request).getURL());
}

From source file:org.jasig.cas.web.flow.InitialFlowSetupActionTests.java

public void testSettingContextPath() throws Exception {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final String CONST_CONTEXT_PATH = "/test";
    request.setContextPath(CONST_CONTEXT_PATH);
    final MockRequestContext context = new MockRequestContext();
    context.setExternalContext(/*w w  w. j  a v a2  s  .  c  om*/
            new ServletExternalContext(new MockServletContext(), request, new MockHttpServletResponse()));

    this.action.doExecute(context);

    assertEquals(CONST_CONTEXT_PATH + "/", this.warnCookieGenerator.getCookiePath());
    assertEquals(CONST_CONTEXT_PATH + "/", this.tgtCookieGenerator.getCookiePath());
}

From source file:fragment.web.AccessDecisionTest.java

private MockHttpServletRequest createRequest(String uri) {
    MockHttpServletRequest mockRequest = getRequestTemplate(HttpMethod.GET, uri);
    mockRequest.setContextPath("/portal");
    mockRequest.setServletPath(null);/*from  ww w  . j a  v a  2s.  co m*/
    return mockRequest;
}

From source file:org.cloudfoundry.identity.uaa.security.web.UaaRequestMatcherTests.java

private MockHttpServletRequest request(String path, String accept, String... parameters) {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setContextPath("/ctx");
    request.setRequestURI("/ctx" + path);
    if (accept != null) {
        request.addHeader("Accept", accept);
    }//from   w ww  .  jav a2 s  . c o  m
    for (int i = 0; i < parameters.length; i += 2) {
        String key = parameters[i];
        String value = parameters[i + 1];
        request.addParameter(key, value);
    }
    return request;
}

From source file:org.cloudfoundry.identity.uaa.login.EmailResetPasswordServiceTests.java

@Test
public void testForgotPasswordWhenAResetCodeIsReturnedByTheUaa() throws Exception {
    mockUaaServer.expect(requestTo("http://uaa.example.com/uaa/password_resets")).andExpect(method(POST))
            .andRespond(withSuccess("{\"code\":\"the_secret_code\",\"user_id\":\"user-id-001\"}",
                    APPLICATION_JSON));/*  ww  w . j a  v a2s  .co m*/

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setProtocol("http");
    request.setContextPath("/login");
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));

    emailResetPasswordService.forgotPassword("user@example.com");

    mockUaaServer.verify();

    Mockito.verify(messageService).sendMessage(eq("user-id-001"), eq("user@example.com"),
            eq(MessageType.PASSWORD_RESET), eq("Pivotal account password reset request"), contains(
                    "<a href=\"http://localhost/login/reset_password?code=the_secret_code&amp;email=user%40example.com\">Reset your password</a>"));
}

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

private String runServletWithConcepts(Concept... concepts) throws Exception {
    DownloadDictionaryServlet downloadServlet = new DownloadDictionaryServlet();
    MockHttpServletRequest request = new MockHttpServletRequest("GET",
            "/moduleServlet/legacyui/downloadDictionaryServlet");
    request.setContextPath("/somecontextpath");
    MockHttpServletResponse response = new MockHttpServletResponse();

    List<Concept> conceptList = Arrays.asList(concepts);
    Mockito.when(conceptService.conceptIterator()).thenReturn(conceptList.iterator());

    downloadServlet.service(request, response);
    return response.getContentAsString();
}

From source file:org.openmrs.module.atomfeed.web.AtomFeedDownloadServletTest.java

/**
 * @see AtomFeedDownloadServlet#doHead(javax.servlet.http.HttpServletRequest,
 *      javax.servlet.http.HttpServletResponse)
 * @verifies send valid headers if atom feed has changed
 *///  w w w . ja va 2 s.  c o m
@Test
public void doHead_shouldSendValidHeadersIfAtomFeedHasChanged() throws Exception {
    // create servlet and corresponding request and response object to be sent
    AtomFeedDownloadServlet atomFeedDownloadServlet = new AtomFeedDownloadServlet();
    MockHttpServletRequest request = new MockHttpServletRequest("HEAD", "/atomfeed");
    request.setContextPath("/somecontextpath");
    MockHttpServletResponse response = new MockHttpServletResponse();

    // intentionally change atom feed in order to not depend from other tests
    AtomFeedUtil.objectCreated(new Encounter());

    String etagToken = "somevalue";
    Date lastModified = new Date();
    // set request headers 
    request.addHeader("If-None-Match", '"' + etagToken + '"');
    request.addHeader("If-Modified-Since", lastModified);

    atomFeedDownloadServlet.service(request, response);
    // check response headers
    Assert.assertNotSame(0, response.getContentLength());
    Assert.assertEquals("application/atom+xml", response.getContentType());
    Assert.assertNotSame(HttpServletResponse.SC_NOT_MODIFIED, response.getStatus());
    Assert.assertNotSame('"' + etagToken + '"', response.getHeader("Etag"));
    Assert.assertNotNull(response.getHeader("Last-Modified"));
}

From source file:ar.com.zauber.commons.web.uri.assets.AssetsTest.java

/** create request */
private MockHttpServletRequest createRequest(final XmlWebApplicationContext ctx) {
    final MockHttpServletRequest request = new MockHttpServletRequest("GET",
            "/foo/organizations/zauber/projects");
    request.setContextPath("/foo");
    // esto se requiere para que funcione el buscar el ctx dado un request 
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, ctx);
    final ServletRequestAttributes attributes = new ServletRequestAttributes(request);
    request.setAttribute(RequestContextListener.class.getName() + ".REQUEST_ATTRIBUTES", attributes);
    LocaleContextHolder.setLocale(request.getLocale());
    RequestContextHolder.setRequestAttributes(attributes);
    return request;
}

From source file:org.openmrs.module.atomfeed.web.AtomFeedDownloadServletTest.java

/**
 * @see AtomFeedDownloadServlet#doHead(javax.servlet.http.HttpServletRequest,
 *      javax.servlet.http.HttpServletResponse)
 * @verifies send not modified error if atom feed has not changed
 *//*from w  ww.  j  av  a 2  s .c o  m*/
@Test
public void doHead_shouldSendNotModifiedErrorIfAtomFeedHasNotChanged() throws Exception {
    // create servlet and corresponding request and response object to be sent
    AtomFeedDownloadServlet atomFeedDownloadServlet = new AtomFeedDownloadServlet();
    MockHttpServletRequest request = new MockHttpServletRequest("HEAD", "/atomfeed");
    request.setContextPath("/somecontextpath");
    MockHttpServletResponse response = new MockHttpServletResponse();

    // intentionally change atom feed in order to not depend from other tests
    AtomFeedUtil.objectCreated(new Encounter());

    // read atom feed header specific information from the header file
    String headerFileContent = AtomFeedUtil.readFeedHeaderFile();
    int contentLength = 0;
    String etagToken = "";
    Date lastModified = null;
    if (StringUtils.isNotBlank(headerFileContent)) {
        contentLength = headerFileContent.length() + Integer
                .valueOf(StringUtils.substringBetween(headerFileContent, "<entriesSize>", "</entriesSize>"));
        etagToken = StringUtils.substringBetween(headerFileContent, "<versionId>", "</versionId>");
        try {
            lastModified = new SimpleDateFormat(AtomFeedUtil.RFC_3339_DATE_FORMAT)
                    .parse(StringUtils.substringBetween(headerFileContent, "<updated>", "</updated>"));
        } catch (ParseException e) {
            // ignore it here
        }
    }
    // set request headers 
    request.addHeader("If-None-Match", '"' + etagToken + '"');
    request.addHeader("If-Modified-Since", lastModified);

    atomFeedDownloadServlet.service(request, response);
    // check response headers
    Assert.assertEquals(contentLength, response.getContentLength());
    Assert.assertEquals("application/atom+xml", response.getContentType());
    Assert.assertEquals(HttpServletResponse.SC_NOT_MODIFIED, response.getStatus());
    Assert.assertNotNull(response.getHeader("Last-Modified"));
}