forked from pcewebpython/echo-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
46 lines (38 loc) · 1.37 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from echo_client import client
import socket
import unittest
class EchoTestCase(unittest.TestCase):
"""tests for the echo server and client"""
def send_message(self, message):
"""Attempt to send a message using the client
In case of a socket error, fail and report the problem
"""
try:
reply = client(message)
except socket.error as e:
if e.errno == 61:
msg = "Error: {0}, is the server running?"
self.fail(msg.format(e.strerror))
else:
self.fail("Unexpected Error: {0}".format(str(e)))
return reply
def test_short_message_echo(self):
"""test that a message short than 16 bytes echoes cleanly"""
expected = "short message"
actual = self.send_message(expected)
self.assertEqual(
expected,
actual,
"expected {0}, got {1}".format(expected, actual)
)
def test_long_message_echo(self):
"""test that a message longer than 16 bytes echoes in 16-byte chunks"""
expected = "Four score and seven years ago our fathers did stuff"
actual = self.send_message(expected)
self.assertEqual(
expected,
actual,
"expected {0}, got {1}".format(expected, actual)
)
if __name__ == '__main__':
unittest.main()