_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
This commit is contained in:
Laurent Monin
2022-01-06 14:41:09 +01:00
parent d1de2a7208
commit 8b787482ff
2 changed files with 3 additions and 13 deletions

View File

@@ -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)

View File

@@ -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")