Пример #1
0
  @Test
  public void inMemorySessionTest() throws IOException {
    TestHttpClient client = new TestHttpClient();
    client.setCookieStore(new BasicCookieStore());
    final CookieHandler cookieHandler = new CookieHandler();
    try {
      final SessionCookieConfig sessionConfig = new SessionCookieConfig();
      final SessionAttachmentHandler handler =
          new SessionAttachmentHandler(new InMemorySessionManager(), sessionConfig);
      handler.setNext(
          new HttpHandler() {
            @Override
            public void handleRequest(final HttpServerExchange exchange) throws Exception {
              final SessionManager manager = exchange.getAttachment(SessionManager.ATTACHMENT_KEY);
              Session session = manager.getSession(exchange, sessionConfig);
              if (session == null) {
                session = manager.createSession(exchange, sessionConfig);
                session.setAttribute(COUNT, 0);
              }
              Integer count = (Integer) session.getAttribute(COUNT);
              exchange.getResponseHeaders().add(new HttpString(COUNT), count.toString());
              session.setAttribute(COUNT, ++count);
              HttpHandlers.executeHandler(ResponseCodeHandler.HANDLE_200, exchange);
            }
          });
      cookieHandler.setNext(handler);
      DefaultServer.setRootHandler(cookieHandler);

      HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/notamatchingpath");
      HttpResponse result = client.execute(get);
      Assert.assertEquals(200, result.getStatusLine().getStatusCode());
      HttpClientUtils.readResponse(result);
      Header[] header = result.getHeaders(COUNT);
      Assert.assertEquals("0", header[0].getValue());

      get = new HttpGet(DefaultServer.getDefaultServerURL() + "/notamatchingpath");
      result = client.execute(get);
      Assert.assertEquals(200, result.getStatusLine().getStatusCode());
      HttpClientUtils.readResponse(result);
      header = result.getHeaders(COUNT);
      Assert.assertEquals("1", header[0].getValue());

      get = new HttpGet(DefaultServer.getDefaultServerURL() + "/notamatchingpath");
      result = client.execute(get);
      Assert.assertEquals(200, result.getStatusLine().getStatusCode());
      HttpClientUtils.readResponse(result);
      header = result.getHeaders(COUNT);
      Assert.assertEquals("2", header[0].getValue());

    } finally {
      client.getConnectionManager().shutdown();
    }
  }
  @Test
  public void testFileBasedErrorPageIsGenerated() throws IOException {
    DefaultHttpClient client = new DefaultHttpClient();
    try {
      final FileErrorPageHandler handler =
          new FileErrorPageHandler(
              new File(getClass().getResource("errorpage.html").getFile()), 404);
      DefaultServer.setRootHandler(handler);

      HttpGet get = new HttpGet(DefaultServer.getDefaultServerAddress() + "/path");
      HttpResponse result = client.execute(get);
      Assert.assertEquals(404, result.getStatusLine().getStatusCode());
      final String response = HttpClientUtils.readResponse(result);

      Assert.assertTrue(response, response.contains("Custom Error Page"));

    } finally {
      client.getConnectionManager().shutdown();
    }
  }
Пример #3
0
  @Test
  public void testBasicPathHanding() throws IOException {
    TestHttpClient client = new TestHttpClient();
    try {
      final PathHandler handler = new PathHandler();
      handler.addPath("a", new RemainingPathHandler("/a"));
      handler.addPath("/aa", new RemainingPathHandler("/aa"));
      handler.addPath("/aa/anotherSubPath", new RemainingPathHandler("/aa/anotherSubPath"));

      final PathHandler sub = new PathHandler();

      handler.addPath("/path", sub);
      sub.addPath("/subpath", new RemainingPathHandler("/subpath"));
      sub.setDefaultHandler(new RemainingPathHandler("/path"));

      DefaultServer.setRootHandler(handler);

      HttpGet get = new HttpGet(DefaultServer.getDefaultServerAddress() + "/notamatchingpath");
      HttpResponse result = client.execute(get);
      Assert.assertEquals(404, result.getStatusLine().getStatusCode());
      HttpClientUtils.readResponse(result);

      get = new HttpGet(DefaultServer.getDefaultServerAddress() + "/");
      result = client.execute(get);
      Assert.assertEquals(404, result.getStatusLine().getStatusCode());
      HttpClientUtils.readResponse(result);

      runPathTest(client, "/path", "/path", "");
      runPathTest(client, "/path/a", "/path", "/a");
      runPathTest(client, "/path/subpath", "/subpath", "");
      runPathTest(client, "/path/subpath/", "/subpath", "/");
      runPathTest(client, "/path/subpath/foo", "/subpath", "/foo");
      runPathTest(client, "/a", "/a", "");
      runPathTest(client, "/aa/anotherSubPath", "/aa/anotherSubPath", "");
      runPathTest(client, "/aa/anotherSubPath/bob", "/aa/anotherSubPath", "/bob");
      runPathTest(client, "/aa?a=b", "/aa", "", Collections.singletonMap("a", "b"));

    } finally {
      client.getConnectionManager().shutdown();
    }
  }
Пример #4
0
 private void runPathTest(
     TestHttpClient client,
     String path,
     String expectedMatch,
     String expectedRemaining,
     Map<String, String> queryParams)
     throws IOException {
   HttpResponse result;
   HttpGet get = new HttpGet(DefaultServer.getDefaultServerAddress() + path);
   result = client.execute(get);
   Assert.assertEquals(200, result.getStatusLine().getStatusCode());
   Header[] header = result.getHeaders(MATCHED);
   Assert.assertEquals(expectedMatch, header[0].getValue());
   header = result.getHeaders(PATH);
   Assert.assertEquals(expectedRemaining, header[0].getValue());
   HttpClientUtils.readResponse(result);
   for (Map.Entry<String, String> entry : queryParams.entrySet()) {
     header = result.getHeaders(entry.getKey());
     Assert.assertEquals(entry.getValue(), header[0].getValue());
   }
 }