outgoing_http: Provide a convenient way to set default headers.

This commit is contained in:
Alex Vandiver
2021-05-06 16:57:46 -07:00
committed by Tim Abbott
parent 6339e7fd47
commit 8df82f50e4
3 changed files with 25 additions and 7 deletions

View File

@@ -5,11 +5,13 @@ from urllib3 import HTTPResponse
class OutgoingSession(requests.Session): class OutgoingSession(requests.Session):
def __init__(self, role: str, timeout: int) -> None: def __init__(self, role: str, timeout: int, headers: Optional[Dict[str, str]] = None) -> None:
super().__init__() super().__init__()
outgoing_adapter = OutgoingHTTPAdapter(role=role, timeout=timeout) outgoing_adapter = OutgoingHTTPAdapter(role=role, timeout=timeout)
self.mount("http://", outgoing_adapter) self.mount("http://", outgoing_adapter)
self.mount("https://", outgoing_adapter) self.mount("https://", outgoing_adapter)
if headers:
self.headers.update(headers)
class OutgoingHTTPAdapter(requests.adapters.HTTPAdapter): class OutgoingHTTPAdapter(requests.adapters.HTTPAdapter):

View File

@@ -36,11 +36,7 @@ class OutgoingWebhookServiceInterface(metaclass=abc.ABCMeta):
self.session: requests.Session = OutgoingSession( self.session: requests.Session = OutgoingSession(
role="webhook", role="webhook",
timeout=10, timeout=10,
) headers={"User-Agent": "ZulipOutgoingWebhook/" + ZULIP_VERSION},
self.session.headers.update(
{
"User-Agent": "ZulipOutgoingWebhook/" + ZULIP_VERSION,
}
) )
@abc.abstractmethod @abc.abstractmethod

View File

@@ -48,15 +48,35 @@ class RequestMockWithTimeoutAsHeader(responses.RequestsMock):
class TestOutgoingHttp(ZulipTestCase): class TestOutgoingHttp(ZulipTestCase):
def test_headers(self) -> None:
with RequestMockWithProxySupport() as mock_requests:
mock_requests.add(responses.GET, "http://example.com/")
OutgoingSession(role="testing", timeout=1, headers={"X-Foo": "bar"}).get(
"http://example.com/"
)
self.assertEqual(len(mock_requests.calls), 1)
headers = mock_requests.calls[0].request.headers
# We don't see a proxy header with no proxy set
self.assertFalse("X-Smokescreen-Role" in headers)
self.assertEqual(headers["X-Foo"], "bar")
@mock.patch.dict(os.environ, {"http_proxy": "http://localhost:4242"}) @mock.patch.dict(os.environ, {"http_proxy": "http://localhost:4242"})
def test_proxy_headers(self) -> None: def test_proxy_headers(self) -> None:
with RequestMockWithProxySupport() as mock_requests: with RequestMockWithProxySupport() as mock_requests:
mock_requests.add(responses.GET, "http://localhost:4242/") mock_requests.add(responses.GET, "http://localhost:4242/")
OutgoingSession(role="testing", timeout=1).get("http://example.com/") OutgoingSession(role="testing", timeout=1, headers={"X-Foo": "bar"}).get(
"http://example.com/"
)
self.assertEqual(len(mock_requests.calls), 1) self.assertEqual(len(mock_requests.calls), 1)
headers = mock_requests.calls[0].request.headers headers = mock_requests.calls[0].request.headers
self.assertEqual(headers["X-Smokescreen-Role"], "testing") self.assertEqual(headers["X-Smokescreen-Role"], "testing")
# We don't see the request-level headers in the proxy
# request. This isn't a _true_ test because we're
# fiddling the headers above, instead of urllib3 actually
# setting them.
self.assertFalse("X-Foo" in headers)
def test_timeouts(self) -> None: def test_timeouts(self) -> None:
with RequestMockWithTimeoutAsHeader() as mock_requests: with RequestMockWithTimeoutAsHeader() as mock_requests:
mock_requests.add(responses.GET, "http://example.com/") mock_requests.add(responses.GET, "http://example.com/")