Skip to content

Commit

Permalink
fix native response handler
Browse files Browse the repository at this point in the history
  • Loading branch information
voidZXL committed Jul 24, 2024
1 parent 1dddb95 commit 7294e8e
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 7 deletions.
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ def __init__(
connections_override=None,
single_thread: bool = False,
):
service._application = None
self.service = service
self.is_ready = threading.Event()
self.error = None
Expand Down
5 changes: 3 additions & 2 deletions tests/test_api/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ def get_requests(backend: str = None, asynchronous: bool = False):
]

return [
('get', 'backend', {}, None, {}, backend, 200),
# (method, path, query, data, headers, result, status)
("get", "@special", {}, None, {}, "@special", 200),
('get', 'backend', {}, None, {}, backend, 200),
(
"post",
"response",
Expand Down Expand Up @@ -292,7 +292,8 @@ def do_live_api_tests(service):
)
assert isinstance(resp, Response), f'invalid response: {resp}'
content = resp.data
assert resp.status == status, f"{method} {path} failed with {content}, {status} expected, got {resp.status}"
assert resp.status == status, \
f"{method} {path} failed with {content}, {status} expected, got {resp.status}, {resp.headers}"
if result is not ...:
if callable(result):
result(content)
Expand Down
3 changes: 2 additions & 1 deletion tests/test_api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ def test_api_features(self):
)()
assert isinstance(resp, response.Response), f'invalid response: {resp}'
content = resp.data
assert resp.status == status, f"{method} {path} failed with {content}, {status} expected, got {resp.status}"
assert resp.status == status, \
f"{method} {path} failed with {content}, {status} expected, got {resp.status}"
if result is not ...:
if callable(result):
result(content)
Expand Down
6 changes: 5 additions & 1 deletion tests/test_api/test_api_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@
from .params import do_live_api_tests
from utilmeta import UtilMeta

setup_service(__name__, backend='flask')
setup_service(__name__, backend='flask', async_param=[False, True])
flask_server_process = make_live_process(backend='flask', port=8003, cmdline=True)
flask_server_thread = make_server_thread(backend='flask', port=8083)


def test_flask_api(service, flask_server_process):
service._application = None
service.adaptor.app = None
do_live_api_tests(service)
service._application = None
service.adaptor.app = None


def test_flask_api_internal(service: UtilMeta, flask_server_thread):
service._application = None
service.adaptor.app = None
do_live_api_tests(service)
service._application = None
service.adaptor.app = None
Expand Down
11 changes: 9 additions & 2 deletions utilmeta/core/response/backends/django.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ def qualify(cls, obj):

@classmethod
def reconstruct(cls, resp: Union['ResponseAdaptor', 'Response']):
if isinstance(resp, (HttpResponse, StreamingHttpResponse)):
return resp

from utilmeta.core.response import Response
if isinstance(resp, ResponseAdaptor):
resp = Response(response=resp)
Expand Down Expand Up @@ -48,9 +51,13 @@ def headers(self):

@property
def body(self) -> bytes:
if self._body is not None:
return self._body
if isinstance(self.response, StreamingHttpResponse):
return self.response.getvalue()
return self.response.content
self._body = self.response.getvalue()
else:
self._body = self.response.content
return self._body

@property
def cookies(self):
Expand Down
3 changes: 3 additions & 0 deletions utilmeta/core/response/backends/sanic.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ def qualify(cls, obj):

@classmethod
def reconstruct(cls, resp: Union['ResponseAdaptor', 'Response']):
if isinstance(resp, HTTPResponse):
return resp

from utilmeta.core.response import Response

if isinstance(resp, ResponseAdaptor):
Expand Down
3 changes: 3 additions & 0 deletions utilmeta/core/response/backends/starlette.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ def qualify(cls, obj):

@classmethod
def reconstruct(cls, resp: Union['ResponseAdaptor', 'Response']):
if isinstance(resp, HttpResponse):
return resp

from utilmeta.core.response import Response

if isinstance(resp, ResponseAdaptor):
Expand Down
3 changes: 3 additions & 0 deletions utilmeta/core/response/backends/werkzeug.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ class WerkzeugResponseAdaptor(ResponseAdaptor):

@classmethod
def reconstruct(cls, resp: Union['ResponseAdaptor', 'WerkzeugResponse']):
if isinstance(resp, WerkzeugResponse):
return resp

from utilmeta.core.response import Response

if isinstance(resp, ResponseAdaptor):
Expand Down
5 changes: 4 additions & 1 deletion utilmeta/core/response/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,10 @@ def prepare_body(self):
@property
def body(self) -> bytes:
if self.adaptor:
return self.adaptor.body
body = self.adaptor.body
# sometime adaptor.body maybe only can read once
if body:
return body
body = self.prepare_body()
if hasattr(body, 'read'):
return body.read() # noqa
Expand Down

0 comments on commit 7294e8e

Please sign in to comment.