1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
import json
from typing import Literal
import pytest
from strawberry.http.base import BaseView
from .clients.base import HttpClient
@pytest.mark.parametrize("method", ["delete", "head", "put", "patch"])
async def test_does_only_allow_get_and_post(
method: Literal["delete", "head", "put", "patch"],
http_client: HttpClient,
):
response = await http_client.request(url="/graphql", method=method)
assert response.status_code == 405
async def test_the_http_handler_uses_the_views_decode_json_method(
http_client: HttpClient, mocker
):
spy = mocker.spy(BaseView, "decode_json")
response = await http_client.query(query="{ hello }")
assert response.status_code == 200
assert response.headers["content-type"].split(";")[0] == "application/json"
data = response.json["data"]
assert isinstance(data, dict)
assert data["hello"] == "Hello world"
assert spy.call_count == 1
async def test_the_http_handler_supports_bytes_encoded_json(
http_client: HttpClient, mocker
):
"""Check that http handlers correctly deal with byte return type from `encode_json`"""
def patched_encode_json(self, data: object) -> bytes:
return json.dumps(data).encode()
mocker.patch("strawberry.http.base.BaseView.encode_json", patched_encode_json)
response = await http_client.query(query="{ hello }")
assert response.status_code == 200
assert response.headers["content-type"].split(";")[0] == "application/json"
data = response.json["data"]
assert isinstance(data, dict)
assert data["hello"] == "Hello world"
async def test_exception(http_client: HttpClient, mocker):
response = await http_client.query(query="{ hello }", operation_name="wrong")
assert response.status_code == 400
assert response.headers["content-type"].split(";")[0] == "text/plain"
assert response.data == b'Unknown operation named "wrong".'
|