Merge remote-tracking branch 'origin/pr/3884'

This commit is contained in:
Florian Bruhin 2018-05-03 09:09:27 +02:00
commit 91aa9f6c0c
3 changed files with 19 additions and 4 deletions

View File

@ -45,7 +45,7 @@ class PyPIVersionClient(QObject):
arg: The error message, as string.
"""
API_URL = 'https://pypi.python.org/pypi/{}/json'
API_URL = 'https://pypi.org/pypi/{}/json'
success = pyqtSignal(str)
error = pyqtSignal(str)

View File

@ -28,6 +28,21 @@ from PyQt5.QtNetwork import (QNetworkAccessManager, QNetworkRequest,
QNetworkReply)
class HTTPRequest(QNetworkRequest):
"""A QNetworkRquest that follows (secure) redirects by default."""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
try:
self.setAttribute(QNetworkRequest.RedirectPolicyAttribute,
QNetworkRequest.NoLessSafeRedirectPolicy)
except AttributeError:
# RedirectPolicyAttribute was introduced in 5.9 to replace
# FollowRedirectsAttribute.
self.setAttribute(QNetworkRequest.FollowRedirectsAttribute,
True)
class HTTPClient(QObject):
"""An HTTP client based on QNetworkAccessManager.
@ -63,7 +78,7 @@ class HTTPClient(QObject):
if data is None:
data = {}
encoded_data = urllib.parse.urlencode(data).encode('utf-8')
request = QNetworkRequest(url)
request = HTTPRequest(url)
request.setHeader(QNetworkRequest.ContentTypeHeader,
'application/x-www-form-urlencoded;charset=utf-8')
reply = self._nam.post(request, encoded_data)
@ -77,7 +92,7 @@ class HTTPClient(QObject):
Args:
url: The URL to access, as QUrl.
"""
request = QNetworkRequest(url)
request = HTTPRequest(url)
reply = self._nam.get(request)
self._handle_reply(reply)

View File

@ -67,7 +67,7 @@ def test_get_version_success(qtbot):
with qtbot.waitSignal(client.success):
client.get_version('test')
assert http_stub.url == QUrl('https://pypi.python.org/pypi/test/json')
assert http_stub.url == QUrl(client.API_URL.format('test'))
def test_get_version_error(qtbot):