Example #1
0
  def test_handle_one_request_closes_connection(self):
    # By default, BaseHTTPServer.py treats all HTTP 1.1 requests as keep-alive.
    # Intentionally use HTTP 1.0 to prevent this behavior.
    response = httparchive.ArchivedHttpResponse(
        version=10, status=200, reason="OK",
        headers=[], response_data=["bat1"])
    self.set_up_proxy_server(response)
    t = threading.Thread(
        target=HttpProxyTest.serve_requests_forever, args=(self,))
    t.start()

    initial_thread_count = threading.activeCount()

    # Make a bunch of requests.
    request_count = 10
    for _ in range(request_count):
      conn = httplib.HTTPConnection('localhost', 8889, timeout=10)
      conn.request("GET", "/index.html")
      res = conn.getresponse().read()
      self.assertEqual(res, "bat1")
      conn.close()

    # Check to make sure that there is no leaked thread.
    util.WaitFor(lambda: threading.activeCount() == initial_thread_count, 2)

    self.assertEqual(request_count, HttpProxyTest.HANDLED_REQUEST_COUNT)
Example #2
0
    def test_keep_alive_header(self):
        response = httparchive.ArchivedHttpResponse(version=11,
                                                    status=200,
                                                    reason="OK",
                                                    headers=[("Connection",
                                                              "keep-alive")],
                                                    response_data=["bat1"])
        self.set_up_proxy_server(response)
        t = threading.Thread(target=HttpProxyTest.serve_requests_forever,
                             args=(self, ))
        t.start()

        initial_thread_count = threading.activeCount()

        # Make a bunch of requests.
        request_count = 10
        connections = []
        for _ in range(request_count):
            conn = httplib.HTTPConnection('localhost', 8889, timeout=10)
            conn.request("GET",
                         "/index.html",
                         headers={"Connection": "keep-alive"})
            res = conn.getresponse().read()
            self.assertEqual(res, "bat1")
            connections.append(conn)

        # Repeat the same requests.
        for conn in connections:
            conn.request("GET",
                         "/index.html",
                         headers={"Connection": "keep-alive"})
            res = conn.getresponse().read()
            self.assertEqual(res, "bat1")

        # Check that the right number of requests have been handled.
        self.assertEqual(2 * request_count,
                         HttpProxyTest.HANDLED_REQUEST_COUNT)

        # Check to make sure that exactly "request_count" new threads are active.
        self.assertEqual(threading.activeCount(),
                         initial_thread_count + request_count)

        for conn in connections:
            conn.close()

        util.WaitFor(lambda: threading.activeCount() == initial_thread_count,
                     1)