def test_auth(self):
        # secret activated by default
        oapp = ClientTesterMiddleware(SomeApp())

        # not using the secret
        app = TestAppPlus(oapp)
        app.get('/', status=200)
        self.assertRaises(AppError, self._run_session, app)

        # now using the secret
        app = TestAppPlus(oapp, secret='CHANGEME')
        app.get('/', status=200)
        self._run_session(app)
    def test_auth(self):
        # secret activated by default
        oapp = ClientTesterMiddleware(SomeApp())

        # not using the secret
        app = TestAppPlus(oapp)
        app.get('/', status=200)
        self.assertRaises(AppError, self._run_session, app)

        # now using the secret
        app = TestAppPlus(oapp, secret='CHANGEME')
        app.get('/', status=200)
        self._run_session(app)
 def setUp(self):
     app = ClientTesterMiddleware(SomeApp(), requires_secret=False)
     self.app = TestAppPlus(app)
class TestSupport(unittest.TestCase):

    def setUp(self):
        app = ClientTesterMiddleware(SomeApp(), requires_secret=False)
        self.app = TestAppPlus(app)

    def test_clienttester(self):
        # that calls the app
        self.app.get('/bleh', status=200)

        # let's ask for a 503 and then a 400
        self.app.mock(503)
        self.app.mock(400)

        self.app.get('/buh', status=503)
        self.app.get('/buh', status=400)

        # back to normal
        self.app.get('/buh', status=200)

        # let's ask for two 503s
        self.app.mock(503, repeat=2)

        self.app.get('/buh', status=503)
        self.app.get('/buh', status=503)
        self.app.get('/buh', status=200)

        # some headers and body now
        self.app.mock(503, 'oy', headers={'foo': '1'})

        res = self.app.get('/buh', status=503)
        self.assertEqual(res.body, 'oy')
        self.assertEqual(res.headers['foo'], '1')

        # repeat stuff indefinitely
        self.app.mock(503, repeat=-1)

        for i in range(20):
            self.app.get('/buh', status=503)

        # let's wipe out the pile
        self.app.del_mocks()
        self.app.get('/buh', status=200)

        # a bit of timing now
        self.app.mock(503, delay=.5)
        now = time.time()
        self.app.get('/buh', status=503)
        then = time.time()
        self.assertTrue(then - now >= .5)

    def test_filtering(self):
        # we want to add .5 delays for *all* requests
        self.app.filter({'*': .5})

        # let see if it worked
        now = time.time()
        self.app.get('/buh', status=200)
        then = time.time()
        self.assertTrue(then - now >= .5)

        # we want to add .5 delays for *503* requests only
        self.app.filter({503: .5})

        # let see if it worked
        now = time.time()
        self.app.get('/buh', status=200)
        then = time.time()
        self.assertTrue(then - now < .5)

        self.app.mock(503)
        now = time.time()
        self.app.get('/buh', status=503)
        then = time.time()
        self.assertTrue(then - now >= .5)

        # let's remove the filters
        self.app.del_filters()

        self.app.mock(503)
        now = time.time()
        self.app.get('/buh', status=503)
        then = time.time()
        self.assertTrue(then - now < .5)

    def test_rec_flag(self):
        self.assertEquals(self.app.rec_status(), DISABLED)

        self.app.start_recording()
        self.assertEquals(self.app.rec_status(), RECORD)

        self.app.start_replaying()
        self.assertEquals(self.app.rec_status(), REPLAY)

        self.app.disable_recording()
        self.assertEquals(self.app.rec_status(), DISABLED)

    def _run_session(self, app=None):
        if app is None:
            app = self.app

        self.assertEquals(app.rec_status(), DISABLED)

        app.start_recording()
        self.assertEquals(app.rec_status(), RECORD)

        # recording three calls
        app.post('/1', params='tic')
        app.post('/2', params='tac')
        app.post('/3', params='toe2')
        app.post('/3', params='toe')

        # good. good. let's replay them
        app.start_replaying()
        self.assertEquals(app.rec_status(), REPLAY)

        old = SomeApp.__call__

        def badreq(*args):
             raise exc.HTTPBadRequest()

        SomeApp.__call__ = badreq

        try:
            self.assertEquals(app.post('/3', params='toe').body, 'toe')
            self.assertEquals(app.post('/2', params='tac').body, 'tac')
            self.assertEquals(app.post('/1', 'tic').body, 'tic')
            app.post('/buuhhh', status=400)
        finally:
            SomeApp.__call__ = old

    def test_auth(self):
        # secret activated by default
        oapp = ClientTesterMiddleware(SomeApp())

        # not using the secret
        app = TestAppPlus(oapp)
        app.get('/', status=200)
        self.assertRaises(AppError, self._run_session, app)

        # now using the secret
        app = TestAppPlus(oapp, secret='CHANGEME')
        app.get('/', status=200)
        self._run_session(app)
 def setUp(self):
     app = ClientTesterMiddleware(SomeApp(), requires_secret=False)
     self.app = TestAppPlus(app)
class TestSupport(unittest.TestCase):
    def setUp(self):
        app = ClientTesterMiddleware(SomeApp(), requires_secret=False)
        self.app = TestAppPlus(app)

    def test_clienttester(self):
        # that calls the app
        self.app.get('/bleh', status=200)

        # let's ask for a 503 and then a 400
        self.app.mock(503)
        self.app.mock(400)

        self.app.get('/buh', status=503)
        self.app.get('/buh', status=400)

        # back to normal
        self.app.get('/buh', status=200)

        # let's ask for two 503s
        self.app.mock(503, repeat=2)

        self.app.get('/buh', status=503)
        self.app.get('/buh', status=503)
        self.app.get('/buh', status=200)

        # some headers and body now
        self.app.mock(503, 'oy', headers={'foo': '1'})

        res = self.app.get('/buh', status=503)
        self.assertEqual(res.body, 'oy')
        self.assertEqual(res.headers['foo'], '1')

        # repeat stuff indefinitely
        self.app.mock(503, repeat=-1)

        for i in range(20):
            self.app.get('/buh', status=503)

        # let's wipe out the pile
        self.app.del_mocks()
        self.app.get('/buh', status=200)

        # a bit of timing now
        self.app.mock(503, delay=.5)
        now = time.time()
        self.app.get('/buh', status=503)
        then = time.time()
        self.assertTrue(then - now >= .5)

    def test_filtering(self):
        # we want to add .5 delays for *all* requests
        self.app.filter({'*': .5})

        # let see if it worked
        now = time.time()
        self.app.get('/buh', status=200)
        then = time.time()
        self.assertTrue(then - now >= .5)

        # we want to add .5 delays for *503* requests only
        self.app.filter({503: .5})

        # let see if it worked
        now = time.time()
        self.app.get('/buh', status=200)
        then = time.time()
        self.assertTrue(then - now < .5)

        self.app.mock(503)
        now = time.time()
        self.app.get('/buh', status=503)
        then = time.time()
        self.assertTrue(then - now >= .5)

        # let's remove the filters
        self.app.del_filters()

        self.app.mock(503)
        now = time.time()
        self.app.get('/buh', status=503)
        then = time.time()
        self.assertTrue(then - now < .5)

    def test_rec_flag(self):
        self.assertEquals(self.app.rec_status(), DISABLED)

        self.app.start_recording()
        self.assertEquals(self.app.rec_status(), RECORD)

        self.app.start_replaying()
        self.assertEquals(self.app.rec_status(), REPLAY)

        self.app.disable_recording()
        self.assertEquals(self.app.rec_status(), DISABLED)

    def _run_session(self, app=None):
        if app is None:
            app = self.app

        self.assertEquals(app.rec_status(), DISABLED)

        app.start_recording()
        self.assertEquals(app.rec_status(), RECORD)

        # recording three calls
        app.post('/1', params='tic')
        app.post('/2', params='tac')
        app.post('/3', params='toe2')
        app.post('/3', params='toe')

        # good. good. let's replay them
        app.start_replaying()
        self.assertEquals(app.rec_status(), REPLAY)

        old = SomeApp.__call__

        def badreq(*args):
            raise exc.HTTPBadRequest()

        SomeApp.__call__ = badreq

        try:
            self.assertEquals(app.post('/3', params='toe').body, 'toe')
            self.assertEquals(app.post('/2', params='tac').body, 'tac')
            self.assertEquals(app.post('/1', 'tic').body, 'tic')
            app.post('/buuhhh', status=400)
        finally:
            SomeApp.__call__ = old

    def test_auth(self):
        # secret activated by default
        oapp = ClientTesterMiddleware(SomeApp())

        # not using the secret
        app = TestAppPlus(oapp)
        app.get('/', status=200)
        self.assertRaises(AppError, self._run_session, app)

        # now using the secret
        app = TestAppPlus(oapp, secret='CHANGEME')
        app.get('/', status=200)
        self._run_session(app)