-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_server.py
80 lines (65 loc) · 2.28 KB
/
test_server.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""
Test the web server.
"""
import mock
from unittest import TestCase
from flask import g
from pyfreelan.server import (
parse_endpoint,
HTTPServer,
)
class ParsingTests(TestCase):
def test_parse_endpoint_with_valid_input(self):
hostname, port = parse_endpoint('this.is.a.host:1234')
self.assertEqual('this.is.a.host', hostname)
self.assertEqual(1234, port)
def test_parse_endpoint_with_valid_ipv6_input(self):
hostname, port = parse_endpoint('[ffff::0:00:eeee]:1234')
self.assertEqual('ffff::eeee', hostname)
self.assertEqual(1234, port)
def test_parse_endpoint_with_hostname_only_no_default(self):
hostname, port = parse_endpoint('0.0.0.0')
self.assertEqual('0.0.0.0', hostname)
self.assertEqual(0, port)
def test_parse_endpoint_with_hostname_only_and_default(self):
hostname, port = parse_endpoint('[::]', default_port=9999)
self.assertEqual('::', hostname)
self.assertEqual(9999, port)
class ServerTests(TestCase):
def test_http_server_parses_configuration(self):
reactor = mock.MagicMock()
configuration = {
'listen_on': '0.0.0.0:1234',
'secret_key': 'secret',
}
callbacks = {
'sign_certificate_request': None,
}
server = HTTPServer(
reactor=reactor,
configuration=configuration,
callbacks=callbacks,
)
reactor.listenTCP.assert_called_once_with(
1234,
server.site,
interface='0.0.0.0',
)
self.assertEqual(configuration, server.configuration)
self.assertEqual(callbacks, server.callbacks)
def test_http_server_registers_into_the_application_context(self):
reactor = mock.MagicMock()
configuration = {
'listen_on': '0.0.0.0:1234',
'secret_key': 'secret',
}
callbacks = {}
server = HTTPServer(
reactor=reactor,
configuration=configuration,
callbacks=callbacks,
)
with server.app.test_request_context('/'):
self.assertFalse(hasattr(g, 'http_server'))
server.app.preprocess_request()
self.assertEqual(getattr(g, 'http_server', None), server)