def validate_router_params(params): print(params) schema = Schema({ Required("lat"): All(int, Range(min=-90, max=90)), Required("lon"): All(int, Range(min=-180, max=180)), }, extra=ALLOW_EXTRA) schema(params)
def test_bad_location(app): bad_location = 'bad_location' with pytest.raises(AttributeError) as excinfo: @expect(Schema({}), bad_location) def func(bad_location): pass assert '{} location not admitted'.format(bad_location) == str( excinfo.value)
def test_expected_decorator_form_location(app): schema = Schema({ Required('text'): str, Required('per_page', default=5): int }) @expect(schema, 'form') def func(form): assert form['text'] == 'testo' assert form['per_page'] == 5 with app.test_request_context('/', method='POST', data={'text': 'testo'}): ret = func()
def test_expected_args_decorator(app): schema = Schema({ Required('text'): str, Required('per_page', default=5): int }) @expect(schema, 'args') def func(args): assert args['text'] == 'testo' assert args['per_page'] == 5 with app.test_request_context('/?text=testo'): ret = func()
def test_expect_bad_request(app): schema = Schema({Required('text'): str, Required('per_page'): int}) @expect(schema, 'json') def func(json): assert json['text'] == 'testo' assert json['per_page'] == 5 with app.test_request_context(data=json.dumps({'text': 'testo'}), headers={'content-type': 'application/json'}): with pytest.raises(NotAcceptable) as excinfo: ret = func() assert '406 Not Acceptable: required key not provided @ data[\'per_page\']' == str( excinfo.value)
def test_expect_json_location(app): schema = Schema({ Required('text'): str, Required('per_page', default=5): int }) @expect(schema, 'json') def func(json): assert json['text'] == 'testo' assert json['per_page'] == 5 with app.test_request_context(data=json.dumps({'text': 'testo'}), headers={'content-type': 'application/json'}): ret = func()
abort(406, str(e)) #kargs["args"] = args_filtered return f(*args, **kargs) return update_wrapper(wrapper, f) return decorator @app.route('/') def index(): return 'Hello, World!' @app.route("/router") @myexpect(Schema({ #"lat": All(float, Range(min=-90, max=90)), #'lat': All(str,str), #'lon': All(str,str), 'lat': float, 'lon': float, #"lon": All(int, Range(min=-180, max=180)), #Required("lat"): All(int, Range(min=-90, max=90)), #Required("lon"): All(int, Range(min=-180, max=180)), }, extra=REMOVE_EXTRA)) def myrouter(lat: float, lon: float): print("type(lat)=" + str(type(lat))) print("request=" + str(request)) print("request.args=" + str(getattr(request,"args"))) print("request.json=" + str(request.get_json())) #print("schema(req)=" + schema(request.args.to_dict())) lat = request.args.get("lat", "") lon = request.args.get("lon", "") #validate_router_params(request.args) return "This is router. lat=" + lat + " and lon=" + lon