예제 #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'}