Skip to content

Commit

Permalink
improve coverage tests on the request client
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabien Coelho committed Mar 13, 2024
1 parent bedfa5a commit d4a822e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
4 changes: 2 additions & 2 deletions FlaskTester.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def check(self, method: str, path: str, status: int, content: str|None = None, *
return res


class RequestClient(Client): # pragma: no cover
class RequestClient(Client):
"""Request-based test provider."""

def __init__(self, auth: Authenticator, base_url: str, default_login=None):
Expand All @@ -316,7 +316,7 @@ def _request(self, method: str, path: str, **kwargs):
files: dict[str, Any] = {}
for name, whatever in data.items():
# FIXME what types should be accepted?
if isinstance(whatever, io.BufferedReader):
if isinstance(whatever, io.IOBase):
files[name] = whatever
elif isinstance(whatever, tuple):
# reorder tuple to match requests expectations:
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Only one set of tests is needed, switching from internal to external is
achieved through environment variables.

![Status](https://github.com/zx80/flask-tester/actions/workflows/package.yml/badge.svg?branch=main&style=flat)
![Tests](https://img.shields.io/badge/tests-7%20✓-success)
![Tests](https://img.shields.io/badge/tests-8%20✓-success)
![Coverage](https://img.shields.io/badge/coverage-100%25-success)
![Issues](https://img.shields.io/github/issues/zx80/flask-tester?style=flat)
![Python](https://img.shields.io/badge/python-3-informational)
Expand Down Expand Up @@ -131,7 +131,6 @@ please report any [issues](https://github.com/zx80/flask-tester/issues).

- API documentation generation
- control logging level
- improve actual coverage

## Versions

Expand Down
18 changes: 18 additions & 0 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import FlaskTester as ft
from FlaskTester import ft_client, ft_authenticator
import app
import http.server as htsv
import threading
import io
import logging

logging.basicConfig(level=logging.INFO)
Expand Down Expand Up @@ -180,3 +183,18 @@ def test_client():
assert False, "must raise an error" # pragma: no cover
except NotImplementedError:
assert True, "error raised"

def test_request_client():
httpd = htsv.HTTPServer(("", 8888), htsv.SimpleHTTPRequestHandler)
thread = threading.Thread(target = lambda: httpd.serve_forever())
thread.start()
try:
client = ft.RequestClient(ft.Authenticator(), "http://localhost:8888")
client.get("/", status=200)
hello = io.BytesIO(b"hello world")
client.post("/", status=501, data={"hello": hello})
hello = io.BytesIO(b"hello world")
client.post("/", status=501, data={"hello": (hello, "hello.txt", "text/plain")})
client.post("/", status=501, data={"hello": "world!"})
finally:
httpd.shutdown()

0 comments on commit d4a822e

Please sign in to comment.