Example usage for org.springframework.mock.web MockServletContext MockServletContext

List of usage examples for org.springframework.mock.web MockServletContext MockServletContext

Introduction

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

Prototype

public MockServletContext() 

Source Link

Document

Create a new MockServletContext , using no base path and a DefaultResourceLoader (i.e.

Usage

From source file:org.springframework.boot.actuate.autoconfigure.security.servlet.ManagementWebSecurityAutoConfigurationTests.java

private int getResponseStatus(AssertableWebApplicationContext context, String path)
        throws IOException, javax.servlet.ServletException {
    FilterChainProxy filterChainProxy = context.getBean(FilterChainProxy.class);
    MockServletContext servletContext = new MockServletContext();
    MockHttpServletResponse response = new MockHttpServletResponse();
    servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);
    MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
    request.setServletPath(path);//from w w  w.  ja  v  a  2  s.c  o m
    request.setMethod("GET");
    filterChainProxy.doFilter(request, response, new MockFilterChain());
    return response.getStatus();
}

From source file:org.jasig.cas.adaptors.x509.web.flow.X509CertificateCredentialsNonInteractiveActionTests.java

public void testCredentialsResultsInSuccess() throws Exception {
    final MockRequestContext context = new MockRequestContext();
    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.setAttribute("javax.servlet.request.X509Certificate", new X509Certificate[] { VALID_CERTIFICATE });
    context.setExternalContext(//from w  ww  .ja  v  a2s  . co  m
            new ServletExternalContext(new MockServletContext(), request, new MockHttpServletResponse()));
    assertEquals("success", this.action.execute(context).getId());
}

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

@Test
public void testSuccessfulAuthenticationWithServiceAndWarn() throws Exception {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    final MockRequestContext context = new MockRequestContext();

    request.addParameter("username", "test");
    request.addParameter("password", "test");
    request.addParameter("warn", "true");
    request.addParameter("service", "test");

    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    context.getRequestScope().put("credentials", TestUtils.getCredentialsWithSameUsernameAndPassword());
    //       this.action.bind(context);
    //       assertEquals("success", this.action.submit(context).getId());
    //        assertNotNull(response.getCookie(this.warnCookieGenerator
    //            .getCookieName()));
}

From source file:org.settings4j.helper.web.DefaultPropertiesLoaderTest.java

/**
 * Check if the Call without InitParameter doesn't add a Connector.
 *//*from   ww w  .ja  v  a2 s . c o  m*/
@Test
public void testInitDefaultPropertiesWithoutInitParameter() {

    // Prepare servletContext without InitParameter.
    final MockServletContext servletContext = new MockServletContext();

    assertThat(Settings4j.getConnectors(), hasSize(DEFAULT_CONNECTOR_COUNT));

    // start test one time: initDefaultProperties from ServletContext initParameter
    new DefaultPropertiesLoader().initDefaultProperties(servletContext);

    // validate Result (no connector should be added)
    assertThat(Settings4j.getConnectors(), hasSize(DEFAULT_CONNECTOR_COUNT));

}

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

public void testServiceFound() throws Exception {
    final MockRequestContext context = new MockRequestContext();
    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.setParameter("service", "test");
    context.setExternalContext(//from   ww w.jav  a 2s .  com
            new ServletExternalContext(new MockServletContext(), request, new MockHttpServletResponse()));

    final Event event = this.action.execute(context);

    assertEquals("test", WebUtils.getService(context).getId());
    assertEquals("success", event.getId());
}

From source file:newcontroller.handler.impl.DefaultRequestTest.java

@Test
public void testPutAndGet() throws Exception {
    HttpServletRequest request = MockMvcRequestBuilders.get("/").buildRequest(new MockServletContext());
    Request req = new DefaultRequest(request);
    LocalDate now = LocalDate.now();
    req.put("foo", 100).put("bar", "hoge").put("piyo", now);

    assertThat(req.get("foo", Integer.class), is(100));
    assertThat(req.get("bar", String.class), is("hoge"));
    assertThat(req.get("piyo", LocalDate.class), is(now));
}

From source file:org.jasig.cas.TestUtils.java

public static MockRequestContext getContext(final MockHttpServletRequest request,
        final MockHttpServletResponse response) {
    final MockRequestContext context = new MockRequestContext();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    return context;
}

From source file:net.node42.filter.SimpleTimeoutLoadLimitingFilterTest.java

@Test
public void testTimeoutResponseReturned() throws Exception {
    final SimpleTimeoutLoadLimitingFilter target = new SimpleTimeoutLoadLimitingFilter();
    final MockFilterConfig filterConfig = new MockFilterConfig(new MockServletContext());
    // Nothing gets through, and it times out immediately...
    filterConfig.addInitParameter("request_count_concurrent_max", "0");
    filterConfig.addInitParameter("request_timeout", "-1000");
    target.init(filterConfig);//from www  .jav a  2 s .  c  o m

    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    response.setWriterAccessAllowed(true);
    final FilterChain filterChain = mock(FilterChain.class);

    runLoadLimitingFilter(target, request, response, filterChain);
    await().atMost(2, TimeUnit.SECONDS).untilAtomic(target.ACTIVE_REQUESTS, equalTo(0));
    assertThat(response.getContentAsString()).isNotNull().isNotEmpty()
            .isEqualTo(SimpleTimeoutLoadLimitingFilter.CONTENT_TIMEOUT_DEFAULT);
}

From source file:org.sventon.appl.ApplicationTest.java

@Test
public void testGetBaseURL() throws Exception {
    final MockServletContext servletContext = new MockServletContext();
    servletContext.setContextPath("sventon-test");
    configDirectory.setCreateDirectories(false);
    configDirectory.setServletContext(servletContext);
    final Application application = new Application(configDirectory);

    assertNull(application.getBaseURL());

    System.setProperty(Application.PROPERTY_KEY_SVENTON_BASE_URL, "not-a-url");
    assertNull(application.getBaseURL());

    System.setProperty(Application.PROPERTY_KEY_SVENTON_BASE_URL, "http://validurl");
    assertEquals("http://validurl/", application.getBaseURL().toString());

    System.setProperty(Application.PROPERTY_KEY_SVENTON_BASE_URL, "http://validurl:81/");
    assertEquals("http://validurl:81/", application.getBaseURL().toString());
}

From source file:org.jasig.cas.support.openid.web.flow.OpenIdSingleSignOnActionTests.java

public void testNoTgt() throws Exception {
    final MockRequestContext context = new MockRequestContext();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(),
            new MockHttpServletRequest(), new MockHttpServletResponse()));
    assertEquals("error", this.action.execute(context).getId());
}