def testApacheLog(self):
        requestHandler = CallTrace(
            'handler', ignoredAttributes=['writeLog', 'do_unknown'])
        requestHandler.returnValues['handleRequest'] = (
            f for f in [Yield, okPlainText, 'te', callable, 'xt'])
        stream = StringIO()
        handleRequestLog = HandleRequestLog()
        handleRequestLog._time = lambda: 1395409143.0

        observable = be((Observable(), (
            LogCollector(),
            (handleRequestLog, (requestHandler, )),
            (ApacheLogWriter(stream), ),
        )))

        result = asList(
            observable.all.handleRequest(
                Method='GET',
                Client=('127.0.0.1', 1234),
                RequestURI='http://example.org/path?key=value',
                query='key=value',
                path='/path',
                Headers={
                    'Referer': 'http://meresco.org',
                    'User-Agent': 'Meresco-Components Test'
                },
                otherKwarg='value'))

        self.assertEquals([Yield, okPlainText, 'te', callable, 'xt'], result)
        self.assertEquals(['handleRequest'],
                          requestHandler.calledMethodNames())
        logline = stream.getvalue()
        self.assertEquals(
            '127.0.0.1 - - [21/Mar/2014:13:39:03 +0000] "GET /path?key=value HTTP/1.0" 200 64 "http://meresco.org" "Meresco-Components Test"\n',
            logline)
Example #2
0
    def testLogRequestInCaseOfExceptionAfterStatusCode(self):
        requestHandler = CallTrace('handler', ignoredAttributes=['writeLog', 'do_unknown'])
        def handleRequestRaisesException(**kwargs):
            yield Yield
            yield okPlainText
            yield 'text'
            raise ValueError('doesntreallymatter')
            yield
        requestHandler.methods['handleRequest'] = handleRequestRaisesException
        stream = StringIO()
        handleRequestLog = HandleRequestLog()
        handleRequestLog._time = lambda: 1395409143.0

        observable = be((Observable(),
            (LogCollector(),
                (handleRequestLog,
                    (requestHandler,)
                ),
                (ApacheLogWriter(stream),),
            )
        ))

        result = []
        g = compose(observable.all.handleRequest(Method='GET', Client=('127.0.0.1', 1234), RequestURI='http://example.org/path?key=value', query='key=value', path='/path', Headers={'Referer': 'http://meresco.org', 'User-Agent': 'Meresco-Components Test'}, otherKwarg='value'))
        try:
            for x in g:
                result.append(x)
        except Exception:
            pass

        self.assertEqual([Yield, bytes(okPlainText, encoding='utf-8'), b'text'], result)
        self.assertEqual(['handleRequest'], requestHandler.calledMethodNames())
        logline = stream.getvalue()
        self.assertEqual('127.0.0.1 - - [21/Mar/2014:13:39:03 +0000] "GET /path?key=value HTTP/1.0" 200 64 "http://meresco.org" "Meresco-Components Test" Exception raised:\n    ValueError(\'doesntreallymatter\')\n', logline)
    def testApacheLog(self):
        requestHandler = CallTrace("handler", ignoredAttributes=["writeLog", "do_unknown"])
        requestHandler.returnValues["handleRequest"] = (f for f in [Yield, okPlainText, "te", callable, "xt"])
        stream = StringIO()
        handleRequestLog = HandleRequestLog()
        handleRequestLog._time = lambda: 1395409143.0

        observable = be(
            (Observable(), (LogCollector(), (handleRequestLog, (requestHandler,)), (ApacheLogWriter(stream),)))
        )

        result = asList(
            observable.all.handleRequest(
                Method="GET",
                Client=("127.0.0.1", 1234),
                RequestURI="http://example.org/path?key=value",
                query="key=value",
                path="/path",
                Headers={"Referer": "http://meresco.org", "User-Agent": "Meresco-Components Test"},
                otherKwarg="value",
            )
        )

        self.assertEquals([Yield, okPlainText, "te", callable, "xt"], result)
        self.assertEquals(["handleRequest"], requestHandler.calledMethodNames())
        logline = stream.getvalue()
        self.assertEquals(
            '127.0.0.1 - - [21/Mar/2014:13:39:03 +0000] "GET /path?key=value HTTP/1.0" 200 64 "http://meresco.org" "Meresco-Components Test"\n',
            logline,
        )
    def testLogRequestInCaseOfExceptionAfterStatusCode(self):
        requestHandler = CallTrace("handler", ignoredAttributes=["writeLog", "do_unknown"])

        def handleRequestRaisesException(**kwargs):
            yield Yield
            yield okPlainText
            yield "text"
            raise ValueError("doesntreallymatter")
            yield

        requestHandler.methods["handleRequest"] = handleRequestRaisesException
        stream = StringIO()
        handleRequestLog = HandleRequestLog()
        handleRequestLog._time = lambda: 1395409143.0

        observable = be(
            (Observable(), (LogCollector(), (handleRequestLog, (requestHandler,)), (ApacheLogWriter(stream),)))
        )

        result = []
        g = compose(
            observable.all.handleRequest(
                Method="GET",
                Client=("127.0.0.1", 1234),
                RequestURI="http://example.org/path?key=value",
                query="key=value",
                path="/path",
                Headers={"Referer": "http://meresco.org", "User-Agent": "Meresco-Components Test"},
                otherKwarg="value",
            )
        )
        try:
            for x in g:
                result.append(x)
        except Exception:
            pass

        self.assertEquals([Yield, okPlainText, "text"], result)
        self.assertEquals(["handleRequest"], requestHandler.calledMethodNames())
        logline = stream.getvalue()
        self.assertEquals(
            '127.0.0.1 - - [21/Mar/2014:13:39:03 +0000] "GET /path?key=value HTTP/1.0" 200 64 "http://meresco.org" "Meresco-Components Test" Exception raised:\n    ValueError(\'doesntreallymatter\',)\n',
            logline,
        )
Example #5
0
    def testLogHttpError(self):
        requestHandler = CallTrace('handler', ignoredAttributes=['writeLog', 'do_unknown'])
        stream = StringIO()
        handleRequestLog = HandleRequestLog()
        handleRequestLog._time = lambda: 1395409143.0

        observable = be((Observable(),
            (LogCollector(),
                (handleRequestLog,
                    (requestHandler,),
                ),
                (ApacheLogWriter(stream),),
            )
        ))

        # called by ObservableHttpServer
        observable.do.logHttpError(Method='GET', ResponseCode=503, Client=('127.0.0.1', 1234), RequestURI='http://example.org/path?key=value', Headers={}, otherKwarg='value')

        logline = stream.getvalue()
        self.assertEqual('127.0.0.1 - - [21/Mar/2014:13:39:03 +0000] "GET /path?key=value HTTP/1.0" 503 - "-" "-"\n', logline)
        self.assertEqual(['logHttpError'], requestHandler.calledMethodNames())
        self.assertEqual(dict(Method='GET', ResponseCode=503, Client=('127.0.0.1', 1234), RequestURI='http://example.org/path?key=value', Headers={}, otherKwarg='value'), requestHandler.calledMethods[0].kwargs)
    def testLogHttpError(self):
        requestHandler = CallTrace("handler", ignoredAttributes=["writeLog", "do_unknown"])
        stream = StringIO()
        handleRequestLog = HandleRequestLog()
        handleRequestLog._time = lambda: 1395409143.0

        observable = be(
            (Observable(), (LogCollector(), (handleRequestLog, (requestHandler,)), (ApacheLogWriter(stream),)))
        )

        # called by ObservableHttpServer
        observable.do.logHttpError(
            Method="GET",
            ResponseCode=503,
            Client=("127.0.0.1", 1234),
            RequestURI="http://example.org/path?key=value",
            Headers={},
            otherKwarg="value",
        )

        logline = stream.getvalue()
        self.assertEquals(
            '127.0.0.1 - - [21/Mar/2014:13:39:03 +0000] "GET /path?key=value HTTP/1.0" 503 - "-" "-"\n', logline
        )
        self.assertEquals(["logHttpError"], requestHandler.calledMethodNames())
        self.assertEquals(
            dict(
                Method="GET",
                ResponseCode=503,
                Client=("127.0.0.1", 1234),
                RequestURI="http://example.org/path?key=value",
                Headers={},
                otherKwarg="value",
            ),
            requestHandler.calledMethods[0].kwargs,
        )