Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix Response.update_from to also copy close handlers #8946

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions localstack/http/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ class Response(WerkzeugResponse):
def update_from(self, other: WerkzeugResponse):
"""
Updates this response object with the data from the given response object. It reads the status code,
the response data, and updates its own headers (overwrites existing headers, but does not remove ones not
present in the given object).
the response data, and updates its own headers (overwrites existing headers, but does not remove ones
not present in the given object). Also updates ``call_on_close`` callbacks in the same way.

:param other: the response object to read from
"""
self.status_code = other.status_code
self.response = other.response
self._on_close.extend(other._on_close)
self.headers.update(other.headers)

def set_json(self, doc: Any):
Expand Down
21 changes: 21 additions & 0 deletions tests/integration/aws/test_app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import threading

import pytest
import requests
import websocket
Expand Down Expand Up @@ -79,6 +81,25 @@ def test_404_unfortunately_detected_as_s3_request(self):
assert "<Error><Code>NoSuchBucket</Code>" in response.text


class TestWerkzeugIntegration:
def test_response_close_handlers_called_with_router(self, cleanups):
closed = threading.Event()

def _test_route(_request):
r = Response("ok", 200)
r.call_on_close(closed.set)
return r

rule = ROUTER.add("/_test/test_route", _test_route)
cleanups.append(lambda: ROUTER.remove(rule))

response = requests.get(get_edge_url() + "/_test/test_route")
assert response.status_code == 200, response.text
assert response.text == "ok"

assert closed.wait(timeout=3), "expected closed.set to be called"


class TestWebSocketIntegration:
"""
Test for the WebSocket/HandlerChain integration.
Expand Down