def testDiscarded(self):
        # discard: no response
        pHandler = proxyhandler.testHandleMlog(testdir+'empty_response.mlog')
        self.assertEqual(messagelog.mlog.currentId, None)

        # discard: 404
        pHandler = proxyhandler.testHandleMlog(testdir+'404.mlog')
        self.assertEqual(messagelog.mlog.currentId, None)
    def testHandlerOverflow(self):

        backup = cfg.cparser.get('messagelog', 'max_messagelog')
        # set maxresponse to 1KB so that sample.log will overflow
        cfg.cparser.set('messagelog', 'max_messagelog', '1')
        try:
            pHandler = proxyhandler.testHandleMlog(testdir + 'creative_commons.qlog')    # discard: ?
        finally:
            cfg.cparser.set('messagelog', 'max_messagelog', backup)

        # check no log files are created
        self.assertEqual(messagelog.mlog.currentId, None)
    def testNextProxy(self):
        testServer = proxyhandler.TestServer(next_proxy='myproxy:8080')
        pHandler = proxyhandler.testHandleMlog(testdir+'creative_commons.qlog', server=testServer)

        # check connect to destination directly
        self.assertEqual(pHandler.connect_dest, 'myproxy:8080')

        # check correct request message
        pHandler.s_out.seek(0)
        s_out = pHandler.s_out.read()
        self.assert_(s_out.find('GET http://creativecommons.org/ HTTP/1.0\r\n') == 0)
        self.assert_(s_out.find('Host: creativecommons.org') > 0)
        self.assert_(s_out.find('Connection: close') > 0)
        self.assert_(s_out.find('Proxy-connection: close') < 0)

        # check 1 message logged
        self.assertEqual(messagelog.mlog.currentId, 2)
    def testRequestForwarded(self):
        pHandler = proxyhandler.testHandleMlog(testdir+'creative_commons.qlog')

        # check connect to destination directly
        self.assertEqual(pHandler.connect_dest, 'creativecommons.org')

        # check correct request message
        pHandler.s_out.seek(0)
        s_out = pHandler.s_out.read()
        self.assert_(s_out.find('GET / HTTP/1.0\r\n') == 0)
        self.assert_(s_out.find('Host: creativecommons.org') > 0)
        self.assert_(s_out.find('Connection: close') > 0)
        self.assert_(s_out.find('Proxy-connection: close') < 0)

        # check 1 message logged
        self.assertEqual(messagelog.mlog.currentId, 2)

        # the new logged file should be identical to the orignal
        logpath = messagelog.mlog._getMsgLogPath('000000001')
        self.assert_(os.path.exists(logpath))
        self.assertEqual(file(logpath,'rb').read(), file(testdir+'creative_commons.qlog','rb').read())