def tearDown(self): while self.p.poll() is None: os.kill(self.p.pid, signal.SIGINT) time.sleep(0.1) if self.p.poll() is None: os.kill(self.p.pid, signal.SIGTERM) for stream in (self.p.stdout, self.p.stderr): for line in stream: if tob('Warning') in line \ or tob('Error') in line or True: print line.strip().decode('utf8')
def test_default(self): """ WSGI: default routes """ @bottle.route('/') def test2(): return 'test' self.assertEqual(404, self.urlopen('/not/found').code) self.assertEqual(200, self.urlopen('/').code) self.assertEqual(tob('test'), self.urlopen('/').read()) @bottle.default() def test(): return 'default' self.assertEqual(200, self.urlopen('/not/found').code) self.assertEqual(tob('default'), self.urlopen('/not/found').read()) self.assertEqual(200, self.urlopen('/').code) self.assertEqual(tob('test'), self.urlopen('/').read())
def testWithBottle(self): bottle.app.push() bottle.response.bind(bottle.app()) bottle.response.set_cookie('key', dict(value=5), secret=tob('1234')) cheader = [ v for k, v in bottle.response.wsgiheader() if k == 'Set-Cookie' ][0] bottle.request.bind({'HTTP_COOKIE': cheader.split(';')[0]}, bottle.app()) self.assertEqual( repr(dict(value=5)), repr(bottle.request.get_cookie('key', secret=tob('1234')))) bottle.app.pop()
def test_headget(self): """ WSGI: HEAD routes and GET fallback""" @bottle.route('/get') def test(): return 'test' @bottle.route('/head', method='HEAD') def test2(): return 'test' # GET -> HEAD self.assertEqual(404, self.urlopen('/head').code) # HEAD -> HEAD self.assertEqual(200, self.urlopen('/head', method='HEAD').code) self.assertEqual(tob(''), self.urlopen('/head', method='HEAD').read()) # HEAD -> GET self.assertEqual(200, self.urlopen('/get', method='HEAD').code) self.assertEqual(tob(''), self.urlopen('/get', method='HEAD').read())
def tearDown(self): if self.skip: return for i in range(10): if self.p.poll() != None: break os.kill(self.p.pid, signal.SIGINT) time.sleep(0.1 * i) for i in range(10): if self.p.poll() != None: break os.kill(self.p.pid, signal.SIGINT) time.sleep(i) for stream in (self.p.stdout, self.p.stderr): for line in stream: if tob('warning') in line.lower(): tools.warn(line.strip().decode('utf8')) elif tob('error') in line.lower(): raise AssertionError(line.strip().decode('utf8'))
def tearDown(self): if self.skip: return if self.p.poll() == None: os.kill(self.p.pid, signal.SIGINT) time.sleep(0.5) while self.p.poll() == None: os.kill(self.p.pid, signal.SIGTERM) time.sleep(1) for stream in (self.p.stdout, self.p.stderr): for line in stream: if tob('warning') in line.lower(): tools.warn(line.strip().decode('utf8')) elif tob('error') in line.lower(): raise AssertionError(line.strip().decode('utf8'))
def tearDown(self): if self.skip: return for i in range(10): if self.p.poll() != None: break os.kill(self.p.pid, signal.SIGINT) time.sleep(0.1*i) for i in range(10): if self.p.poll() != None: break os.kill(self.p.pid, signal.SIGINT) time.sleep(i) for stream in (self.p.stdout, self.p.stderr): for line in stream: if tob('warning') in line.lower(): tools.warn(line.strip().decode('utf8')) elif tob('error') in line.lower(): raise AssertionError(line.strip().decode('utf8'))
def test_post(self): """ WSGI: POST routes""" @bottle.route('/', method='POST') def test(): return 'test' self.assertEqual(404, self.urlopen('/not/found').code) self.assertEqual(404, self.urlopen('/').code) self.assertEqual(tob('test'), self.urlopen('/', post="var=value").read())
def testWithBottle(self): bottle.app.push() bottle.response.bind() bottle.response.set_cookie('key', dict(value=5), secret=tob('1234')) cheader = [v for k, v in bottle.response.wsgiheader() if k == 'Set-Cookie'][0] bottle.request.bind({'HTTP_COOKIE': cheader.split(';')[0]}) self.assertEqual(repr(dict(value=5)), repr(bottle.request.get_cookie('key', secret=tob('1234')))) bottle.app.pop()
def test_casting(self): """ WSGI: Output Casting (strings an lists) """ @bottle.route('/str') def test(): return 'test' self.assertEqual(tob('test'), self.urlopen('/str').read()) @bottle.route('/list') def test2(): return ['t','e','st'] self.assertEqual(tob('test'), self.urlopen('/list').read()) @bottle.route('/empty') def test3(): return [] self.assertEqual(tob(''), self.urlopen('/empty').read()) @bottle.route('/none') def test4(): return None self.assertEqual(tob(''), self.urlopen('/none').read()) @bottle.route('/bad') def test5(): return 12345 self.assertEqual(500, self.urlopen('/bad').code)
def test_303(self): """ WSGI: redirect (HTTP 303) """ @bottle.route('/') def test(): bottle.redirect('/yes') @bottle.route('/yes') def test2(): return 'yes' self.assertEqual(200, self.urlopen('/').code) self.assertEqual(tob('yes'), self.urlopen('/').read())
def test_view(self): """ WSGI: Test view-decorator (should override autojson) """ @bottle.route('/tpl') @bottle.view('stpl_t2main') def test(): return dict(content='1234') result = tob('+base+\n+main+\n!1234!\n+include+\n-main-\n+include+\n-base-\n') self.assertEqual('text/html; charset=UTF-8', self.urlopen('/tpl').info().get('Content-Type','')) self.assertEqual(result, self.urlopen('/tpl').read())
def test_validate(self): """ WSGI: Test validate-decorator""" @bottle.route('/:var') @bottle.route('/') @bottle.validate(var=int) def test(var): return 'x' * var self.assertEqual(403, self.urlopen('/noint').code) self.assertEqual(403, self.urlopen('/').code) self.assertEqual(200, self.urlopen('/5').code) self.assertEqual(tob('xxx'), self.urlopen('/3').read())
def test_401(self): """ WSGI: abort(401, '') (HTTP 401) """ @bottle.route('/') def test(): bottle.abort(401) self.assertEqual(401, self.urlopen('/').code) @bottle.error(401) def err(e): bottle.response.status = 200 return str(type(e)) self.assertEqual(200, self.urlopen('/').code) self.assertEqual(tob("<class 'bottle.HTTPError'>"), self.urlopen('/').read())
def tearDown(self): if self.skip: return if self.p.poll() == None: os.kill(self.p.pid, signal.SIGINT) time.sleep(0.5) if self.p.poll() == None: os.kill(self.p.pid, signal.SIGTERM) time.sleep(0.5) while self.p.poll() == None: tools.warn("Trying to kill server %r with pid %d." % (self.server, self.p.pid)) os.kill(self.p.pid, signal.SIGKILL) time.sleep(1) lines = [line for stream in (self.p.stdout, self.p.stderr) for line in stream] for line in lines: if tob('warning') in line.lower(): tools.warn(line.strip().decode('utf8')) elif tob('error') in line.lower(): raise AssertionError(line.strip().decode('utf8'))
def test_params(self): """ Environ: GET and POST are combined in request.param """ e = {} wsgiref.util.setup_testing_defaults(e) e['wsgi.input'].write(tob('b=b&c=p')) e['wsgi.input'].seek(0) e['CONTENT_LENGTH'] = '7' e['QUERY_STRING'] = 'a=a&c=g' e['REQUEST_METHOD'] = "POST" request.bind(e, None) self.assertEqual(['a','b','c'], sorted(request.params.keys())) self.assertEqual('p', request.params['c'])
def test_params(self): """ Environ: GET and POST are combined in request.param """ e = {} wsgiref.util.setup_testing_defaults(e) e['wsgi.input'].write(tob('b=b&c=p')) e['wsgi.input'].seek(0) e['CONTENT_LENGTH'] = '7' e['QUERY_STRING'] = 'a=a&c=g' e['REQUEST_METHOD'] = "POST" request.bind(e, None) self.assertEqual(['a', 'b', 'c'], sorted(request.params.keys())) self.assertEqual('p', request.params['c'])
def tearDown(self): if self.skip: return if self.p.poll() == None: os.kill(self.p.pid, signal.SIGINT) time.sleep(0.5) if self.p.poll() == None: os.kill(self.p.pid, signal.SIGTERM) time.sleep(0.5) while self.p.poll() == None: tools.warn("Trying to kill server %r with pid %d." % (self.server, self.p.pid)) os.kill(self.p.pid, signal.SIGKILL) time.sleep(1) lines = [ line for stream in (self.p.stdout, self.p.stderr) for line in stream ] for line in lines: if tob('warning') in line.lower(): tools.warn(line.strip().decode('utf8')) elif tob('error') in line.lower(): raise AssertionError(line.strip().decode('utf8'))
def test_bytes(self): self.app.route('/')(lambda: tob('test')) self.assertBody('test')
def setUp(self): self.data = dict(a=5, b=u'unicode', c=[1, 2, 3, 4, tob('bytestring')]) self.key = tob('secret')
def test_json(self): """ WSGI: Autojson feature """ @bottle.route('/json') def test(): return {'a': 1} self.assertEqual(tob(r'{"a":1}'), tob('').join(self.urlopen('/json').read().split())) self.assertEqual('application/json', self.urlopen('/json').info().get('Content-Type',''))
def testIsEncoded(self): cookie = bottle.cookie_encode(self.data, self.key) self.assertTrue(bottle.cookie_is_encoded(cookie)) self.assertFalse(bottle.cookie_is_encoded(tob('some string')))
def testDeEncode(self): cookie = bottle.cookie_encode(self.data, self.key) decoded = bottle.cookie_decode(cookie, self.key) self.assertEqual(self.data, decoded) decoded = bottle.cookie_decode(cookie + tob('x'), self.key) self.assertEqual(None, decoded)
def test_test(self): ''' Test a simple static page with this server adapter. ''' if self.p.poll() == None: self.assertEqual(tob('OK'), self.fetch('test'))
def setUp(self): self.data = dict(a=5, b=u'unicode', c=[1,2,3,4,tob('bytestring')]) self.secret = tob('secret') bottle.app.push() bottle.response.bind()
def testDeEncode(self): cookie = bottle.cookie_encode(self.data, self.key) decoded = bottle.cookie_decode(cookie, self.key) self.assertEqual(self.data, decoded) decoded = bottle.cookie_decode(cookie+tob('x'), self.key) self.assertEqual(None, decoded)
def setUp(self): self.data = dict(a=5, b=u'unicode', c=[1,2,3,4,tob('bytestring')]) self.key = tob('secret')
def test_simple(self): ''' Test a simple static page with this server adapter. ''' if self.skip: return self.assertEqual(tob('OK'), self.fetch('test'))
def test_file(self): """ WSGI: Output Casting (files) """ @bottle.route('/file') def test(): return StringIO('test') self.assertEqual(tob('test'), self.urlopen('/file').read())