Files
pyscript/core/tests/python/tests/test_fetch.py
Nicholas Tollervey 9dad29ec17 Refactor repository. Fixes #2161 (#2192)
* Remove duplicate LICENSE.
* Remove un-userd pyscript.sw directory and its content.
* Remove ReadTheDocs settings (unused).
* Remove un-used pyproject.toml
* Remove now unused CHANGELOG. Changes now tracked via release notes on GitHub.
* Updated / cleaned release page template and associated GH actions.
* Update prettierignore to remove un-needed refs.
* Move troubleshooting into correct README.
* Add reason for the index.html
* Rename the "pyscript.core" directory to "core".
* Update PR template because CHANGELOG is no longer used.
* Codespell configuration in pyproject.toml.
* Update pyscript.core -> core in .githubignore
* Remove test-results/.last-run.json. This should be ignored by git.
* Pin nodejs version.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-09-30 10:29:26 +01:00

84 lines
2.3 KiB
Python

"""
Ensure the pyscript.test function behaves as expected.
"""
from pyscript import fetch
async def test_fetch_json():
"""
The fetch function should return the expected JSON response.
"""
response = await fetch("https://jsonplaceholder.typicode.com/todos/1")
assert response.ok
data = await response.json()
assert data["userId"] == 1
assert data["id"] == 1
assert data["title"] == "delectus aut autem"
assert data["completed"] is False
async def test_fetch_text():
"""
The fetch function should return the expected text response.
"""
response = await fetch("https://jsonplaceholder.typicode.com/todos/1")
assert response.ok
text = await response.text()
assert "delectus aut autem" in text
assert "completed" in text
assert "false" in text
assert "1" in text
async def test_fetch_bytearray():
"""
The fetch function should return the expected bytearray response.
"""
response = await fetch("https://jsonplaceholder.typicode.com/todos/1")
assert response.ok
data = await response.bytearray()
assert b"delectus aut autem" in data
assert b"completed" in data
assert b"false" in data
assert b"1" in data
async def test_fetch_array_buffer():
"""
The fetch function should return the expected array buffer response.
"""
response = await fetch("https://jsonplaceholder.typicode.com/todos/1")
assert response.ok
data = await response.arrayBuffer()
bytes_ = bytes(data)
assert b"delectus aut autem" in bytes_
assert b"completed" in bytes_
assert b"false" in bytes_
assert b"1" in bytes_
async def test_fetch_ok():
"""
The fetch function should return a response with ok set to True for an
existing URL.
"""
response = await fetch("https://jsonplaceholder.typicode.com/todos/1")
assert response.ok
assert response.status == 200
data = await response.json()
assert data["userId"] == 1
assert data["id"] == 1
assert data["title"] == "delectus aut autem"
assert data["completed"] is False
async def test_fetch_not_ok():
"""
The fetch function should return a response with ok set to False for a
non-existent URL.
"""
response = await fetch("https://jsonplaceholder.typicode.com/todos/1000")
assert not response.ok
assert response.status == 404