示例#1
0
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
示例#2
0
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'
示例#3
0
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
示例#4
0
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
示例#5
0
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
示例#6
0
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
示例#7
0
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'
示例#8
0
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
示例#9
0
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
示例#10
0
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'}