def test_get_json_body_with_required_keys(): data = {'test': 'value', 'a': 1, 'b': 2} handler = base.JsonHandler(MagicMock(), MagicMock()) handler.request.body = json.dumps(data) handler.request.headers = {'Content-Type': 'application/json'} result = handler.get_json_body(required=['a', 'b']) assert result == data
def test_get_missing_json_body(): handler = base.JsonHandler(MagicMock(), MagicMock()) handler.request.headers = {'Content-Type': 'application/json'} with pytest.raises(base.HTTPError) as exc: handler.get_json_body() assert exc.value.status_code == 400 assert exc.value.errors == 'Error parsing JSON'
def test_get_valid_json_body(): data = {'test': 'value'} handler = base.JsonHandler(MagicMock(), MagicMock()) handler.request.body = json.dumps(data) handler.request.headers = {'Content-Type': 'application/json'} result = handler.get_json_body() assert result == data
def test_missing_content_type(): """If content type is not set, then should default to application/json""" handler = base.JsonHandler(MagicMock(), MagicMock()) data = {'test': 'value'} handler.request.body = json.dumps(data) handler.request.headers = {} result = handler.get_json_body() assert result == data
def test_get_json_charset_in_content_type(): handler = base.JsonHandler(MagicMock(), MagicMock()) data = {'test': 'value'} handler.request.body = json.dumps(data) handler.request.headers = { 'Content-Type': 'application/json; charset=UTF-8' } result = handler.get_json_body() assert result == data
def test_get_json_body_missing_required_keys(): data = {'test': 'value'} handler = base.JsonHandler(MagicMock(), MagicMock()) handler.request.body = json.dumps(data) handler.request.headers = {'Content-Type': 'application/json'} with pytest.raises(base.HTTPError) as exc: handler.get_json_body(required=['a', 'b']) assert exc.value.status_code == 400 assert len(exc.value.errors) == 2
def test_get_json_wrong_content_type(): handler = base.JsonHandler(MagicMock(), MagicMock()) data = {'test': 'value'} handler.request.body = json.dumps(data) handler.request.headers = {'Content-Type': 'application/jsonatoehus'} with pytest.raises(base.HTTPError) as exc: handler.get_json_body() assert exc.value.status_code == 415 assert exc.value.errors == 'Content-Type should be application/json'
def test_get_json_body_with_valid_keys(): data = {'test': 'value', 'a': 1, 'b': 2} handler = base.JsonHandler(MagicMock(), MagicMock()) handler.request.body = json.dumps(data) handler.request.headers = {'Content-Type': 'application/json'} def an_int(x): return isinstance(x, int) result = handler.get_json_body(validators={'a': an_int, 'b': an_int}) assert result == data
def test_get_json_body_with_invalid_keys(): data = {'test': 'value', 'a': 1, 'b': 2} handler = base.JsonHandler(MagicMock(), MagicMock()) handler.request.body = json.dumps(data) handler.request.headers = {'Content-Type': 'application/json'} def a_string(x): return isinstance(x, basestring) with pytest.raises(base.HTTPError) as exc: result = handler.get_json_body(validators={ 'a': a_string, 'b': a_string }) assert exc.value.status_code == 400 assert len(exc.value.errors) == 2
def test_http_error(): data = {'test': 'value'} handler = base.JsonHandler(MagicMock(), MagicMock()) handler.request.body = json.dumps(data) handler.request.headers = {'Content-Type': 'application/json'}