From 065da8073e52763388290f28d0e313f2e861e5c4 Mon Sep 17 00:00:00 2001 From: keijack Date: Mon, 7 Nov 2022 17:57:04 +0800 Subject: [PATCH] do unquote to set basic auth headers. --- py_eureka_client/__init__.py | 2 +- py_eureka_client/http_client.py | 7 ++-- .../py_eureka_client/test_http_basic_auth.py | 41 +++++++++++++++++++ 3 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 tests/py_eureka_client/test_http_basic_auth.py diff --git a/py_eureka_client/__init__.py b/py_eureka_client/__init__.py index 1147f73..eb705fc 100644 --- a/py_eureka_client/__init__.py +++ b/py_eureka_client/__init__.py @@ -22,7 +22,7 @@ SOFTWARE. """ -version = "0.11.4" +version = "0.11.5" """ Status of instances diff --git a/py_eureka_client/http_client.py b/py_eureka_client/http_client.py index 37cf1f9..9efa738 100644 --- a/py_eureka_client/http_client.py +++ b/py_eureka_client/http_client.py @@ -25,11 +25,12 @@ import re import base64 import gzip -from typing import Union - import urllib.request import urllib.error +from typing import Union +from urllib.parse import unquote + class HTTPError(urllib.error.HTTPError): pass @@ -64,7 +65,7 @@ def parse_url(url): addr = url if m.group(2) is not None: addr = addr.replace(m.group(2), "") - ori_auth = f"{m.group(3)}:{m.group(4)}".encode() + ori_auth = f"{unquote(m.group(3))}:{unquote(m.group(4))}".encode() auth_str = base64.standard_b64encode(ori_auth).decode() else: auth_str = None diff --git a/tests/py_eureka_client/test_http_basic_auth.py b/tests/py_eureka_client/test_http_basic_auth.py new file mode 100644 index 0000000..11fc555 --- /dev/null +++ b/tests/py_eureka_client/test_http_basic_auth.py @@ -0,0 +1,41 @@ +# -*- coding: utf-8 -*- + +""" +Copyright (c) 2018 Keijack Wu + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +""" + +import unittest +import asyncio + +import py_eureka_client.logger as logger +from py_eureka_client.http_client import HttpClient, HttpRequest, HttpResponse + +logger.set_level("DEBUG") + + +class TestEurekaServer(unittest.TestCase): + + def test_load_page(self): + req = HttpRequest("http://keijack:qwe%40rty%21@10.0.2.16:8080/a.txt") + client = HttpClient() + res: HttpResponse = asyncio.run(client.urlopen(req)) + print(res.body_text) + assert res.body_text == 'hello!'