@Test
  public void requestUriWithEncoding() throws Exception {
    this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/foo bar");
    MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);

    assertEquals("/foo%20bar", request.getRequestURI());
  }
  @Test
  public void requestUriAndFragment() throws Exception {
    this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/foo#bar");
    MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);

    assertEquals("/foo", request.getRequestURI());
  }
  @Test
  public void defaults() throws Exception {
    MockHttpServletRequest request = formLogin().buildRequest(this.servletContext);
    CsrfToken token =
        (CsrfToken)
            request.getAttribute(CsrfRequestPostProcessor.TestCsrfTokenRepository.ATTR_NAME);

    assertThat(request.getParameter("username")).isEqualTo("user");
    assertThat(request.getParameter("password")).isEqualTo("password");
    assertThat(request.getMethod()).isEqualTo("POST");
    assertThat(request.getParameter(token.getParameterName())).isEqualTo(token.getToken());
    assertThat(request.getRequestURI()).isEqualTo("/login");
    assertThat(request.getParameter("_csrf")).isNotNull();
  }
  @Test
  public void uri() throws Exception {
    String uri =
        "https://java.sun.com:8080/javase/6/docs/api/java/util/BitSet.html?foo=bar#and(java.util.BitSet)";
    this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, uri);
    MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);

    assertEquals("https", request.getScheme());
    assertEquals("foo=bar", request.getQueryString());
    assertEquals("java.sun.com", request.getServerName());
    assertEquals(8080, request.getServerPort());
    assertEquals("/javase/6/docs/api/java/util/BitSet.html", request.getRequestURI());
    assertEquals(
        "https://java.sun.com:8080/javase/6/docs/api/java/util/BitSet.html",
        request.getRequestURL().toString());
  }
  /**
   * This method finds the handler for a given request URI, or throws an exception if none were
   * found.
   *
   * <p>It will also ensure that the URI Parameters i.e. /context/test/{name} are added to the
   * request
   *
   * @param request the request for which to find a handler
   * @return The handler that agreed to handle the specified request.
   * @throws NoSuchMethodException if no acceptable handlers could be found
   */
  protected Object getHandler(final MockHttpServletRequest request) throws NoSuchMethodException {
    HandlerExecutionChain chain = null; // NOPMD by jon.adams on 5/14/12

    final Map<String, HandlerMapping> map = applicationContext.getBeansOfType(HandlerMapping.class);
    final Iterator<HandlerMapping> itt = map.values().iterator();

    while (itt.hasNext()) {
      final HandlerMapping mapping = itt.next();

      try {
        chain = mapping.getHandler(request);
      } catch (final HttpRequestMethodNotSupportedException exc) {
        // ignore and try next
        LOGGER.info(
            mapping.getClass().getName()
                + " handler determined it will not handle the request. Message: "
                + exc.getMessage(),
            exc);
      } catch (final Exception exc) {
        throw new RuntimeException(exc); // NOPMD
      }

      if (chain == null) {
        // ignore and try next
        LOGGER.debug(
            mapping.getClass().getName() + " handler determined it will not handle the request.");
      } else {
        // found one. quit looking for more.
        break;
      }
    }

    if (chain == null) {
      throw new NoSuchMethodException(
          "Unable to find handler for request URI: " + request.getRequestURI());
    }

    return chain.getHandler();
  }
 private String createAuthorizationHeader(MockHttpServletRequest request) {
   String uri = request.getRequestURI();
   String responseDigest =
       generateDigest(
           username, realm, password, request.getMethod(), uri, qop, nonce, nc, cnonce);
   return "Digest username=\""
       + username
       + "\", realm=\""
       + realm
       + "\", nonce=\""
       + nonce
       + "\", uri=\""
       + uri
       + "\", response=\""
       + responseDigest
       + "\", qop="
       + qop
       + ", nc="
       + nc
       + ", cnonce=\""
       + cnonce
       + "\"";
 }
 private void exposePathInMapping(MockHttpServletRequest request, String mapping) {
   String pathInMapping =
       this.pathMatcher.extractPathWithinPattern(mapping, request.getRequestURI());
   request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, pathInMapping);
 }