Best Python code snippet using Testify_python
test_wsgi.py
Source:test_wsgi.py  
1# -*- coding: utf-8 -*-2import unittest3import sys, os.path4import bottle5import urllib26from StringIO import StringIO7import thread8import time9from tools import ServerTestBase, tob10class TestWsgi(ServerTestBase):11    ''' Tests for WSGI functionality, routing and output casting (decorators) '''12    def test_get(self):13        """ WSGI: GET routes"""14        @bottle.route('/')15        def test(): return 'test'16        self.assertStatus(404, '/not/found')17        self.assertStatus(404, '/', post="var=value")18        self.assertBody('test', '/')19    def test_post(self):20        """ WSGI: POST routes"""21        @bottle.route('/', method='POST')22        def test(): return 'test'23        self.assertStatus(404, '/not/found')24        self.assertStatus(404, '/')25        self.assertBody('test', '/', post="var=value")26    def test_headget(self):27        """ WSGI: HEAD routes and GET fallback"""28        @bottle.route('/get')29        def test(): return 'test'30        @bottle.route('/head', method='HEAD')31        def test2(): return 'test'32        # GET -> HEAD33        self.assertStatus(404, '/head')34        # HEAD -> HEAD35        self.assertStatus(200, '/head', method='HEAD')36        self.assertBody('', '/head', method='HEAD')37        # HEAD -> GET38        self.assertStatus(200, '/get', method='HEAD')39        self.assertBody('', '/get', method='HEAD')40    def test_anymethod(self):41        self.assertStatus(404, '/any')42        @bottle.route('/any', method='ANY')43        def test2(): return 'test'44        self.assertStatus(200, '/any', method='HEAD')45        self.assertBody('test', '/any', method='GET')46        self.assertBody('test', '/any', method='POST')47        self.assertBody('test', '/any', method='DELETE')48        @bottle.route('/any', method='GET')49        def test2(): return 'test2'50        self.assertBody('test2', '/any', method='GET')51        @bottle.route('/any', method='POST')52        def test2(): return 'test3'53        self.assertBody('test3', '/any', method='POST')54        self.assertBody('test', '/any', method='DELETE')55    def test_500(self):56        """ WSGI: Exceptions within handler code (HTTP 500) """57        @bottle.route('/')58        def test(): return 1/059        self.assertStatus(500, '/')60    def test_503(self):61        """ WSGI: Server stopped (HTTP 503) """62        @bottle.route('/')63        def test(): return 'bla'64        self.assertStatus(200, '/')65        bottle.app().serve = False66        self.assertStatus(503, '/')67    def test_401(self):68        """ WSGI: abort(401, '') (HTTP 401) """69        @bottle.route('/')70        def test(): bottle.abort(401)71        self.assertStatus(401,'/')72        @bottle.error(401)73        def err(e):74            bottle.response.status = 20075            return str(type(e))76        self.assertStatus(200,'/')77        self.assertBody("<class 'bottle.HTTPError'>",'/')78    def test_303(self):79        """ WSGI: redirect (HTTP 303) """80        @bottle.route('/')81        def test(): bottle.redirect('/yes')82        self.assertStatus(303, '/')83        self.assertHeader('Location', 'http://127.0.0.1/yes', '/')84    def test_generator_callback(self):85        @bottle.route('/yield')86        def test():87            bottle.response.headers['Test-Header'] = 'test'88            yield 'foo'89        @bottle.route('/yield_nothing')90        def test2():91            yield92            bottle.response.headers['Test-Header'] = 'test'93        self.assertBody('foo', '/yield')94        self.assertHeader('Test-Header', 'test', '/yield')95        self.assertBody('', '/yield_nothing')96        self.assertHeader('Test-Header', 'test', '/yield_nothing')97    def test_cookie(self):98        """ WSGI: Cookies """99        @bottle.route('/cookie')100        def test():101            bottle.response.COOKIES['a']="a"102            bottle.response.set_cookie('b', 'b')103            bottle.response.set_cookie('c', 'c', path='/')104            return 'hello'105        try:106            c = self.urlopen('/cookie')['header'].get_all('Set-Cookie', '')107        except:108            c = self.urlopen('/cookie')['header'].get('Set-Cookie', '').split(',')109            c = [x.strip() for x in c]110        self.assertTrue('a=a' in c)111        self.assertTrue('b=b' in c)112        self.assertTrue('c=c; Path=/' in c)113class TestDecorators(ServerTestBase):114    ''' Tests Decorators '''115    def test_view(self):116        """ WSGI: Test view-decorator (should override autojson) """117        @bottle.route('/tpl')118        @bottle.view('stpl_t2main')119        def test():120            return dict(content='1234')121        result = '+base+\n+main+\n!1234!\n+include+\n-main-\n+include+\n-base-\n'122        self.assertHeader('Content-Type', 'text/html; charset=UTF-8', '/tpl')123        self.assertBody(result, '/tpl')124    def test_validate(self):125        """ WSGI: Test validate-decorator"""126        @bottle.route('/:var')127        @bottle.route('/')128        @bottle.validate(var=int)129        def test(var): return 'x' * var130        self.assertStatus(403,'/noint')131        self.assertStatus(403,'/')132        self.assertStatus(200,'/5')133        self.assertBody('xxx', '/3')134    def test_routebuild(self):135        """ WSGI: Test validate-decorator"""136        @bottle.route('/a/:b/c', name='named')137        def test(var): pass138        self.assertEqual('/a/xxx/c', bottle.url('named', b='xxx'))139        self.assertEqual('/a/xxx/c', bottle.app().get_url('named', b='xxx'))140    def test_decorators(self):141        app = bottle.Bottle()142        app.route('/g')('foo')143        bottle.route('/g')('foo')144        app.route('/g2', method='GET')('foo')145        bottle.get('/g2')('foo')146        app.route('/p', method='POST')('foo')147        bottle.post('/p')('foo')148        app.route('/p2', method='PUT')('foo')149        bottle.put('/p2')('foo')150        app.route('/d', method='DELETE')('foo')151        bottle.delete('/d')('foo')152        self.assertEqual(app.routes, bottle.app().routes)153    def test_autoroute(self):154        app = bottle.Bottle()155        def a(): pass156        def b(x): pass157        def c(x, y): pass158        def d(x, y=5): pass159        def e(x=5, y=6): pass160        self.assertEqual(['a'],list(bottle.yieldroutes(a)))161        self.assertEqual(['b/:x'],list(bottle.yieldroutes(b)))162        self.assertEqual(['c/:x/:y'],list(bottle.yieldroutes(c)))163        self.assertEqual(['d/:x','d/:x/:y'],list(bottle.yieldroutes(d)))164        self.assertEqual(['e','e/:x','e/:x/:y'],list(bottle.yieldroutes(e)))165class TestAppMounting(ServerTestBase):166    def setUp(self):167        ServerTestBase.setUp(self)168        self.subapp = bottle.Bottle()169    170    def test_basicmounting(self):171        bottle.app().mount(self.subapp, '/test')172        self.assertStatus(404, '/')173        self.assertStatus(404, '/test')174        self.assertStatus(404, '/test/')175        self.assertStatus(404, '/test/test/bar')176        @self.subapp.route('/')177        @self.subapp.route('/test/:test')178        def test(test='foo'):179            return test180        self.assertStatus(404, '/')181        self.assertStatus(404, '/test')182        self.assertStatus(200, '/test/')183        self.assertBody('foo', '/test/')184        self.assertStatus(200, '/test/test/bar')185        self.assertBody('bar', '/test/test/bar')186    187if __name__ == '__main__':...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
