def test_handles_unsupported_http_methods():
    with raises(HttpQueryError) as exc_info:
        run_http_query(schema, "put", None)

    assert exc_info.value == HttpQueryError(
        405,
        "GraphQL only supports GET and POST requests.",
        headers={"Allow": "GET, POST"},
    )
Example #2
0
def test_handles_unsupported_http_methods():
    with raises(HttpQueryError) as exc_info:
        run_http_query(schema, 'put', None)

    assert exc_info.value == HttpQueryError(
        405,
        'GraphQL only supports GET and POST requests.',
        headers={
            'Allow': 'GET, POST'
        }
    )
Example #3
0
def test_hash_http_query_errors():
    errors = {
        HttpQueryError(400, "Bad error 1"),
        HttpQueryError(400, "Bad error 2"),
        HttpQueryError(403, "Bad error 1"),
    }
    assert HttpQueryError(400, "Bad error 1") in errors
    assert HttpQueryError(400, "Bad error 2") in errors
    assert HttpQueryError(403, "Bad error 1") in errors
    assert HttpQueryError(403, "Bad error 2") not in errors
def test_handles_poorly_formed_variables():
    with raises(HttpQueryError) as exc_info:
        run_http_query(
            schema,
            "get",
            {},
            dict(
                query="query helloWho($who: String){ test(who: $who) }",
                variables="who:You",
            ),
        )

    assert exc_info.value == HttpQueryError(400, "Variables are invalid JSON.")
def test_errors_when_sending_a_mutation_via_get():
    with raises(HttpQueryError) as exc_info:
        run_http_query(
            schema,
            "get",
            {},
            query_data=dict(query="""
                mutation TestMutation { writeTest { test } }
                """),
        )

    assert exc_info.value == HttpQueryError(
        405,
        "Can only perform a mutation operation from a POST request.",
        headers={"Allow": "POST"},
    )
def test_handles_plain_post_text():
    with raises(HttpQueryError) as exc_info:
        run_http_query(schema, "post", {})

    assert exc_info.value == HttpQueryError(400, "Must provide query string.")
def test_handles_incomplete_json_bodies():
    with raises(HttpQueryError) as exc_info:
        run_http_query(schema, "post", load_json_body('{"query":'))

    assert exc_info.value == HttpQueryError(400,
                                            "POST body sent invalid JSON.")
def test_handles_batch_correctly_if_is_disabled():
    with raises(HttpQueryError) as exc_info:
        run_http_query(schema, "post", [])

    assert exc_info.value == HttpQueryError(
        400, "Batch GraphQL requests are not enabled.")
def test_handles_errors_caused_by_a_lack_of_query():
    with raises(HttpQueryError) as exc_info:
        run_http_query(schema, "get", {})

    assert exc_info.value == HttpQueryError(400, "Must provide query string.")
def test_empty_batch_in_batch_query():
    with raises(HttpQueryError) as exc_info:
        run_http_query(schema, "get", [], batch_enabled=True)

    assert exc_info.value == HttpQueryError(
        400, "Received an empty list in the batch request.")
Example #11
0
def test_load_invalid_json_body():
    with raises(HttpQueryError) as exc_info:
        load_json_body('{"query":')
    assert exc_info.value == HttpQueryError(400,
                                            "POST body sent invalid JSON.")
def test_handles_errors_caused_by_invalid_query_type():
    with raises(HttpQueryError) as exc_info:
        results, params = run_http_query(schema, "get", dict(query=42))

    assert exc_info.value == HttpQueryError(400, "Unexpected query type.")
Example #13
0
def test_can_create_http_query_error():
    error = HttpQueryError(400, "Bad error")
    assert error.status_code == 400
    assert error.message == "Bad error"
    assert not error.is_graphql_error
    assert error.headers is None