예제 #1
0
    def test_timeouts(self) -> None:
        with RequestMockWithTimeoutAsHeader() as mock_requests:
            mock_requests.add(responses.GET, "http://example.com/")
            OutgoingSession(role="testing",
                            timeout=17).get("http://example.com/")
            self.assert_length(mock_requests.calls, 1)
            self.assertEqual(
                mock_requests.calls[0].request.headers["X-Timeout"], 17)

        with RequestMockWithTimeoutAsHeader() as mock_requests:
            mock_requests.add(responses.GET, "http://example.com/")
            OutgoingSession(role="testing",
                            timeout=17).get("http://example.com/", timeout=42)
            self.assert_length(mock_requests.calls, 1)
            self.assertEqual(
                mock_requests.calls[0].request.headers["X-Timeout"], 42)
예제 #2
0
def handle_reupload_emojis_event(realm: Realm, logger: logging.Logger) -> None:  # nocoverage
    from zerver.lib.emoji import get_emoji_url

    session = OutgoingSession(role="reupload_emoji", timeout=3, max_retries=3)

    query = RealmEmoji.objects.filter(realm=realm).order_by("id")

    for realm_emoji in query:
        logger.info("Processing emoji %s", realm_emoji.id)
        emoji_filename = realm_emoji.file_name
        emoji_url = get_emoji_url(emoji_filename, realm_emoji.realm_id)
        if emoji_url.startswith("/"):
            emoji_url = urljoin(realm_emoji.realm.uri, emoji_url)

        emoji_file_content = get_emoji_file_content(session, emoji_url, realm_emoji.id, logger)

        emoji_bytes_io = io.BytesIO(emoji_file_content)

        user_profile = realm_emoji.author
        # When this runs, emojis have already been migrated to always have .author set.
        assert user_profile is not None

        logger.info("Reuploading emoji %s", realm_emoji.id)
        realm_emoji.is_animated = upload_emoji_image(emoji_bytes_io, emoji_filename, user_profile)
        realm_emoji.save(update_fields=["is_animated"])
예제 #3
0
 def __init__(self, token: str, user_profile: UserProfile,
              service_name: str) -> None:
     self.token: str = token
     self.user_profile: UserProfile = user_profile
     self.service_name: str = service_name
     self.session: requests.Session = OutgoingSession(
         role="webhook",
         timeout=10,
         headers={"User-Agent": "ZulipOutgoingWebhook/" + ZULIP_VERSION},
     )
예제 #4
0
def get_emoji_file_content(
    session: OutgoingSession, emoji_url: str, emoji_id: int, logger: logging.Logger
) -> bytes:  # nocoverage
    original_emoji_url = emoji_url + ".original"

    logger.info("Downloading %s", original_emoji_url)
    response = session.get(original_emoji_url)
    if response.status_code == 200:
        assert type(response.content) == bytes
        return response.content

    logger.info("Error fetching emoji from URL %s", original_emoji_url)
    logger.info("Trying %s instead", emoji_url)
    response = session.get(emoji_url)
    if response.status_code == 200:
        assert type(response.content) == bytes
        return response.content
    logger.info("Error fetching emoji from URL %s", emoji_url)
    logger.error("Could not fetch emoji %s", emoji_id)
    raise AssertionError(f"Could not fetch emoji {emoji_id}")
예제 #5
0
 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.assert_length(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")
예제 #6
0
    def test_retries(self) -> None:
        # Responses doesn't support testing the low-level retry
        # functionality, so we can't test the retry itself easily. :(
        # https://github.com/getsentry/responses/issues/135

        # Defaults to no retries
        session = requests.Session()
        self.assertEqual(session.adapters["http://"].max_retries.total, 0)
        self.assertEqual(session.adapters["https://"].max_retries.total, 0)

        session = OutgoingSession(role="testing", timeout=1)
        self.assertEqual(session.adapters["http://"].max_retries.total, 0)
        self.assertEqual(session.adapters["https://"].max_retries.total, 0)

        session = OutgoingSession(role="testing", timeout=1, max_retries=2)
        self.assertEqual(session.adapters["http://"].max_retries.total, 2)
        self.assertEqual(session.adapters["https://"].max_retries.total, 2)

        session = OutgoingSession(role="testing",
                                  timeout=1,
                                  max_retries=Retry(total=5))
        self.assertEqual(session.adapters["http://"].max_retries.total, 5)
        self.assertEqual(session.adapters["https://"].max_retries.total, 5)
예제 #7
0
    def test_proxy_headers(self) -> None:
        with RequestMockWithProxySupport() as mock_requests:
            mock_requests.add(responses.GET, "http://localhost:4242/")
            OutgoingSession(role="testing",
                            timeout=1,
                            headers={
                                "X-Foo": "bar"
                            }).get("http://example.com/")
            self.assert_length(mock_requests.calls, 1)
            headers = mock_requests.calls[0].request.headers
            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)