import socket from urllib.request import urlopen, Request from unittest import TestCase from fixtures import device 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: self.sock = socket.socket() self.sock.connect(TCP_ADDR) def test_nm(self) -> None: self.sock.sendall(device.nm_req_data().encode()) self._wait_for_ok() def test_json(self) -> None: raise NotImplemented def _wait_for_ok(self) -> None: data = self.sock.recv(1024) self.assertEqual(data, b'OK') class TestHTTP(TestCase): def test_get(self) -> None: self._send_and_check(Request( HTTP_URL + '/get?' + device.http_req_data(), )) def test_post(self) -> None: self._send_and_check(Request( HTTP_URL + '/post', data=device.http_req_data().encode(), )) def test_json(self) -> None: raise NotImplemented def _send_and_check(self, req: Request) -> None: with urlopen(req) as resp: self.assertEqual(resp.status, 200)