Replace urllib with urllib3

This commit is contained in:
DarkCat09 2024-06-03 09:00:28 +04:00
parent 1d75e10c63
commit b4ab6a47d9
Signed by: DarkCat09
GPG key ID: 0A26CD5B3345D6E3
2 changed files with 21 additions and 9 deletions

29
main.py
View file

@ -1,6 +1,7 @@
import socket
from urllib.request import urlopen, Request
import urllib3
import logging
import unittest
from unittest import TestCase
@ -54,26 +55,36 @@ class TestUDP(TestCase):
class TestHTTP(TestCase):
def setUp(self) -> None:
self.http = urllib3.PoolManager()
def test_get(self) -> None:
self._send_and_check(Request(
self._check_status(self.http.request(
'GET',
HTTP_URL + '/get?' + device.http_req_data(),
))
def test_post(self) -> None:
self._send_and_check(Request(
self._check_status(self.http.request(
'POST',
HTTP_URL + '/post',
data=device.http_req_data().encode(),
body=device.http_req_data(),
))
def test_json(self) -> None:
self._send_and_check(Request(
self._check_status(self.http.request(
'POST',
HTTP_URL + '/json',
data=device.json_req_data().encode(),
body=device.json_req_data(),
))
def _send_and_check(self, req: Request) -> None:
with urlopen(req) as resp:
self.assertEqual(resp.status, 200)
def _check_status(self, resp: urllib3.HTTPResponse) -> None:
logging.debug(resp.headers.items())
logging.debug(resp.data)
self.assertEqual(resp.status, 200)
def tearDown(self) -> None:
self.http.clear()
if __name__ == '__main__':

1
requirements.txt Normal file
View file

@ -0,0 +1 @@
urllib3