From 8b787482ff73bceddd5f8edbe2c9c06954792dcc Mon Sep 17 00:00:00 2001 From: Laurent Monin Date: Thu, 6 Jan 2022 14:41:09 +0100 Subject: [PATCH] _query_data(): Use urllib.parse.urlencode() instead of QUrl/QUrlQuery Note: previously spaces weren't encoded, with this change they are (as plus) which is more conform to expected format for application/x-www-form-urlencoded Actually this was a bug in previous version, as addQueryItem() didn't encode spaces as they should See https://doc.qt.io/qt-5/qurlquery.html#addQueryItem "This method does not treat spaces (ASCII 0x20) and plus ("+") signs as the same, like HTML forms do. If you need spaces to be represented as plus signs, use actual plus signs." It wasn't an issue because none of passed data should contain spaces in our current code --- picard/oauth.py | 14 ++------------ test/test_oauth.py | 2 +- 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/picard/oauth.py b/picard/oauth.py index d6bba3477..bb56e5637 100644 --- a/picard/oauth.py +++ b/picard/oauth.py @@ -28,11 +28,7 @@ from functools import partial from json.decoder import JSONDecodeError import time - -from PyQt5.QtCore import ( - QUrl, - QUrlQuery, -) +import urllib.parse from picard import log from picard.config import get_config @@ -179,13 +175,7 @@ class OAuthManager(object): @staticmethod def _query_data(params): - url = QUrl() - url_query = QUrlQuery() - for key, value in params.items(): - if key: - url_query.addQueryItem(key, value) - url.setQuery(url_query.query(QUrl.FullyEncoded)) - return url.query() + return urllib.parse.urlencode({key: value for key, value in params.items() if key}) def refresh_access_token(self, callback): log.debug("OAuth: refreshing access_token with a refresh_token %s", self.refresh_token) diff --git a/test/test_oauth.py b/test/test_oauth.py index c17ed198d..b93d875e3 100644 --- a/test/test_oauth.py +++ b/test/test_oauth.py @@ -33,4 +33,4 @@ class OAuthManagerTest(PicardTestCase): '': '', } data = OAuthManager._query_data(params) - self.assertEqual(data, "a%26b=a b&c d=c%26d") + self.assertEqual(data, "a%26b=a+b&c+d=c%26d")