def _testDataForward(self, data, method="GET", body=""): """ Build a fake proxy connection, and send C{data} over it, checking that it's forwarded to the originating request. """ # Connect everything clientTransport = StringTransportWithDisconnection() serverTransport = StringTransportWithDisconnection() channel = DummyChannel(serverTransport) parent = DummyParent(channel) serverTransport.protocol = channel client = ProxyClient(method, '/foo', 'HTTP/1.0', {"accept": "text/html"}, body, parent) clientTransport.protocol = client client.makeConnection(clientTransport) # Check data sent self.assertEquals( clientTransport.value(), "%s /foo HTTP/1.0\r\n" "connection: close\r\n" "accept: text/html\r\n\r\n%s" % (method, body)) # Fake an answer client.dataReceived(data) # Check that the data has been forwarded self.assertEquals(serverTransport.value(), data) clientTransport.loseConnection() self.assertIsInstance(channel.lostReason, ConnectionDone)
def test_headersCleanups(self): """ The headers given at initialization should be modified: B{proxy-connection} should be removed if present, and B{connection} should be added. """ client = ProxyClient(b'GET', b'/foo', b'HTTP/1.0', {b"accept": b"text/html", b"proxy-connection": b"foo"}, b'', None) self.assertEqual(client.headers, {b"accept": b"text/html", b"connection": b"close"})
def _testDataForward(self, code, message, headers, body, method="GET", requestBody="", loseConnection=True): """ Build a fake proxy connection, and send C{data} over it, checking that it's forwarded to the originating request. """ request = DummyRequest(['foo']) # Connect a proxy client to a fake transport. clientTransport = StringTransportWithDisconnection() client = ProxyClient(method, '/foo', 'HTTP/1.0', {"accept": "text/html"}, requestBody, request) clientTransport.protocol = client client.makeConnection(clientTransport) # Check data sent self.assertEquals( clientTransport.value(), "%s /foo HTTP/1.0\r\n" "connection: close\r\n" "accept: text/html\r\n\r\n%s" % (method, requestBody)) # Fake an answer client.dataReceived("HTTP/1.0 %d %s\r\n" % (code, message)) for (header, values) in headers: for value in values: client.dataReceived("%s: %s\r\n" % (header, value)) client.dataReceived("\r\n" + body) # Check that the response data has been forwarded back to the original # requester. self.assertEquals(request.responseCode, code) self.assertEquals(request.responseMessage, message) receivedHeaders = list(request.responseHeaders.getAllRawHeaders()) receivedHeaders.sort() expectedHeaders = headers[:] expectedHeaders.sort() self.assertEquals(receivedHeaders, expectedHeaders) self.assertEquals(''.join(request.written), body) # Check that when the response is done, the request is finished. if loseConnection: clientTransport.loseConnection() # Even if we didn't call loseConnection, the transport should be # disconnected. This lets us not rely on the server to close our # sockets for us. self.assertFalse(clientTransport.connected) self.assertEquals(request.finished, 1)
def test_keepaliveNotForwarded(self): """ The proxy doesn't really know what to do with keepalive things from the remote server, so we stomp over any keepalive header we get from the client. """ headers = { b"accept": b"text/html", b"keep-alive": b"300", b"connection": b"keep-alive", } expectedHeaders = headers.copy() expectedHeaders[b"connection"] = b"close" del expectedHeaders[b"keep-alive"] client = ProxyClient(b"GET", b"/foo", b"HTTP/1.0", headers, b"", None) self.assertForwardsHeaders(client, b"GET /foo HTTP/1.0", expectedHeaders)
def test_keepaliveNotForwarded(self): """ The proxy doesn't really know what to do with keepalive things from the remote server, so we stomp over any keepalive header we get from the client. """ headers = { "accept": "text/html", 'keep-alive': '300', 'connection': 'keep-alive', } expectedHeaders = headers.copy() expectedHeaders['connection'] = 'close' del expectedHeaders['keep-alive'] client = ProxyClient('GET', '/foo', 'HTTP/1.0', headers, '', None) self.assertForwardsHeaders(client, 'GET /foo HTTP/1.0', expectedHeaders)
def makeProxyClient(self, request, method=b"GET", headers=None, requestBody=b""): """ Make a L{ProxyClient} object used for testing. @param request: The request to use. @param method: The HTTP method to use, GET by default. @param headers: The HTTP headers to use expressed as a dict. If not provided, defaults to {'accept': 'text/html'}. @param requestBody: The body of the request. Defaults to the empty string. @return: A L{ProxyClient} """ if headers is None: headers = {b"accept": b"text/html"} path = b'/' + request.postpath return ProxyClient( method, path, b'HTTP/1.0', headers, requestBody, request)