コード例 #1
0
  public void testAsyncResponse1() throws Exception {
    String request =
        "GET /test/async HTTP/1.1\015\012"
            + "Host: localhost\015\012"
            + "Connection: close\015\012\015\012";

    String expectedResponse =
        "HTTP/1.1 200 OK\015\012"
            + "Date: <date>\015\012"
            + "Content-Length: 17\015\012"
            + "Connection: close\015\012"
            + "Server: Nginious/1.0.0\015\012\015\012"
            + "GET Async World!\012";

    HttpTestConnection conn = null;

    try {
      conn = new HttpTestConnection(4000);
      conn.write(request);

      long startTimeMillis = System.currentTimeMillis();
      String response = conn.readString();
      long endTimeMillis = System.currentTimeMillis();
      expectedResponse = conn.setHeaders(response, expectedResponse);
      assertEquals(expectedResponse, response);
      assertTrue(endTimeMillis - startTimeMillis >= 2000);
    } finally {
      if (conn != null) {
        conn.close();
      }
    }
  }
コード例 #2
0
  public void testAsyncResponse2() throws Exception {
    String requestHeaders =
        "GET /test/async HTTP/1.1\015\012"
            + "Host: localhost\015\012"
            + "Connection: <connection>\015\012\015\012";

    String expectedResponse =
        "HTTP/1.1 200 OK\015\012"
            + "Date: <date>\015\012"
            + "Content-Length: 17\015\012"
            + "Connection: <connection>\015\012"
            + "Server: Nginious/1.0.0\015\012\015\012"
            + "GET Async World!\012";

    HttpTestConnection conn = null;

    try {
      conn = new HttpTestConnection(4000);

      for (int i = 0; i < 3; i++) {
        String request =
            requestHeaders.replaceFirst("<connection>", i == 2 ? "close" : "keep-alive");
        conn.write(request);

        long startTimeMillis = System.currentTimeMillis();
        String response = null;

        if (i == 2) {
          response = conn.readString();
        } else {
          response = conn.readKeepAliveString();
        }

        long endTimeMillis = System.currentTimeMillis();
        String expectedResponse2 = conn.setHeaders(response, expectedResponse);
        expectedResponse2 =
            expectedResponse2.replaceFirst("<connection>", i == 2 ? "close" : "keep-alive");
        assertEquals(expectedResponse2, response);
        assertTrue(endTimeMillis - startTimeMillis >= 2000);
      }
    } finally {
      if (conn != null) {
        conn.close();
      }
    }
  }