def test_logFormatOverride(self): """ If the factory is initialized with a custom log formatter then that formatter is used to generate lines for the log file. """ def notVeryGoodFormatter(timestamp, request): return u"this is a bad log format" reactor = Clock() reactor.advance(1234567890) logPath = self.mktemp() factory = self.factory( logPath=logPath, logFormatter=notVeryGoodFormatter) factory._reactor = reactor factory.startFactory() try: factory.log(DummyRequestForLogTest(factory)) finally: factory.stopFactory() self.assertEqual( # self.linesep is a sad thing. # https://twistedmatrix.com/trac/ticket/6938 b"this is a bad log format" + self.linesep, FilePath(logPath).getContent())
def _xforwardedforTest(self, header): """ Assert that a request with the given value in its I{X-Forwarded-For} header is logged by L{proxiedLogFormatter} the same way it would have been logged by L{combinedLogFormatter} but with 172.16.1.2 as the client address instead of the normal value. @param header: An I{X-Forwarded-For} header with left-most address of 172.16.1.2. """ reactor = Clock() reactor.advance(1234567890) timestamp = http.datetimeToLogString(reactor.seconds()) request = DummyRequestForLogTest(http.HTTPFactory()) expected = http.combinedLogFormatter(timestamp, request).replace( u"1.2.3.4", u"172.16.1.2") request.requestHeaders.setRawHeaders(b"x-forwarded-for", [header]) line = http.proxiedLogFormatter(timestamp, request) self.assertEqual(expected, line)
def test_nonASCII(self): """ Bytes in fields of the request which are not part of ASCII are escaped in the result. """ reactor = Clock() reactor.advance(1234567890) timestamp = http.datetimeToLogString(reactor.seconds()) request = DummyRequestForLogTest(http.HTTPFactory()) request.client = IPv4Address("TCP", b"evil x-forwarded-for \x80", 12345) request.method = b"POS\x81" request.protocol = b"HTTP/1.\x82" request.headers[b"referer"] = b"evil \x83" request.headers[b"user-agent"] = b"evil \x84" line = http.combinedLogFormatter(timestamp, request) self.assertEqual( u'"evil x-forwarded-for \\x80" - - [13/Feb/2009:23:31:30 +0000] ' u'"POS\\x81 /dummy HTTP/1.0" 123 - "evil \\x83" "evil \\x84"', line)
def test_combinedLogFormat(self): """ The factory's C{log} method writes a I{combined log format} line to the factory's log file. """ reactor = Clock() # Set the clock to an arbitrary point in time. It doesn't matter when # as long as it corresponds to the timestamp in the string literal in # the assertion below. reactor.advance(1234567890) logPath = self.mktemp() factory = self.factory(logPath=logPath) factory._reactor = reactor factory.startFactory() try: factory.log(DummyRequestForLogTest(factory)) finally: factory.stopFactory() self.assertEqual( # Client IP b'"1.2.3.4" ' # Some blanks we never fill in b'- - ' # The current time (circa 1234567890) b'[13/Feb/2009:23:31:30 +0000] ' # Method, URI, version b'"GET /dummy HTTP/1.0" ' # Response code b'123 ' # Response length b'- ' # Value of the "Referer" header. Probably incorrectly quoted. b'"-" ' # Value pf the "User-Agent" header. Probably incorrectly quoted. b'"-"' + self.linesep, FilePath(logPath).getContent())