tests/main.py

106 lines
2.7 KiB
Python
Raw Normal View History

2024-05-31 14:58:46 +03:00
import socket
2024-06-03 08:00:28 +03:00
import urllib3
2024-05-31 14:58:46 +03:00
2024-06-03 08:00:28 +03:00
import logging
2024-05-31 18:49:36 +03:00
import unittest
2024-05-31 14:58:46 +03:00
from unittest import TestCase
2024-05-31 16:49:44 +03:00
from fixtures import device
2024-05-31 14:58:46 +03:00
TCP_ADDR = ('127.0.0.1', 8283)
UDP_ADDR = ('127.0.0.1', 8283)
HTTP_URL = 'http://127.0.0.1:8080'
class TestTCP(TestCase):
def setUp(self) -> None:
2024-08-23 19:50:37 +03:00
self.skipTest('Not implemented yet')
2024-05-31 14:58:46 +03:00
self.sock = socket.socket()
self.sock.connect(TCP_ADDR)
def test_nm(self) -> None:
2024-05-31 16:49:44 +03:00
self.sock.sendall(device.nm_req_data().encode())
2024-05-31 14:58:46 +03:00
self._wait_for_ok()
def test_json(self) -> None:
2024-05-31 17:45:44 +03:00
self.sock.sendall(device.json_req_data().encode())
self._wait_for_ok()
2024-05-31 14:58:46 +03:00
def _wait_for_ok(self) -> None:
data = self.sock.recv(1024)
self.assertEqual(data, b'OK')
2024-05-31 15:22:25 +03:00
2024-05-31 17:58:12 +03:00
def tearDown(self) -> None:
self.sock.close()
2024-05-31 15:22:25 +03:00
2024-05-31 18:27:07 +03:00
class TestUDP(TestCase):
def setUp(self) -> None:
2024-08-23 19:50:37 +03:00
self.skipTest('Useless until no client API')
2024-05-31 18:27:07 +03:00
self.sock = socket.socket(type=socket.SOCK_DGRAM)
def test_nm(self) -> None:
self.sock.sendto(device.nm_req_data().encode(), UDP_ADDR)
# aaand we can not check if server accepted the data
# so this TestCase is absolutely useless
# until client API is implemented
def test_json(self) -> None:
self.sock.sendto(device.json_req_data().encode(), UDP_ADDR)
2024-05-31 18:50:06 +03:00
def tearDown(self) -> None:
self.sock.close()
2024-05-31 18:27:07 +03:00
2024-05-31 15:22:25 +03:00
class TestHTTP(TestCase):
2024-06-03 08:00:28 +03:00
def setUp(self) -> None:
self.http = urllib3.PoolManager()
2024-05-31 16:49:44 +03:00
def test_get(self) -> None:
2024-06-03 08:00:28 +03:00
self._check_status(self.http.request(
'GET',
2024-05-31 16:49:44 +03:00
HTTP_URL + '/get?' + device.http_req_data(),
))
2024-05-31 15:22:25 +03:00
2024-05-31 16:50:13 +03:00
def test_post(self) -> None:
2024-06-03 08:00:28 +03:00
self._check_status(self.http.request(
'POST',
2024-05-31 16:50:13 +03:00
HTTP_URL + '/post',
2024-06-03 08:00:28 +03:00
body=device.http_req_data(),
2024-08-23 19:50:37 +03:00
headers={
'Content-Type': 'application/x-www-form-urlencoded',
},
2024-05-31 16:50:13 +03:00
))
2024-05-31 15:22:25 +03:00
def test_json(self) -> None:
2024-08-23 19:50:37 +03:00
self.skipTest('Not implemented yet')
2024-06-03 08:00:28 +03:00
self._check_status(self.http.request(
'POST',
2024-05-31 17:45:44 +03:00
HTTP_URL + '/json',
2024-06-03 08:00:28 +03:00
body=device.json_req_data(),
2024-08-23 19:50:37 +03:00
headers={
'Content-Type': 'application/json',
},
2024-05-31 17:45:44 +03:00
))
2024-05-31 15:22:25 +03:00
2024-06-03 08:00:28 +03:00
def _check_status(self, resp: urllib3.HTTPResponse) -> None:
logging.debug(resp.headers.items())
logging.debug(resp.data)
2024-06-03 08:10:34 +03:00
if resp.status == 400:
logging.warning(' x-full-error = %s', resp.getheader('x-full-error'))
2024-06-03 08:00:28 +03:00
self.assertEqual(resp.status, 200)
def tearDown(self) -> None:
self.http.clear()
2024-05-31 18:49:36 +03:00
if __name__ == '__main__':
2024-06-03 08:04:06 +03:00
import os
if os.getenv('DEBUG', '0') not in {'0', 'false'}:
logging.basicConfig(level=logging.DEBUG)
2024-05-31 18:49:36 +03:00
unittest.main()