Replace urllib with urllib3
This commit is contained in:
parent
1d75e10c63
commit
b4ab6a47d9
2 changed files with 21 additions and 9 deletions
29
main.py
29
main.py
|
@ -1,6 +1,7 @@
|
||||||
import socket
|
import socket
|
||||||
from urllib.request import urlopen, Request
|
import urllib3
|
||||||
|
|
||||||
|
import logging
|
||||||
import unittest
|
import unittest
|
||||||
from unittest import TestCase
|
from unittest import TestCase
|
||||||
|
|
||||||
|
@ -54,26 +55,36 @@ class TestUDP(TestCase):
|
||||||
|
|
||||||
class TestHTTP(TestCase):
|
class TestHTTP(TestCase):
|
||||||
|
|
||||||
|
def setUp(self) -> None:
|
||||||
|
self.http = urllib3.PoolManager()
|
||||||
|
|
||||||
def test_get(self) -> None:
|
def test_get(self) -> None:
|
||||||
self._send_and_check(Request(
|
self._check_status(self.http.request(
|
||||||
|
'GET',
|
||||||
HTTP_URL + '/get?' + device.http_req_data(),
|
HTTP_URL + '/get?' + device.http_req_data(),
|
||||||
))
|
))
|
||||||
|
|
||||||
def test_post(self) -> None:
|
def test_post(self) -> None:
|
||||||
self._send_and_check(Request(
|
self._check_status(self.http.request(
|
||||||
|
'POST',
|
||||||
HTTP_URL + '/post',
|
HTTP_URL + '/post',
|
||||||
data=device.http_req_data().encode(),
|
body=device.http_req_data(),
|
||||||
))
|
))
|
||||||
|
|
||||||
def test_json(self) -> None:
|
def test_json(self) -> None:
|
||||||
self._send_and_check(Request(
|
self._check_status(self.http.request(
|
||||||
|
'POST',
|
||||||
HTTP_URL + '/json',
|
HTTP_URL + '/json',
|
||||||
data=device.json_req_data().encode(),
|
body=device.json_req_data(),
|
||||||
))
|
))
|
||||||
|
|
||||||
def _send_and_check(self, req: Request) -> None:
|
def _check_status(self, resp: urllib3.HTTPResponse) -> None:
|
||||||
with urlopen(req) as resp:
|
logging.debug(resp.headers.items())
|
||||||
self.assertEqual(resp.status, 200)
|
logging.debug(resp.data)
|
||||||
|
self.assertEqual(resp.status, 200)
|
||||||
|
|
||||||
|
def tearDown(self) -> None:
|
||||||
|
self.http.clear()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
1
requirements.txt
Normal file
1
requirements.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
urllib3
|
Loading…
Add table
Reference in a new issue