Esempio n. 1
0
    def testProcessorExceptions(self):
        app = shimehari.Shimehari(__name__)

        @app.beforeRequest
        def beforeReq():
            if trigger == 'before':
                1 / 0

        @app.afterRequest
        def afterRequest(response):
            if trigger == 'after':
                1 / 0
            return response

        def index():
            return 'Foo'

        app.router = shimehari.Router(
            [Rule('/', endpoint='index', methods=['GET'])])
        app.controllers['index'] = index

        @app.errorHandler(500)
        def internalServerError(e):
            return 'Hello Server Error', 500

        for trigger in 'before', 'after':
            rv = app.testClient().get('/')
            self.assertEqual(rv.status_code, 500)
            self.assertEqual(rv.data, 'Hello Server Error')
Esempio n. 2
0
    def testJsoniFy(self):
        d = dict(a=23, b=42, c=[1, 2, 3])
        ConfigManager.addConfig(testConfig)
        app = shimehari.Shimehari(__name__)

        #hum
        def returnKwargs():
            return shimehari.jsonify(**d)

        def returnDict():
            return shimehari.jsonify(d)

        app.router = shimehari.Router([
            Rule('/kw', endpoint='returnKwargs', methods=['GET']),
            Rule('/dict', endpoint='returnDict', methods=['GET'])
        ])
        app.controllers['returnKwargs'] = returnKwargs
        app.controllers['returnDict'] = returnDict

        c = app.testClient()
        for url in '/kw', '/dict':
            rv = c.get(url)
            print rv.mimetype
            self.assertEqual(rv.mimetype, 'application/json')
            self.assertEqual(shimehari.json.loads(rv.data), d)
Esempio n. 3
0
    def testDebugLog(self):
        app = shimehari.Shimehari(__name__)
        app.debug = True

        def index():
            app.logger.warning('the standard library is dead')
            app.logger.debug('this is a debug statement')
            return ''

        def exc():
            1 / 0

        app.router = shimehari.Router([
            Rule('/', endpoint='index', methods=['GET']),
            Rule('/exc', endpoint='exc', methods=['GET']),
        ])
        app.controllers['index'] = index
        app.controllers['exc'] = exc
        with app.testClient() as c:
            with catchStdErr() as err:
                c.get('/')
                out = err.getvalue()
                self.assert_('WARNING' not in out)
                self.assert_(
                    os.path.basename(__file__.rsplit('.', 1)[0] +
                                     '.py') not in out)
                self.assert_('the standard library is dead' not in out)
                self.assert_('this is a debug statement' not in out)
            with catchStdErr() as err:
                try:
                    c.get('/exc')
                except ZeroDivisionError:
                    pass
                else:
                    self.assert_(False, 'debug log ate the exception')
Esempio n. 4
0
    def testGotFirstRequest(self):
        ConfigManager.addConfig(testConfig)
        app = shimehari.Shimehari(__name__)
        self.assertEqual(app.gotFirstRequest, False)

        def returnHello(*args, **kwargs):
            return 'Hello'
        app.router = shimehari.Router([Rule('/hell', endpoint='returnHello', methods=['POST'])])
        app.controllers['returnHello'] = returnHello
        c = app.testClient()
        c.get('/hell', content_type='text/planetext')
        self.assert_(app.gotFirstRequest)
Esempio n. 5
0
    def testGenerateURL(self):
        ConfigManager.addConfig(testConfig)
        app = shimehari.Shimehari(__name__)

        def index(*args, **kwargs):
            return 'index'

        app.router = shimehari.Router(
            [Rule('/', endpoint='index', methods=['GET'])])

        with app.appContext():
            rv = shimehari.urlFor('index')
            self.assertEqual(rv, 'https://localhost/')
Esempio n. 6
0
    def testURLForWithAnchro(self):
        app = shimehari.Shimehari(__name__)

        def index():
            return '42'

        app.router = shimehari.Router(
            [Rule('/', endpoint='index', methods=['GET'])])
        app.controllers['index'] = index

        with app.testRequestContext():
            self.assertEqual(shimehari.urlFor('index', _anchor='x y'),
                             '/#x%20y')
Esempio n. 7
0
    def testJSONBadRequests(self):
        ConfigManager.addConfig(testConfig)
        app = shimehari.Shimehari(__name__)

        def returnJSON(*args, **kwargs):
            return unicode(shimehari.request.json)

        app.router = shimehari.Router(
            [Rule('/json', endpoint='returnJSON', methods=['POST'])])
        app.controllers['returnJSON'] = returnJSON
        c = app.testClient()
        rv = c.post('/json', data='malformed', content_type='application/json')
        self.assertEqual(rv.status_code, 400)
Esempio n. 8
0
    def jsonBodyEncoding(self):
        ConfigManager.addConfig(testConfig)
        app = shimehari.Shimehari(__name__)

        app.testing = True

        def returnJSON(*args, **kwargs):
            return shimehari.request.json

        app.router = shimehari.Router(
            [Rule('/json', endpoint='returnJSON', methods=['GET'])])
        app.controllers['returnJSON'] = returnJSON
        c = app.testClient()
        resp = c.get('/',
                     data=u"はひ".encode('iso-8859-15'),
                     content_type='application/json; charset=iso-8859-15')
        self.assertEqual(resp.data, u'はひ'.encode('utf-8'))
Esempio n. 9
0
    def testModifiedURLEncoding(self):
        class ModifiedRequest(shimehari.Request):
            url_charset = 'euc-jp'

        app = shimehari.Shimehari(__name__)
        app.requestClass = ModifiedRequest
        app.router.charset = 'euc-jp'

        def index():
            return shimehari.request.args['foo']

        app.router = shimehari.Router(
            [Rule('/', endpoint='index', methods=['GET'])])
        app.controllers['index'] = index

        rv = app.testClient().get(u'/?foo=ほげほげ'.encode('euc-jp'))
        self.assertEqual(rv.status_code, 200)
        self.assertEqual(rv.data, u'ほげほげ'.encode('utf-8'))
Esempio n. 10
0
    def testExceptionLogging(self):
        out = StringIO()
        app = shimehari.Shimehari(__name__)
        app.loggerName = 'shimehariaaa'
        app.logger.addHandler(StreamHandler(out))

        def index():
            1 / 0

        app.router = shimehari.Router(
            [Rule('/', endpoint='index', methods=['GET'])])
        app.controllers['index'] = index

        rv = app.testClient().get('/')
        self.assertEqual(rv.status_code, 500)
        self.assert_('Internal Server Error' in rv.data)

        err = out.getvalue()
        self.assert_('ZeroDivisionError: ' in err)
Esempio n. 11
0
    def testJSONAttr(self):
        ConfigManager.addConfig(testConfig)
        app = shimehari.Shimehari(__name__)

        def returnJSON(*args, **kwargs):
            return unicode(shimehari.request.json['a'] +
                           shimehari.request.json['b'])

        app.router = shimehari.Router(
            [Rule('/add', endpoint='returnJSON', methods=['POST'])])
        app.controllers['returnJSON'] = returnJSON

        c = app.testClient()
        rv = c.post('/add',
                    data=shimehari.json.dumps({
                        'a': 1,
                        'b': 2
                    }),
                    content_type='application/json')
        self.assertEqual(rv.data, '3')
Esempio n. 12
0
    def testTryTriggerBeforeFirstRequest(self):
        ConfigManager.removeConfig('development')
        ConfigManager.addConfig(testConfig)
        app = shimehari.Shimehari(__name__)

        app.testCnt = 0

        @app.beforeFirstRequest
        def doFirst():
            app.testCnt = app.testCnt + 1
            return app.testCnt

        def returnHello(*args, **kwargs):
            return 'Hello'
        app.router = shimehari.Router([Rule('/hell', endpoint='returnHello', methods=['POST'])])
        app.controllers['returnHello'] = returnHello
        c = app.testClient()
        self.assertEqual(app.testCnt, 0)
        c.get('/hell', content_type='text/planetext')
        self.assertEqual(app.testCnt, 1)
        c.get('/hell', content_type='text/planetext')
        self.assertEqual(app.testCnt, 1)
Esempio n. 13
0
    def testSuperessedExceptionLogging(self):
        class SupressedShimehari(shimehari.Shimehari):
            def logException(self, exc_info):
                pass

        out = StringIO()
        app = SupressedShimehari(__name__)
        app.loggerName = 'shimehariTests/test'
        app.logger.addHandler(StreamHandler(out))

        def index():
            1 / 0

        app.router = shimehari.Router(
            [Rule('/', endpoint='index', methods=['GET'])])
        app.controllers['index'] = index

        rv = app.testClient().get('/')
        self.assertEqual(rv.status_code, 500)
        self.assert_('Internal Server Error' in rv.data)

        err = out.getvalue()
        self.assertEqual(err, '')