Exemple #1
0
def test_assert_response_bad_status_code_blind():
    """Different status code than expected, no more info."""
    response = create_response(status_code=500, raw_content=b"stuff")
    with pytest.raises(CommandError) as cm:
        assert_response_ok(response)
    assert str(cm.value) == (
        "Wrong status code from server (expected=200, got=500) errors=None headers={}"
    )
Exemple #2
0
def test_assert_response_errors_in_result():
    """Response is as expected but server flags errors."""
    errors = [{"foo": "bar"}]
    test_content = {"errors": errors}
    response = create_response(json_content=test_content)
    with pytest.raises(CommandError) as cm:
        assert_response_ok(response)
    assert str(
        cm.value) == "Response with errors from server: {}".format(errors)
Exemple #3
0
def test_assert_response_bad_status_code_with_json_errors():
    """Different status code than expected, with the server including errors."""
    errors = [{"foo": "bar"}]
    test_content = {"errors": errors}
    response = create_response(status_code=404, json_content=test_content)
    with pytest.raises(CommandError) as cm:
        assert_response_ok(response)
    assert str(cm.value) == (
        "Wrong status code from server (expected=200, got=404) errors={} "
        "headers={{'Content-Type': 'application/json'}}".format(errors))
def test_assert_response_bad_status_code_with_json_errors():
    """Different status code than expected, with the server including errors."""
    errors = [{"foo": "bar"}]
    test_content = {"errors": errors}
    response = create_response(status_code=404, json_content=test_content)
    with pytest.raises(CraftError) as cm:
        assert_response_ok(response)
    error = cm.value
    assert str(
        error) == "Wrong status code from server (expected=200, got=404)"
    assert error.details == f"errors={errors} headers={{'Content-Type': 'application/json'}}"
Exemple #5
0
def test_assert_response_bad_status_code_with_extra_json_errors():
    """The server still including errors, weird content type."""
    errors = [{"foo": "bar"}]
    test_content = {"errors": errors}
    response = create_response(
        status_code=404,
        json_content=test_content,
        content_type="application/json;stuff",
    )
    with pytest.raises(CommandError) as cm:
        assert_response_ok(response)
    assert str(cm.value) == (
        "Wrong status code from server (expected=200, got=404) errors={} "
        "headers={{'Content-Type': 'application/json;stuff'}}".format(errors))
Exemple #6
0
def test_assert_response_ok_different_status():
    """A good response with a different status code."""
    test_content = {"foo": 2, "bar": 1}
    response = create_response(json_content=test_content, status_code=201)
    result = assert_response_ok(response, expected_status=201)
    assert result == test_content
Exemple #7
0
def test_assert_response_ok_not_json():
    """A good non-json response."""
    response = create_response(raw_content=b"stuff")
    result = assert_response_ok(response)
    assert result is None
Exemple #8
0
def test_assert_response_ok_simple_json():
    """Simple case for a good response with JSON content."""
    test_content = {"foo": 2, "bar": 1}
    response = create_response(json_content=test_content)
    result = assert_response_ok(response)
    assert result == test_content