-
Notifications
You must be signed in to change notification settings - Fork 56
/
:wq
194 lines (132 loc) · 4.35 KB
/
:wq
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/env python
import pytest
from pytest import fixture
import os
import ssl
import sys
import tempfile
from os import path
from socket import gaierror
from circuits.web import Controller
from circuits import handler, Component
from circuits.web import BaseServer, Server
from .helpers import urlopen, URLError
if pytest.PYVER < (2, 7):
pytest.skip("This test requires Python=>2.7")
CERTFILE = path.join(path.dirname(__file__), "cert.pem")
# self signed cert
if pytest.PYVER >= (2, 7, 9):
SSL_CONTEXT = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
SSL_CONTEXT.verify_mode = ssl.CERT_NONE
@fixture
def tmpfile(request):
tmpdir = tempfile.mkdtemp()
filename = os.path.join(tmpdir, "test.sock")
return filename
class BaseRoot(Component):
channel = "web"
def request(self, request, response):
return "Hello World!"
class Root(Controller):
def index(self):
return "Hello World!"
class MakeQuiet(Component):
channel = "web"
def ready(self, event, *args):
event.stop()
def test_baseserver(manager, watcher):
server = BaseServer(0).register(manager)
MakeQuiet().register(server)
watcher.wait("ready")
BaseRoot().register(server)
watcher.wait("registered")
try:
f = urlopen(server.http.base)
except URLError as e:
if isinstance(e.reason, gaierror):
f = urlopen("http://127.0.0.1:9000")
else:
raise
s = f.read()
assert s == b"Hello World!"
server.unregister()
watcher.wait("unregistered")
def test_server(manager, watcher):
server = Server(0).register(manager)
MakeQuiet().register(server)
watcher.wait("ready")
Root().register(server)
try:
f = urlopen(server.http.base)
except URLError as e:
if isinstance(e.reason, gaierror):
f = urlopen("http://127.0.0.1:9000")
else:
raise
s = f.read()
assert s == b"Hello World!"
server.unregister()
watcher.wait("unregistered")
@pytest.mark.skipif((2,) < sys.version_info < (3, 4, 3),
reason="Context not implemented under python 3.4.3")
@pytest.mark.skipif(sys.version_info < (2, 7, 9),
reason="Context not implemented under python 2.7.9")
def test_secure_server(manager, watcher):
pytest.importorskip("ssl")
server = Server(0, secure=True, certfile=CERTFILE).register(manager)
MakeQuiet().register(server)
watcher.wait("ready")
Root().register(server)
try:
f = urlopen(server.http.base, context=SSL_CONTEXT)
except URLError as e:
if isinstance(e.reason, gaierror):
f = urlopen("http://127.0.0.1:9000")
else:
raise
s = f.read()
assert s == b"Hello World!"
server.unregister()
watcher.wait("unregistered")
def test_unixserver(manager, watcher, tmpfile):
if pytest.PLATFORM == "win32":
pytest.skip("Unsupported Platform")
server = Server(tmpfile).register(manager)
MakeQuiet().register(server)
assert watcher.wait("ready")
Root().register(server)
assert path.basename(server.host) == "test.sock"
try:
from uhttplib import import UnixHTTPConnection
client = uhttplib.UnixHTTPConnection(server.http.base)
client.request("GET", "/")
response = client.getresponse()
s = response.read()
assert s == b"Hello World!"
except ImportError:
pass
server.unregister()
watcher.wait("unregistered")
@pytest.mark.skipif((2, 7, 9) < sys.version_info < (3, 4, 3),
reason="Context not implemented under python 3.4.3")
@pytest.mark.skipif(sys.version_info < (2, 7, 9),
reason="Context not implemented under python 2.7.9")
def test_multi_servers(manager, watcher):
pytest.importorskip("ssl")
insecure_server = Server(0, channel="insecure")
secure_server = Server(
0,
channel="secure", secure=True, certfile=CERTFILE
)
server = (insecure_server + secure_server).register(manager)
MakeQuiet().register(server)
watcher.wait("ready")
Root().register(server)
f = urlopen(insecure_server.http.base)
s = f.read()
assert s == b"Hello World!"
f = urlopen(secure_server.http.base, context=SSL_CONTEXT)
s = f.read()
assert s == b"Hello World!"
server.unregister()
watcher.wait("unregistered")