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

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

Introduction

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

Prototype

public void setMethod(@Nullable String method) 

Source Link

Usage

From source file:ar.com.zauber.commons.spring.test.impl.TamperdataHttpServletRequestFactory.java

/** hace el trabajo sucio 
 * @throws UnsupportedEncodingException */
private HttpServletRequest parse(final XMLStreamReader reader)
        throws XMLStreamException, UnsupportedEncodingException {
    final MockHttpServletRequest ret = new MockHttpServletRequest();
    ret.setMethod("POST");
    String header = null;//  ww w .  j a  v a2  s  . co  m
    String postHeader = null;
    int event;
    while ((event = reader.next()) != XMLStreamConstants.END_DOCUMENT) {
        if (event == XMLStreamConstants.START_ELEMENT) {
            final String name = reader.getLocalName();
            if (name.equals("tdRequestHeader") || name.equals("tdPostHeader")) {
                header = reader.getAttributeValue(0);
            } else if (name.equals("tdPostElements")) {
                ret.setMethod("POST");
            } else if (name.equals("tdPostElement")) {
                postHeader = reader.getAttributeValue(0);
            }
        } else if (event == XMLStreamConstants.CHARACTERS) {
            String text = reader.getText();
            if (text.length() > 1 && Character.isWhitespace(text.charAt(0))) {
                text = text.substring(1);
            }
            if (text.length() > 1 && Character.isWhitespace(text.charAt(text.length() - 1))) {
                text = text.substring(0, text.length() - 1);
            }

            final String value = URLDecoder.decode(URLDecoder.decode(text, encoding), encoding);
            if (header != null) {
                ret.addHeader(header, value);
            } else if (postHeader != null) {
                ret.addParameter(postHeader, value);
            }
            header = null;
            postHeader = null;
        } else {
            header = null;
            postHeader = null;
        }
    }
    reader.close();
    return ret;
}

From source file:org.jasig.cas.web.support.ThrottledSubmissionByIpAddressHandlerInterceptorAdapterTests.java

public void testOneFailure() throws Exception {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final ModelAndView modelAndView = new ModelAndView("casLoginView");
    request.setMethod("POST");
    request.setRemoteAddr("111.111.111.111");
    this.adapter.postHandle(request, new MockHttpServletResponse(), new Object(), modelAndView);

    assertEquals("casLoginView", modelAndView.getViewName());
}

From source file:org.jasig.cas.web.support.ThrottledSubmissionByIpAddressHandlerInterceptorAdapterTests.java

public void testEnoughFailuresToCauseProblem() throws Exception {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final ModelAndView modelAndView = new ModelAndView("casLoginView");
    request.setMethod("POST");
    request.setRemoteAddr("111.111.111.111");
    for (int i = 0; i < CONST_FAILURE_THRESHHOLD + 1; i++) {
        this.adapter.postHandle(request, new MockHttpServletResponse(), new Object(), modelAndView);
    }/*from  ww w  .  j ava 2 s.  c  o  m*/

    assertFalse(this.adapter.preHandle(request, new MockHttpServletResponse(), new Object()));
}

From source file:org.jasig.cas.web.support.ThrottledSubmissionByIpAddressHandlerInterceptorAdapterTests.java

public void testFailuresThenSuccess() throws Exception {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final ModelAndView modelAndView = new ModelAndView("casLoginView");
    request.setMethod("POST");
    request.setRemoteAddr("111.111.111.111");
    for (int i = 0; i < CONST_FAILURE_THRESHHOLD + 1; i++) {
        this.adapter.postHandle(request, new MockHttpServletResponse(), new Object(), modelAndView);
    }//from w w  w. j ava  2  s .c  o m

    assertFalse(this.adapter.preHandle(request, new MockHttpServletResponse(), new Object()));

    for (int i = 0; i < CONST_FAILURE_THRESHHOLD; i++) {
        this.adapter.decrementCounts();
    }

    assertTrue(this.adapter.preHandle(request, new MockHttpServletResponse(), new Object()));
}

From source file:nl.lumc.nanopub.store.api.NanopubControllerIntegrationTest.java

@DirtiesContext
@Test//from   w w w .  ja va2 s.  c om
public void testListZeroNanopubs() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("GET");
    request.setRequestURI("/nanopubs");

    MockHttpServletResponse response = new MockHttpServletResponse();

    Object handler = handlerMapping.getHandler(request).getHandler();
    handlerAdapter.handle(request, response, handler);

    assertEquals("[]", response.getContentAsString());
}

From source file:nl.lumc.nanopub.store.api.NanopubControllerIntegrationTest.java

@DirtiesContext
@Test//from  w  w w .  j av  a 2  s . c  o m
public void testListNanopubs() throws Exception {
    addNanopub(this.repository, EXAMPLE_STORED_NANOPUB_NAME);

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("GET");
    request.setRequestURI("/nanopubs");

    MockHttpServletResponse response = new MockHttpServletResponse();

    Object handler = handlerMapping.getHandler(request).getHandler();
    handlerAdapter.handle(request, response, handler);

    assertEquals("[\"" + EXAMPLE_STORED_URI + "\"]", response.getContentAsString());
}

From source file:edu.internet2.middleware.shibboleth.idp.system.conf1.SAML1ArtifactResolutionTest.java

public void testArtifactResolution() throws Exception {
    String relyingPartyId = "urn:example.org:sp1";
    SAMLArtifactMapEntry artifactEntry = stageArtifact(relyingPartyId);
    String soapMessage = buildRequestMessage(relyingPartyId, artifactEntry.getArtifact());

    MockHttpServletRequest servletRequest = new MockHttpServletRequest();
    servletRequest.setMethod("POST");
    servletRequest.setPathInfo("/saml1/SOAP/ArtifactResolution");
    servletRequest.setContent(soapMessage.getBytes());

    MockHttpServletResponse servletResponse = new MockHttpServletResponse();

    ProfileHandlerManager handlerManager = (ProfileHandlerManager) getApplicationContext()
            .getBean("shibboleth.HandlerManager");
    ProfileHandler handler = handlerManager.getProfileHandler(servletRequest);
    assertNotNull(handler);//from   w w w.  j a  v a 2s . co m

    // Process request
    HTTPInTransport profileRequest = new HttpServletRequestAdapter(servletRequest);
    HTTPOutTransport profileResponse = new HttpServletResponseAdapter(servletResponse, false);
    handler.processRequest(profileRequest, profileResponse);

    String response = servletResponse.getContentAsString();
    assertTrue(response.contains("saml1p:Success"));
    assertTrue(response.contains("saml1:Assertion"));
}

From source file:edu.internet2.middleware.shibboleth.idp.system.conf1.SAML1ArtifactResolutionTest.java

public void testWithoutConfiguration() throws Exception {
    String relyingPartyId = "urn:example.org:BogusSP";
    SAMLArtifactMapEntry artifactEntry = stageArtifact(relyingPartyId);
    String soapMessage = buildRequestMessage(relyingPartyId, artifactEntry.getArtifact());

    MockHttpServletRequest servletRequest = new MockHttpServletRequest();
    servletRequest.setMethod("POST");
    servletRequest.setPathInfo("/saml1/SOAP/ArtifactResolution");
    servletRequest.setContent(soapMessage.getBytes());

    MockHttpServletResponse servletResponse = new MockHttpServletResponse();

    ProfileHandlerManager handlerManager = (ProfileHandlerManager) getApplicationContext()
            .getBean("shibboleth.HandlerManager");
    ProfileHandler handler = handlerManager.getProfileHandler(servletRequest);
    assertNotNull(handler);/*from w w w.j a  v a  2s  . c  om*/

    // Process request
    HTTPInTransport profileRequest = new HttpServletRequestAdapter(servletRequest);
    HTTPOutTransport profileResponse = new HttpServletResponseAdapter(servletResponse, false);
    handler.processRequest(profileRequest, profileResponse);

    String response = servletResponse.getContentAsString();
    assertTrue(response.contains("saml1p:Success"));
    assertTrue(response.contains("saml1p:RequestDenied"));
}

From source file:com.tasktop.c2c.server.common.service.tests.ajp.AjpProxyWebTest.java

@Test
public void testProxyHandlesCookies() throws Exception {
    final String ajpBaseUri = String.format("ajp://localhost:%s", container.getAjpPort());
    Payload payload = new Payload(HttpServletResponse.SC_OK, "some content\none two three\n\nfour");
    payload.getResponseHeaders().put("foo", "bar");
    payload.getSessionVariables().put("s1", "v1");
    TestServlet.setResponsePayload(payload);

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("GET");
    request.setRequestURI("/test");
    request.setQueryString("a=b");
    request.setParameter("a", new String[] { "b" });
    request.addHeader("c", "d ef");
    MockHttpServletResponse response = new MockHttpServletResponse();
    proxy.proxyRequest(ajpBaseUri + "/foo", request, response);

    Request firstRequest = null;/*from ww  w .  j  a  v  a2s .  c om*/
    for (int i = 0; i < 100; i++) {
        firstRequest = TestServlet.getLastRequest();

        // If our request is not yet there, then pause and retry shortly - proxying is an async process, and this
        // request was sometimes coming back as null which was causing test failures on the first assert below.
        if (firstRequest == null) {
            Thread.sleep(10);
        } else {
            // Got our request, so break now.
            break;
        }
    }

    Assert.assertTrue(firstRequest.isNewSession());
    Assert.assertEquals("v1", firstRequest.getSessionAttributes().get("s1"));

    List<org.apache.commons.httpclient.Cookie> cookies = new ArrayList<org.apache.commons.httpclient.Cookie>();
    for (String headerName : response.getHeaderNames()) {
        if (headerName.equalsIgnoreCase("set-cookie") || headerName.equalsIgnoreCase("set-cookie2")) {
            cookies.addAll(Arrays.asList(new RFC2965Spec().parse("localhost", container.getPort(), "/", false,
                    response.getHeader(headerName).toString())));
        }
    }
    Assert.assertEquals(1, cookies.size());
    Cookie cookie = cookies.get(0);
    Assert.assertEquals("almp.JSESSIONID", cookie.getName());

    MockHttpServletRequest request2 = new MockHttpServletRequest();
    request2.setMethod("GET");
    request2.setRequestURI("/test");
    request2.addHeader("Cookie", cookie.toExternalForm());
    MockHttpServletResponse response2 = new MockHttpServletResponse();

    payload = new Payload(HttpServletResponse.SC_OK, "test");
    TestServlet.setResponsePayload(payload);

    proxy.proxyRequest(ajpBaseUri + "/foo", request2, response2);

    Request secondRequest = TestServlet.getLastRequest();
    Assert.assertFalse(secondRequest.isNewSession());
    Assert.assertEquals(firstRequest.getSessionId(), secondRequest.getSessionId());
    Assert.assertEquals("v1", secondRequest.getSessionAttributes().get("s1"));
}

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 .  j ava2  s .c  o m
    request.setMethod("GET");
    filterChainProxy.doFilter(request, response, new MockFilterChain());
    return response.getStatus();
}