@Test
  public void getRequestURL() {
    request.setServerPort(8080);
    request.setRequestURI("/path");
    assertEquals("http://localhost:8080/path", request.getRequestURL().toString());

    request.setScheme("https");
    request.setServerName("example.com");
    request.setServerPort(8443);
    assertEquals("https://example.com:8443/path", request.getRequestURL().toString());
  }
 @Test
 public void getRequestURLWithDefaultsAndHttps() {
   request.setScheme("https");
   request.setServerPort(443);
   StringBuffer requestURL = request.getRequestURL();
   assertEquals("https://localhost", requestURL.toString());
 }
 @Test
 public void getURI() throws Exception {
   URI uri = new URI("http://example.com/path?query");
   mockRequest.setServerName(uri.getHost());
   mockRequest.setServerPort(uri.getPort());
   mockRequest.setRequestURI(uri.getPath());
   mockRequest.setQueryString(uri.getQuery());
   assertEquals("Invalid uri", uri, request.getURI());
 }
Esempio n. 4
0
  @Test
  public void uri() throws IOException {
    final MockHttpServletRequest httpRequest = prepareEmptyRequest();
    httpRequest.setScheme("https");
    httpRequest.setServerName("example.com");
    httpRequest.setServerPort(1234);
    httpRequest.setRequestURI("/test/a/b?a=1");

    Request req = RequestUtils.convert(httpRequest);
    assertThat(req.getURI(), is(URI.create("https://example.com:1234/test/a/b?a=1")));
  }
Esempio n. 5
0
  private MockHttpServletRequest constructMockRequest(
      String method, String requestURI, String acceptHeader, String contentType) {
    MockHttpServletRequest mockRequest =
        new MockHttpServletRequest() {
          public String getPathTranslated() {
            return null; // prevent Spring to resolve the file on the filesystem which fails
          }
        };

    mockRequest.setMethod(method);
    mockRequest.setRequestURI(requestURI);
    mockRequest.setServerPort(8080);
    mockRequest.addHeader("Accept", acceptHeader);
    mockRequest.addHeader("Content-Type", contentType);
    mockRequest.setContentType(contentType);
    return mockRequest;
  }
Esempio n. 6
0
 private MockHttpServletRequest buildMock(String completeUrl, String contextPath) {
   try {
     URI uri = new URI(completeUrl);
     MockServletContext context = new MockServletContext();
     context.setContextPath(contextPath);
     MockHttpServletRequest httpRequest =
         new MockHttpServletRequest(context, "get", uri.getPath());
     httpRequest.setLocalName(uri.getHost());
     httpRequest.setServerName(uri.getHost());
     httpRequest.setServletPath(
         completeUrl.substring(completeUrl.indexOf(contextPath) + contextPath.length()));
     httpRequest.setContextPath(contextPath);
     httpRequest.setServerPort(uri.getPort());
     return httpRequest;
   } catch (URISyntaxException e) {
     return null;
   }
 }
 @Test
 public void getRequestURLWithNegativePort() {
   request.setServerPort(-99);
   StringBuffer requestURL = request.getRequestURL();
   assertEquals("http://localhost", requestURL.toString());
 }
 @Test
 public void getServerPortWithCustomPort() {
   request.setServerPort(8080);
   assertEquals(8080, request.getServerPort());
 }