def __init__(self, counter, method, path, headers, content): channel = HTTPChannel() host = IPv4Address(b"TCP", b"127.0.0.1", 80) channel.makeConnection(StringTransport(hostAddress=host)) Request.__init__(self, channel, False) # An extra attribute for identifying this fake request self._counter = counter # Attributes a Request is supposed to have but we have to set ourselves # because the base class mixes together too much other logic with the # code that sets them. self.prepath = [] self.requestHeaders = headers self.content = BytesIO(content) self.requestReceived(method, path, b"HTTP/1.1") # requestReceived initializes the path attribute for us (but not # postpath). self.postpath = list(map(unquote, self.path[1:].split(b'/'))) # Our own notifyFinish / finish state because the inherited # implementation wants to write confusing stuff to the transport when # the request gets finished. self._finished = False self._finishedChannel = EventChannel() # Our own state for the response body so we don't have to dig it out of # the transport. self._responseBody = b""
def test_formatting(self): """ ``_LogFormatter.json_access_log`` returns a JSON-encoded object with the usual http access log information as properties. """ when = 123456789 json_access_log = _LogFormatter( lambda: datetime.utcfromtimestamp(when), ).json_access_log ip = "192.0.2.1" channel = HTTPChannel() transport = StringTransport(peerAddress=IPv4Address("TCP", ip, 12345)) channel.makeConnection(transport) request = Request(channel) request.gotLength(None) request.requestReceived("GET", "/", "HTTP/1.1") event = json_access_log(datetimeToLogString(when), request) self.assertThat( loads(event), MatchesDict( dict( timestamp=Equals("1973-11-29T21:33:09"), ip=Equals(ip), method=Equals("GET"), uri=Equals("/"), protocol=Equals("HTTP/1.1"), code=Equals(200), length=Equals(None), referrer=Equals(None), agent=Equals(None), )), )
def frombytes(cls, byte_string): """ Parse an AWS request from a byte string. @param byte_string: The request as parsed from a C{.req} file. @type byte_string: L{bytes} @return: A request object. @rtype: L{_AWSRequest} """ # Ensure there's a blank line so that status and header # parsing completes. blank_line = b'\n\n' if blank_line not in byte_string: byte_string += blank_line channel = HTTPChannel() channel.delimiter = b'\n' channel.makeConnection(StringTransport()) channel.dataReceived(byte_string) channel.connectionLost(ConnectionDone()) request = channel.requests[-1] method = request.method path = request.uri headers = dict(request.requestHeaders.getAllRawHeaders()) # what comes after the empty line is a body = byte_string.split(blank_line, 1)[-1] return cls(method, path, headers, body)
def test_client_sends_body(self): self.cl.post_json("testserv:8008", "foo/bar", timeout=10000, data={"a": "b"}) self.pump() clients = self.reactor.tcpClients self.assertEqual(len(clients), 1) client = clients[0][2].buildProtocol(None) server = HTTPChannel() client.makeConnection(FakeTransport(server, self.reactor)) server.makeConnection(FakeTransport(client, self.reactor)) self.pump(0.1) self.assertEqual(len(server.requests), 1) request = server.requests[0] content = request.content.read() self.assertEqual(content, b'{"a":"b"}')