-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
""" | ||
Server class test suite | ||
""" | ||
import logging | ||
import unittest | ||
import socket | ||
|
||
from src.packets.message_response import MessageResponse | ||
from src.applications.server import Server | ||
|
||
|
||
class TestServer(unittest.TestCase): | ||
""" | ||
Test suite for Server class | ||
""" | ||
|
||
port_number = 12000 | ||
hostname = "localhost" | ||
|
||
def test_construction(self) -> None: | ||
""" | ||
Tests that a Server object can be constructed given correct arguments | ||
""" | ||
Server([str(TestServer.port_number)]) | ||
|
||
def test_construction_raise_error(self) -> None: | ||
""" | ||
Tests that a Server object cannot be constructed given an invalid arguments | ||
""" | ||
logging.disable(logging.CRITICAL) | ||
self.assertRaises(SystemExit, Server, ["invalid"]) | ||
|
||
def test_process_read_request(self) -> None: | ||
""" | ||
Tests that Server objects correctly responds to read requests | ||
""" | ||
server = Server([str(TestServer.port_number)]) | ||
receiver_name = "John" | ||
sender_name = "Alice" | ||
message = b"Hello John" | ||
server.messages[receiver_name] = [(sender_name, message)] | ||
|
||
with socket.socket() as server_welcoming_socket: | ||
server_welcoming_socket.bind((TestServer.hostname, TestServer.port_number)) | ||
server_welcoming_socket.listen(1) | ||
|
||
with socket.socket() as client_socket: | ||
client_socket.connect((TestServer.hostname, TestServer.port_number)) | ||
server_connection_socket, _ = server_welcoming_socket.accept() | ||
|
||
with server_connection_socket: | ||
server.process_read_request(server_connection_socket, receiver_name) | ||
|
||
# Receive message from server | ||
packet = client_socket.recv(1024) | ||
response = MessageResponse.decode_packet(packet) | ||
|
||
# Check that the message is correct | ||
self.assertEqual(([(sender_name, message.decode())], False), response) |