def test_request_charset(encoding, expected): app = Burette() @app.route('/get_url', method='POST') def get_url(request): assert(request.charset == expected) assert(request.text == 'あいうえお') assert(request.body.decode(encoding=expected) == 'あいうえお') return 'かきくけこ' class Reader: def read(self, length): return 'あいうえお'.encode(encoding=expected) def start_response_callback(status, headers): assert(status == '200 OK') found = False for header_name, header_value in headers: if header_name == 'Content-Type': found = True assert(header_value == 'text/html; charset=' + expected) assert found content_type = 'text/plain; charset=' + encoding if encoding is not None else 'text/plain' env = { 'REQUEST_METHOD': 'POST', 'PATH_INFO': '/get_url', 'CONTENT_TYPE': content_type, 'wsgi.input': Reader() } app(env, start_response_callback)
def test_isp(): app = Burette() @app.route('/get_url') def get_url(): raise Exception() def start_response_callback(status, headers): assert(status == '500 Internal Server Error') env = { 'REQUEST_METHOD': 'GET', 'PATH_INFO': '/get_url' } app(env, start_response_callback)
def test_request_path_params_not_found(route_path, request_path): app = Burette() @app.route(route_path) def get_url(): assert False return '' def start_response_callback(status, headers): assert(status == '404 Not Found') env = { 'REQUEST_METHOD': 'GET', 'PATH_INFO': request_path, } app(env, start_response_callback)
def test_request(route_path, route_method): app = Burette() @app.route(route_path, method=route_method) def get_url(request): assert(request.path == route_path) assert(request.method == route_method) return '' def start_response_callback(status, headers): assert(status == '200 OK') env = { 'REQUEST_METHOD': route_method, 'PATH_INFO': route_path } app(env, start_response_callback)
def test_route(route_path, route_method, request_path, request_method, expected): app = Burette() @app.route(route_path, method=route_method) def test_routing(): pass def start_response_callback(status, headers): assert(status == expected) env = { 'REQUEST_METHOD': request_method, 'PATH_INFO': request_path } app(env, start_response_callback)
def test_request_path_params(route_path, request_path, path_params): app = Burette() @app.route(route_path) def get_url(request): assert(request.path == request_path) assert(request.method == 'GET') assert(request.path_params == path_params) return '' def start_response_callback(status, headers): assert(status == '200 OK') env = { 'REQUEST_METHOD': 'GET', 'PATH_INFO': request_path, } app(env, start_response_callback)
from burette import Burette, redirect, jinja2 app = Burette() @app.route('/hoge/path') def hogepath(req): q = req.params.get('q', [''])[0] return "<html><body>my first app: q = " + q + \ "<form method='POST' action='/post'><input name=mytext type=text><input name='POST' type=submit></form>" + \ "</body></html>" @app.route('/empty') def empty(): return "<html><body>" \ "<form method='POST' action='/post'><input name=mytext type=text><input name='POST' type=submit></form>" + \ "</body></html>" @app.route('/post', method='POST') def post_route(request): return "<html><body> posted body = " + request.text + \ "<form method='POST' action='/post'><input name=mytext type=text><input name='POST' type=submit></form>" + \ "</body></html>" @app.route('/json') def give_me_json(): return {'a': 'b', 'aa': [1, 2, 3]}