From 122e0a73a979f379e6bc7a3f795847be8dc6db0b Mon Sep 17 00:00:00 2001 From: meejah Date: Thu, 22 Jun 2023 01:29:55 -0600 Subject: [PATCH] more-generic testing hook --- src/allmydata/storage/http_client.py | 9 ++------- src/allmydata/test/test_storage_http.py | 17 ++++++++++++++--- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/allmydata/storage/http_client.py b/src/allmydata/storage/http_client.py index e7df3709d..8c0100656 100644 --- a/src/allmydata/storage/http_client.py +++ b/src/allmydata/storage/http_client.py @@ -427,7 +427,7 @@ class StorageClient(object): _pool: HTTPConnectionPool _clock: IReactorTime # Are we running unit tests? - _test_mode: bool + _analyze_response: Callable[[IResponse], None] = lambda _: None def relative_url(self, path: str) -> DecodedURL: """Get a URL relative to the base URL.""" @@ -534,12 +534,7 @@ class StorageClient(object): response = await self._treq.request( method, url, headers=headers, timeout=timeout, **kwargs ) - - if self._test_mode and response.code != 404: - # We're doing API queries, HTML is never correct except in 404, but - # it's the default for Twisted's web server so make sure nothing - # unexpected happened. - assert get_content_type(response.headers) != "text/html" + self._analyze_response(response) return response diff --git a/src/allmydata/test/test_storage_http.py b/src/allmydata/test/test_storage_http.py index 233d82989..aaa858db4 100644 --- a/src/allmydata/test/test_storage_http.py +++ b/src/allmydata/test/test_storage_http.py @@ -316,6 +316,17 @@ def result_of(d): + "This is probably a test design issue." ) +def response_is_not_html(response): + """ + During tests, this is registered so we can ensure the web server + doesn't give us text/html. + + HTML is never correct except in 404, but it's the default for + Twisted's web server so we assert nothing unexpected happened. + """ + if response.code != 404: + assert get_content_type(response.headers) != "text/html" + class CustomHTTPServerTests(SyncTestCase): """ @@ -342,7 +353,7 @@ class CustomHTTPServerTests(SyncTestCase): # fixed if https://github.com/twisted/treq/issues/226 were ever # fixed. clock=treq._agent._memoryReactor, - test_mode=True, + analyze_response=response_is_not_html, ) self._http_server.clock = self.client._clock @@ -560,7 +571,7 @@ class HttpTestFixture(Fixture): treq=self.treq, pool=None, clock=self.clock, - test_mode=True, + analyze_response=response_is_not_html, ) def result_of_with_flush(self, d): @@ -674,7 +685,7 @@ class GenericHTTPAPITests(SyncTestCase): treq=StubTreq(self.http.http_server.get_resource()), pool=None, clock=self.http.clock, - test_mode=True, + analyze_response=response_is_not_html, ) ) with assert_fails_with_http_code(self, http.UNAUTHORIZED):