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

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

Introduction

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

Prototype

public MockHttpServletRequest(@Nullable String method, @Nullable String requestURI) 

Source Link

Document

Create a new MockHttpServletRequest with a default MockServletContext .

Usage

From source file:org.tonguetied.web.RequestUtilsTest.java

/**
 * Test method for {@link org.tonguetied.web.RequestUtils#isGetMethod(javax.servlet.http.HttpServletRequest)}.
 *///from   w w  w  .  j a  va  2 s  . co m
@Test
public final void testIsGetMethodIsFalse() {
    final MockHttpServletRequest request = new MockHttpServletRequest("post", "/");
    assertFalse(RequestUtils.isGetMethod(request));
}

From source file:org.openmrs.module.emrapi.web.controller.BaseEmrControllerTest.java

/**
 * Creates a request from the given parameters.
 * <p>// w  ww  .jav  a2  s .c o  m
 * The requestURI is automatically preceded with "/rest/" + RestConstants.VERSION_1.
 *
 * @param method
 * @param requestURI
 * @return
 */
public MockHttpServletRequest request(RequestMethod method, String requestURI) {
    MockHttpServletRequest request = new MockHttpServletRequest(method.toString(), requestURI);
    request.addHeader("content-type", "application/json");
    return request;
}

From source file:org.jasig.cas.support.oauth.web.OAuth10LoginControllerTests.java

@Test
public void testOK() throws Exception {
    // must be an OAuth 1.0 provider
    final TwitterProvider twitterProvider = new TwitterProvider();
    twitterProvider.setKey("OPEWaSoTuAe49K4dSoRvNw");
    twitterProvider.setSecret("aKmvleltXAmLKcnlMgzRjTsCnhV3QVMVDh153xJttCo");
    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET", MY_LOGIN_URL);
    mockRequest.setParameter(OAuthConstants.OAUTH_PROVIDER, twitterProvider.getType());
    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();
    final OAuthConfiguration oAuthConfiguration = new OAuthConfiguration();
    oAuthConfiguration.setLoginUrl(MY_LOGIN_URL);
    final List<OAuthProvider> providers = new ArrayList<OAuthProvider>();
    providers.add(twitterProvider);/*from  w ww . j  av a2  s.c  o  m*/
    oAuthConfiguration.setProviders(providers);
    // use OAuthAction to init oAuthConfiguration (as it's done in its class)
    final OAuthAction oAuthAction = new OAuthAction();
    oAuthAction.setConfiguration(oAuthConfiguration);
    final OAuth10LoginController oAuth10LoginController = new OAuth10LoginController();
    oAuth10LoginController.setConfiguration(oAuthConfiguration);
    final ModelAndView modelAndView = oAuth10LoginController.handleRequest(mockRequest, mockResponse);
    final View view = modelAndView.getView();
    assertTrue(view instanceof RedirectView);
    final RedirectView redirectView = (RedirectView) view;
    assertTrue(redirectView.getUrl().startsWith("https://api.twitter.com/oauth/authorize?oauth_token="));
}

From source file:ejportal.webapp.filter.StaticFilterTest.java

/**
 * Test filter doesnt forward when path matches.
 * // ww  w .  j a  v  a 2 s  .  c o m
 * @throws Exception
 *             the exception
 */
public void testFilterDoesntForwardWhenPathMatches() throws Exception {
    final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/scripts/dojo/test.html");
    final MockHttpServletResponse response = new MockHttpServletResponse();
    final MockFilterChain chain = new MockFilterChain();

    this.filter.doFilter(request, response, chain);

    Assert.assertNull(chain.getForwardURL());
}

From source file:ar.com.zauber.commons.spring.servlet.mvc.support.ZauberBeanNameBasedClassNameHandlerMappingTest.java

/** @throws Exception on error*/
public final void testSimpleName() throws Exception {
    MockHttpServletRequest req = new MockHttpServletRequest("GET", "/logout/");
    final HandlerExecutionChain hec = hm.getHandler(req);
    assertEquals(((DummyController) hec.getHandler()).getId(), "logout");
}

From source file:ar.com.zauber.commons.web.proxy.impl.dao.properties.PropertiesChainedRegexURLRequestMapperDAOTest.java

/** @throws Exception on error */
public final void testLoad() throws Exception {
    final Properties p = new Properties();
    p.put("0", "^/nexus/(.*)$=http://localhost:9095/nexus/$1");
    p.put("1", "^/([^/]+)/([^/]+)/([^/]+)/(.*)$"
            + "=http://localhost:9095/nexus/content/repositories/$1-$2-$3/$4");

    final PropertiesChainedRegexURLRequestMapperDAO dao = new PropertiesChainedRegexURLRequestMapperDAO(
            new SimplePropertiesProvider(p), new NullPropertiesPersister());

    final URLRequestMapper c = dao.load();
    assertFalse(c.getProxiedURLFromRequest(new MockHttpServletRequest("GET", "/foo")).hasResult());
    assertEquals(new URL("http://localhost:9095/nexus/foo/bar"),
            c.getProxiedURLFromRequest(new MockHttpServletRequest("GET", "/nexus/foo/bar")).getURL());
    assertEquals(new URL("http://localhost:9095/nexus/content/repositories/" + "zauber-code-releases/foo/bar"),
            c.getProxiedURLFromRequest(new MockHttpServletRequest("GET", "/zauber/code/releases/foo/bar"))
                    .getURL());/*from w  w w .j  a  va 2s .com*/
}

From source file:it.geosolutions.httpproxy.service.ProxyServiceDefaultTest.java

/**
 * Test IProxyService execute as HTTP GET
 *//*from  w  ww.ja  va  2  s  . c o m*/
@Test
public void testExecuteGet() {
    try {
        // Generate mocked request and response
        MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET", "/proxy/");
        mockRequest.addParameter("url", TEST_URL);
        MockHttpServletResponse mockResponse = new MockHttpServletResponse();

        // Call proxy execute
        proxy.execute(mockRequest, mockResponse);

        // Assert the response
        assertNotNull(mockResponse);
        assertEquals(mockResponse.getStatus(), HttpStatus.SC_OK);
        assertNotNull(mockResponse.getOutputStream());
        assertNotNull(mockResponse.getContentType());
        assertTrue(mockResponse.getContentType().contains("application/vnd.ogc.wms_xml"));

        LOGGER.info("Success proxy GET in '" + TEST_URL + "'");
        LOGGER.info("************************ Response ************************");
        LOGGER.info(mockResponse.getContentAsString());
        LOGGER.info("********************** EoF Response **********************");

    } catch (Exception e) {
        fail("Exception executing proxy-->\t" + e.getLocalizedMessage());
    }
}

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

/**  @throws MalformedURLException */
public final void testDirString() throws MalformedURLException {
    final URLRequestMapper r = new RegexURLRequestMapper(Pattern.compile("^/([^/]+)/([^/]+)/([^/]+)/(.*)$"),
            "http://localhost:9095/nexus/content/repositories/$1-$2-$3/$4");

    final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/zauber/code/releases/foo/hola");
    assertEquals(/*w  w w.  j  a v a  2s.c  om*/
            new URL("http://localhost:9095/nexus/content/repositories/" + "zauber-code-releases/" + "foo/hola"),
            r.getProxiedURLFromRequest(request).getURL());
}

From source file:org.craftercms.security.processors.impl.UrlAccessRestrictionCheckingProcessorTest.java

@Test
public void testAllowedAccess() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", URL);
    MockHttpServletResponse response = new MockHttpServletResponse();
    RequestContext context = new RequestContext(request, response);
    RequestSecurityProcessorChain chain = mock(RequestSecurityProcessorChain.class);

    Profile profile = new Profile();
    profile.setRoles(SetUtils.asSet(ADMIN_ROLE));

    SecurityUtils.setAuthentication(request, new DefaultAuthentication(new ObjectId().toString(), profile));

    processor.processRequest(context, chain);

    verify(chain).processRequest(context);
}

From source file:nl.surfnet.coin.selfservice.filter.SabEntitlementsFilterTest.java

@Before
public void setUp() throws Exception {
    filter = new SabEntitlementsFilter();

    filter.setAdminSurfConextIdPRole(ROLE_DASHBOARD_ADMIN.name());
    filter.setViewerSurfConextIdPRole(ROLE_DASHBOARD_VIEWER.name());

    MockitoAnnotations.initMocks(this);

    request = new MockHttpServletRequest("GET", "/anyUrl");
    response = new MockHttpServletResponse();

    SecurityContextHolder.getContext().setAuthentication(null);

}