Xqt has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1360546?usp=email )

Change subject: tests: Use an allowed destination for HTTPBin redirects
......................................................................

tests: Use an allowed destination for HTTPBin redirects

HTTPBin now restricts redirect destinations and rejects Wikidata with
HTTP 403, leaving the response history empty.

Redirect through HTTPBin to example.com, explicitly requesting the
second redirect as HTTP 301. Preserve the HTTP 302 -> 301 chain and
cross-domain coverage. Check the final status before redirect history
so HTTP errors produce a clearer assertion failure.

Bug: T442820
Change-Id: Iae9ceb13f2c8149649b47c0db47463157ac23d96
---
M tests/http_tests.py
1 file changed, 11 insertions(+), 6 deletions(-)

Approvals:
  Xqt: Verified; Looks good to me, approved




diff --git a/tests/http_tests.py b/tests/http_tests.py
index 3a9d926..081107c 100755
--- a/tests/http_tests.py
+++ b/tests/http_tests.py
@@ -15,6 +15,7 @@
 from io import BytesIO
 from platform import python_implementation
 from unittest.mock import Mock, patch
+from urllib.parse import urlencode

 import requests

@@ -165,7 +166,7 @@
             http.fetch('invalid://url')

     def test_follow_redirects(self) -> None:
-        """Test follow 301 redirects correctly."""
+        """Test following 301 and 302 redirects correctly."""
         # The following will redirect from ' ' -> '_', and maybe to https://
         r = http.fetch('http://en.wikipedia.org/wiki/Main%20Page')
         self.assertEqual(r.status_code, HTTPStatus.OK.value)
@@ -174,15 +175,19 @@
                          HTTPStatus.MOVED_PERMANENTLY.value)
         self.assertIn('//en.wikipedia.org/wiki/Main_Page', r.url)

-        r = self.fetch(self.httpbin
-                       + '/redirect-to?url=https://www.wikidata.org')
+        # Use an allowed destination and explicitly request a 302 -> 301 chain.
+        redirect_url = self.httpbin + '/redirect-to'
+        target = redirect_url + '?' + urlencode({
+            'url': 'https://example.com/',
+            'status_code': HTTPStatus.MOVED_PERMANENTLY.value,
+        })
+        r = self.fetch(redirect_url, params={'url': target})
+        self.assertEqual(r.status_code, HTTPStatus.OK.value)
         self.assertIsNotEmpty(r.history)
         self.assertEqual(r.history[0].status_code, HTTPStatus.FOUND.value)
         self.assertEqual(r.history[-1].status_code,
                          HTTPStatus.MOVED_PERMANENTLY.value)
-        self.assertEqual(r.status_code, HTTPStatus.OK.value)
-        self.assertEqual(r.url,
-                         'https://www.wikidata.org/wiki/Wikidata:Main_Page')
+        self.assertEqual(r.url, 'https://example.com/')


 class UserAgentTestCase(DeprecationTestCase):

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1360546?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.wikimedia.org/r/settings?usp=email

Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Iae9ceb13f2c8149649b47c0db47463157ac23d96
Gerrit-Change-Number: 1360546
Gerrit-PatchSet: 1
Gerrit-Owner: Mahveotm <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to