def test_no_schema(self): # Use the decorator without the @ syntax: self.callback = rest.schema()(self.callback) self.root_app.route('/foo', callback=self.callback) result = self.app.get('/foo') self.assertEqual(dict(body=None, query=None), result.json)
def test_no_schema(self): # Use the decorator without the @ syntax: self.callback = rest.schema()(self.callback) self.root_app.route('/foo', callback=self.callback) result = self.app.get('/foo') self.assertEqual(dict(), result.json)
def test_body_required_body_empty(self): self.callback = rest.schema(body_required=True)(self.callback) self.root_app.route('/foo', method='POST', callback=self.callback) result = self.app.post_json('/foo', expect_errors=True) self.assertEqual(400, result.status_int)
def test_no_schema_with_query(self): self.callback = rest.schema(query_schema=lambda _x: _x)(self.callback) self.root_app.route('/foo', callback=self.callback) result = self.app.get('/foo?a=1&b=2&a=foo') self.assertEqual(dict(query=dict(b=['2'], a=['1', 'foo'])), result.json)
def test_no_schema_with_query(self): self.callback = rest.schema()(self.callback) self.root_app.route('/foo', callback=self.callback) result = self.app.get('/foo?a=1&b=2&a=foo') self.assertEqual( dict(body=None, query=dict(b=['2'], a=['1', 'foo'])), result.json )
def test_query_schema(self): self.callback = rest.schema( query_schema=volup.Schema({ 'a': rest.coerce_one(str), 'b': rest.coerce_many(int), }))(self.callback) self.root_app.route('/foo', callback=self.callback) result = self.app.get('/foo?a=foo&b=1&b=2&b=3') self.assertEqual(dict(query=dict(a='foo', b=[1, 2, 3])), result.json)
def test_query_schema_fail(self): self.callback = rest.schema(query_schema=volup.Schema({ 'a': rest.coerce_one(str), 'b': rest.coerce_many(int), }))(self.callback) self.root_app.route('/foo', callback=self.callback) # There are two values for `a`. We only expect one. result = self.app.get('/foo?a=foo&b=bar&b=2&b=3', expect_errors=True) self.assertEqual(400, result.status_int)
def test_body_schema_no_body(self): body_schema = volup.Schema({ 'a': int, 'b': [str], }) self.callback = rest.schema(body_schema=body_schema)(self.callback) self.root_app.route('/foo', method='POST', callback=self.callback) # Empty request body result = self.app.post_json('/foo', expect_errors=True) self.assertEqual(400, result.status_int)
def test_no_schema_with_body_and_query(self): self.callback = rest.schema()(self.callback) self.root_app.route('/foo', method='POST', callback=self.callback) result = self.app.post_json( '/foo?a=1&b=2&a=foo', dict(d='3', e=['4', 'bar']) ) self.assertEqual( {'body': {'d': '3', 'e': ['4', 'bar']}, 'query': {'a': ['1', 'foo'], 'b': ['2']}}, result.json )
def test_query_schema(self): self.callback = rest.schema(query_schema=volup.Schema({ 'a': rest.coerce_one(str), 'b': rest.coerce_many(int), }))(self.callback) self.root_app.route('/foo', callback=self.callback) result = self.app.get('/foo?a=foo&b=1&b=2&b=3') self.assertEqual( dict(body=None, query=dict(a='foo', b=[1, 2, 3])), result.json )
def test_body_schema_with_body(self): body_schema = volup.Schema({ 'a': volup.Coerce(int), 'b': [volup.Coerce(str)], }) self.callback = rest.schema(body_schema=body_schema)(self.callback) self.root_app.route('/foo', method='POST', callback=self.callback) result = self.app.post_json('/foo', dict(a='1', b=['foo', 'bar']), expect_errors=True) self.assertEqual( # Note that `a` in the body is converted to an int. dict(body=dict(a=1, b=['foo', 'bar'])), result.json)
def test_body_schema_with_body(self): body_schema = volup.Schema({ 'a': volup.Coerce(int), 'b': [volup.Coerce(str)], }) self.callback = rest.schema(body_schema=body_schema)(self.callback) self.root_app.route('/foo', method='POST', callback=self.callback) result = self.app.post_json('/foo', dict(a='1', b=['foo', 'bar']), expect_errors=True) self.assertEqual( # Note that `a` in the body is converted to an int. dict(body=dict(a=1, b=['foo', 'bar']), query=None), result.json )
def test_no_schema_with_body(self): self.callback = rest.schema(body_required=True)(self.callback) self.root_app.route('/foo', method='POST', callback=self.callback) result = self.app.post_json('/foo', params={'a': 1}) self.assertEqual(dict(body={'a': 1}), result.json)
def test_no_schema_with_body(self): self.callback = rest.schema()(self.callback) self.root_app.route('/foo', method='POST', callback=self.callback) result = self.app.post_json('/foo', params={'a': 1}) self.assertEqual(dict(body={'a': 1}, query=None), result.json)