Exemplo n.º 1
0
  @Test
  public void testRedirectHandler() throws IOException {
    TestHttpClient client = new TestHttpClient();
    try {
      HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path/a");
      HttpResponse result = client.execute(get);
      Assert.assertEquals(200, result.getStatusLine().getStatusCode());
      HttpClientUtils.readResponse(result);
      Assert.assertEquals("/target/path/a", message);

      get = new HttpGet(DefaultServer.getDefaultServerURL() + "/aabc");
      result = client.execute(get);
      Assert.assertEquals(200, result.getStatusLine().getStatusCode());
      HttpClientUtils.readResponse(result);
      Assert.assertEquals("/target/matched/aab", message);

      get = new HttpGet(DefaultServer.getDefaultServerURL() + "/somePath/aabc");
      result = client.execute(get);
      Assert.assertEquals(200, result.getStatusLine().getStatusCode());
      HttpClientUtils.readResponse(result);
      Assert.assertEquals("/target/matched/aab", message);
    } finally {
      client.getConnectionManager().shutdown();
    }
  }
  @Test
  public void testReqriteHostHeader() throws Exception {
    setProxyHandler(true, false);
    TestHttpClient client = new TestHttpClient();

    try {
      HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/x-forwarded");
      get.addHeader(Headers.X_FORWARDED_FOR.toString(), "50.168.245.32");
      HttpResponse result = client.execute(get);
      Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());

      Assert.assertEquals(
          port,
          Integer.parseInt(result.getFirstHeader(Headers.X_FORWARDED_PORT.toString()).getValue()));
      Assert.assertEquals(
          "http", result.getFirstHeader(Headers.X_FORWARDED_PROTO.toString()).getValue());
      Assert.assertEquals(
          String.format("localhost:%d", port),
          result.getFirstHeader(Headers.X_FORWARDED_HOST.toString()).getValue());
      Assert.assertEquals(
          DefaultServer.getDefaultServerAddress().getAddress().getHostAddress(),
          result.getFirstHeader(Headers.X_FORWARDED_FOR.toString()).getValue());

    } finally {
      client.getConnectionManager().shutdown();
    }
  }
Exemplo n.º 3
0
  @Test
  public void testMultiPartIndividualFileToLarge() throws IOException {
    TestHttpClient client = new TestHttpClient();
    try {
      String uri = DefaultServer.getDefaultServerURL() + "/servletContext/3";
      HttpPost post = new HttpPost(uri);
      MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

      entity.addPart(
          "formValue", new StringBody("myValue", "text/plain", Charset.forName("UTF-8")));
      entity.addPart(
          "file",
          new FileBody(new File(MultiPartTestCase.class.getResource("uploadfile.txt").getFile())));

      post.setEntity(entity);
      HttpResponse result = client.execute(post);
      String response = HttpClientUtils.readResponse(result);
      Assert.assertEquals(
          "TEST FAILED: wrong response code\n" + response,
          500,
          result.getStatusLine().getStatusCode());
    } finally {
      client.getConnectionManager().shutdown();
    }
  }
  @Test
  public void testXForwardedSsl() throws Exception {
    setProxyHandler(false, false);
    TestHttpClient client = new TestHttpClient();

    try {
      client.setSSLContext(DefaultServer.getClientSSLContext());
      HttpGet get = new HttpGet(DefaultServer.getDefaultServerSSLAddress() + "/x-forwarded");

      HttpResponse result = client.execute(get);
      Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());

      Assert.assertEquals(
          sslPort,
          Integer.parseInt(result.getFirstHeader(Headers.X_FORWARDED_PORT.toString()).getValue()));
      Assert.assertEquals(
          "https", result.getFirstHeader(Headers.X_FORWARDED_PROTO.toString()).getValue());
      Assert.assertEquals(
          "localhost", result.getFirstHeader(Headers.X_FORWARDED_HOST.toString()).getValue());
      Assert.assertEquals(
          DefaultServer.getDefaultServerAddress().getAddress().getHostAddress(),
          result.getFirstHeader(Headers.X_FORWARDED_FOR.toString()).getValue());

    } finally {
      client.getConnectionManager().shutdown();
    }
  }
  @Test
  public void testExtensionMatchServletWithGlobalFilter() throws IOException, ServletException {

    DeploymentInfo builder = new DeploymentInfo();

    final PathHandler root = new PathHandler();
    final ServletContainer container = ServletContainer.Factory.newInstance();

    builder.addServlet(new ServletInfo("*.jsp", PathMappingServlet.class).addMapping("*.jsp"));

    builder.addFilter(new FilterInfo("/*", PathFilter.class));
    builder.addFilterUrlMapping("/*", "/*", DispatcherType.REQUEST);

    builder
        .setClassIntrospecter(TestClassIntrospector.INSTANCE)
        .setClassLoader(FilterPathMappingTestCase.class.getClassLoader())
        .setContextPath("/servletContext")
        .setDeploymentName("servletContext.war");

    final DeploymentManager manager = container.addDeployment(builder);
    manager.deploy();
    root.addPrefixPath(builder.getContextPath(), manager.start());

    DefaultServer.setRootHandler(root);

    TestHttpClient client = new TestHttpClient();
    try {
      runTest(client, "aa.jsp", "*.jsp - /aa.jsp - null", "/*");

    } finally {
      client.getConnectionManager().shutdown();
    }
  }
  @Test
  public void testFileUpload() throws Exception {
    DefaultServer.setRootHandler(new BlockingHandler(createHandler()));
    TestHttpClient client = new TestHttpClient();
    try {

      HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL() + "/path");
      // post.setHeader(Headers.CONTENT_TYPE, MultiPartHandler.MULTIPART_FORM_DATA);
      MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

      entity.addPart(
          "formValue", new StringBody("myValue", "text/plain", Charset.forName("UTF-8")));
      entity.addPart(
          "file",
          new FileBody(
              new File(
                  MultipartFormDataParserTestCase.class.getResource("uploadfile.txt").getFile())));

      post.setEntity(entity);
      HttpResponse result = client.execute(post);
      Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
      HttpClientUtils.readResponse(result);

    } finally {
      client.getConnectionManager().shutdown();
    }
  }
 @Test
 public void testHeadersAreLowercase() throws IOException {
   TestHttpClient client = new TestHttpClient();
   try {
     HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/name");
     HttpResponse result = client.execute(get);
     Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
     HttpClientUtils.readResponse(result);
     Header header = result.getFirstHeader("x-custom-header");
     Assert.assertEquals("x-custom-header", header.getName());
   } finally {
     client.getConnectionManager().shutdown();
   }
 }
Exemplo n.º 8
0
  @Test
  public void testRewrite() throws Exception {

    TestHttpClient client = new TestHttpClient();
    try {
      HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/foo1");
      HttpResponse result = client.execute(get);
      Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
      String response = HttpClientUtils.readResponse(result);
      Assert.assertEquals(
          "pathInfo:null queryString:null servletPath:/bar1 requestUri:/servletContext/foo1",
          response);

    } finally {
      client.getConnectionManager().shutdown();
    }
  }
  // https://issues.jboss.org/browse/UNDERTOW-289
  @Test
  public void testDistributeLoadToGivenHost() throws Throwable {
    final StringBuilder resultString = new StringBuilder();

    for (int i = 0; i < 6; ++i) {
      TestHttpClient client = new TestHttpClient();
      try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/name");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        resultString.append(HttpClientUtils.readResponse(result));
        resultString.append(' ');
      } finally {
        client.getConnectionManager().shutdown();
      }
    }
    Assert.assertTrue(resultString.toString().contains("server1"));
    Assert.assertFalse(resultString.toString().contains("server2"));
  }
Exemplo n.º 10
0
  @Test
  public void testMultiPartRequest() throws IOException {
    TestHttpClient client = new TestHttpClient();
    try {
      String uri = DefaultServer.getDefaultServerURL() + "/servletContext/1";
      HttpPost post = new HttpPost(uri);
      MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

      entity.addPart(
          "formValue", new StringBody("myValue", "text/plain", Charset.forName("UTF-8")));
      entity.addPart(
          "file",
          new FileBody(new File(MultiPartTestCase.class.getResource("uploadfile.txt").getFile())));

      post.setEntity(entity);
      HttpResponse result = client.execute(post);
      Assert.assertEquals(200, result.getStatusLine().getStatusCode());
      final String response = HttpClientUtils.readResponse(result);
      Assert.assertEquals(
          "PARAMS:\n"
              + "name: formValue\n"
              + "filename: null\n"
              + "content-type: null\n"
              + "Content-Disposition: form-data; name=\"formValue\"\n"
              + "size: 7\n"
              + "content: myValue\n"
              + "name: file\n"
              + "filename: uploadfile.txt\n"
              + "content-type: null\n"
              + "Content-Disposition: form-data; name=\"file\"; filename=\"uploadfile.txt\"\n"
              + "size: 13\n"
              + "content: file contents\n",
          response);
    } finally {
      client.getConnectionManager().shutdown();
    }
  }
Exemplo n.º 11
0
  @Test
  public void testMultiPartRequestToLarge() throws IOException {
    TestHttpClient client = new TestHttpClient();
    try {
      String uri = DefaultServer.getDefaultServerURL() + "/servletContext/2";
      HttpPost post = new HttpPost(uri);
      MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

      entity.addPart(
          "formValue", new StringBody("myValue", "text/plain", Charset.forName("UTF-8")));
      entity.addPart(
          "file",
          new FileBody(new File(MultiPartTestCase.class.getResource("uploadfile.txt").getFile())));

      post.setEntity(entity);
      HttpResponse result = client.execute(post);
      Assert.assertEquals(500, result.getStatusLine().getStatusCode());
      HttpClientUtils.readResponse(result);
    } catch (IOException expected) {
      // in some environments the forced close of the read side will cause a connection reset
    } finally {
      client.getConnectionManager().shutdown();
    }
  }
  @Test
  public void testAuthentication() throws IOException, ScriptException {
    final TestHttpClient client = new TestHttpClient();
    try {

      HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/all-auth1");
      CloseableHttpResponse result = client.execute(get);
      Assert.assertEquals(StatusCodes.UNAUTHORIZED, result.getStatusLine().getStatusCode());
      Header[] values = result.getHeaders(WWW_AUTHENTICATE.toString());
      String header = getAuthHeader(BASIC, values);
      assertEquals(BASIC + " realm=\"Test Realm\"", header);
      HttpClientUtils.readResponse(result);

      get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/all-auth1");
      get.addHeader(
          AUTHORIZATION.toString(),
          BASIC + " " + FlexBase64.encodeString("user1:password1".getBytes(), false));
      result = client.execute(get);
      Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());

      Assert.assertEquals("ok", HttpClientUtils.readResponse(result));

      get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/all-auth2");
      get.addHeader(
          AUTHORIZATION.toString(),
          BASIC + " " + FlexBase64.encodeString("user2:password2".getBytes(), false));
      result = client.execute(get);
      Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());

      Assert.assertEquals("ok", HttpClientUtils.readResponse(result));

      get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/admin");
      get.addHeader(
          AUTHORIZATION.toString(),
          BASIC + " " + FlexBase64.encodeString("user1:password1".getBytes(), false));
      result = client.execute(get);
      Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());

      Assert.assertEquals("ok", HttpClientUtils.readResponse(result));
      get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/admin");
      get.addHeader(
          AUTHORIZATION.toString(),
          BASIC + " " + FlexBase64.encodeString("user2:password2".getBytes(), false));
      result = client.execute(get);
      Assert.assertEquals(StatusCodes.FORBIDDEN, result.getStatusLine().getStatusCode());
      HttpClientUtils.readResponse(result);
    } finally {
      client.getConnectionManager().shutdown();
    }
  }
 private void runTest(
     final TestHttpClient client,
     final String path,
     final String expected,
     final String... headers)
     throws IOException {
   final HttpGet get;
   final HttpResponse result;
   final String response;
   get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/" + path);
   result = client.execute(get);
   Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
   requireHeaders(result, headers);
   response = HttpClientUtils.readResponse(result);
   Assert.assertEquals(expected, response);
 }
  @Test
  public void testBasicFilterMappings() throws IOException, ServletException {

    DeploymentInfo builder = new DeploymentInfo();

    final PathHandler root = new PathHandler();
    final ServletContainer container = ServletContainer.Factory.newInstance();

    builder.addServlet(new ServletInfo("/a/*", PathMappingServlet.class).addMapping("/a/*"));

    builder.addServlet(new ServletInfo("/aa", PathMappingServlet.class).addMapping("/aa"));

    builder.addServlet(new ServletInfo("/", PathMappingServlet.class).addMapping("/"));

    builder.addServlet(new ServletInfo("contextRoot", PathMappingServlet.class).addMapping(""));

    builder.addServlet(
        new ServletInfo("/myservlet/*", PathMappingServlet.class).addMapping("/myservlet/*"));

    builder.addServlet(new ServletInfo("*.jsp", PathMappingServlet.class).addMapping("*.jsp"));

    builder.addServlet(
        new ServletInfo("/hello/*", PathMappingServlet.class).addMapping("/hello/*"));

    builder.addServlet(new ServletInfo("/test/*", PathMappingServlet.class).addMapping("/test/*"));

    builder.addFilter(new FilterInfo("/*", PathFilter.class));
    builder.addFilterUrlMapping("/*", "/*", DispatcherType.REQUEST);

    // non standard, but we still support it
    builder.addFilter(new FilterInfo("*", PathFilter.class));
    builder.addFilterUrlMapping("*", "*", DispatcherType.REQUEST);

    builder.addFilter(new FilterInfo("/a/*", PathFilter.class));
    builder.addFilterUrlMapping("/a/*", "/a/*", DispatcherType.REQUEST);

    builder.addFilter(new FilterInfo("/aa", PathFilter.class));
    builder.addFilterUrlMapping("/aa", "/aa", DispatcherType.REQUEST);

    builder.addFilter(new FilterInfo("*.bop", PathFilter.class));
    builder.addFilterUrlMapping("*.bop", "*.bop", DispatcherType.REQUEST);

    builder.addFilter(new FilterInfo("/myservlet/myfilter/*", PathFilter.class));
    builder.addFilterUrlMapping(
        "/myservlet/myfilter/*", "/myservlet/myfilter/*", DispatcherType.REQUEST);

    builder.addFilter(new FilterInfo("/myfilter/*", PathFilter.class));
    builder.addFilterUrlMapping("/myfilter/*", "/myfilter/*", DispatcherType.REQUEST);

    builder.addFilter(new FilterInfo("contextRoot", PathFilter.class));
    builder.addFilterServletNameMapping("contextRoot", "contextRoot", DispatcherType.REQUEST);

    builder.addFilter(new FilterInfo("defaultName", PathFilter.class));
    builder.addFilterServletNameMapping("defaultName", "/", DispatcherType.REQUEST);

    builder.addFilter(new FilterInfo("/helloworld/index.html", PathFilter.class));
    builder.addFilterUrlMapping(
        "/helloworld/index.html", "/helloworld/index.html", DispatcherType.REQUEST);

    builder.addFilter(new FilterInfo("/test", PathFilter.class));
    builder.addFilterUrlMapping("/test", "/test", DispatcherType.REQUEST);

    builder
        .setClassIntrospecter(TestClassIntrospector.INSTANCE)
        .setClassLoader(FilterPathMappingTestCase.class.getClassLoader())
        .setContextPath("/servletContext")
        .setDeploymentName("servletContext.war");

    final DeploymentManager manager = container.addDeployment(builder);
    manager.deploy();
    root.addPrefixPath(builder.getContextPath(), manager.start());

    DefaultServer.setRootHandler(root);

    TestHttpClient client = new TestHttpClient();
    try {
      runTest(client, "test", "/test/* - /test - null", "/*", "*", "/test");
      runTest(client, "aa", "/aa - /aa - null", "/*", "*", "/aa");
      runTest(client, "a/c", "/a/* - /a - /c", "/*", "*", "/a/*");
      runTest(client, "a", "/a/* - /a - null", "/*", "*", "/a/*");
      runTest(client, "aa/b", "/ - /aa/b - null", "/*", "*", "defaultName");
      runTest(client, "a/b/c/d", "/a/* - /a - /b/c/d", "/*", "*", "/a/*");
      runTest(client, "defaultStuff", "/ - /defaultStuff - null", "/*", "*", "defaultName");
      runTest(client, "", "contextRoot - / - null", "/*", "*", "contextRoot");
      runTest(client, "yyyy.bop", "/ - /yyyy.bop - null", "/*", "*", "*.bop", "defaultName");
      runTest(client, "a/yyyy.bop", "/a/* - /a - /yyyy.bop", "/*", "*", "*.bop", "/a/*");
      runTest(
          client,
          "myservlet/myfilter/file.dat",
          "/myservlet/* - /myservlet - /myfilter/file.dat",
          "/*",
          "*",
          "/myservlet/myfilter/*");
      runTest(
          client,
          "myservlet/myfilter/file.jsp",
          "/myservlet/* - /myservlet - /myfilter/file.jsp",
          "/*",
          "*",
          "/myservlet/myfilter/*");
      runTest(
          client,
          "otherservlet/myfilter/file.jsp",
          "*.jsp - /otherservlet/myfilter/file.jsp - null",
          "/*",
          "*");
      runTest(
          client,
          "myfilter/file.jsp",
          "*.jsp - /myfilter/file.jsp - null",
          "/*",
          "*",
          "/myfilter/*");
      runTest(
          client,
          "helloworld/index.html",
          "/ - /helloworld/index.html - null",
          "/*",
          "*",
          "/helloworld/index.html",
          "defaultName");

    } finally {
      client.getConnectionManager().shutdown();
    }
  }