Merge lp:~mandel/ubuntu-sso-client/fix-webclient-tests into lp:ubuntu-sso-client
- fix-webclient-tests
- Merge into trunk
Proposed by
Manuel de la Peña
Status: | Merged | ||||
---|---|---|---|---|---|
Approved by: | Manuel de la Peña | ||||
Approved revision: | 967 | ||||
Merged at revision: | 946 | ||||
Proposed branch: | lp:~mandel/ubuntu-sso-client/fix-webclient-tests | ||||
Merge into: | lp:ubuntu-sso-client | ||||
Prerequisite: | lp:~mandel/ubuntu-sso-client/fix-broken-tests | ||||
Diff against target: |
420 lines (+115/-155) 4 files modified
ubuntu_sso/utils/tests/test_common.py (+7/-22) ubuntu_sso/utils/webclient/tests/__init__.py (+0/-94) ubuntu_sso/utils/webclient/tests/test_timestamp.py (+7/-5) ubuntu_sso/utils/webclient/tests/test_webclient.py (+101/-34) |
||||
To merge this branch: | bzr merge lp:~mandel/ubuntu-sso-client/fix-webclient-tests | ||||
Related bugs: |
|
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Brian Curtin (community) | Approve | ||
Diego Sarmentero (community) | Approve | ||
Review via email: mp+101410@code.launchpad.net |
Commit message
- Added the use of the new added webserver that cleans resources correctly (LP: #960436).
Description of the change
- Added the use of the new added webserver that cleans resources correctly (LP: #960436).
To post a comment you must log in.
- 959. By Manuel de la Peña
-
Link bug.
- 960. By Manuel de la Peña
-
Use connetionLost to get a deferred when we cleaned everything.
- 961. By Manuel de la Peña
-
Merged fix-broken-tests into fix-webclient-
tests.
- 962. By Manuel de la Peña
-
Merged fix-broken-tests into fix-webclient-
tests. - 963. By Manuel de la Peña
-
Use the correct namespaces.
Revision history for this message
Brian Curtin (brian.curtin) wrote : | # |
60 + # TODO: why do we need to use localhost and not just use the webserver
61 + # get_uri???
What does get_uri give? localhost does work work fine here.
review:
Needs Information
- 964. By Manuel de la Peña
-
Merged fix-broken-tests into fix-webclient-
tests. - 965. By Manuel de la Peña
-
Updated branch to use the new API.
- 966. By Manuel de la Peña
-
Merged fix-broken-tests into fix-webclient-
tests. - 967. By Manuel de la Peña
-
Updated to use the new API.
Revision history for this message
Brian Curtin (brian.curtin) : | # |
review:
Approve
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'ubuntu_sso/utils/tests/test_common.py' | |||
2 | --- ubuntu_sso/utils/tests/test_common.py 2012-04-09 17:38:24 +0000 | |||
3 | +++ ubuntu_sso/utils/tests/test_common.py 2012-04-17 20:34:24 +0000 | |||
4 | @@ -33,11 +33,11 @@ | |||
5 | 33 | import sys | 33 | import sys |
6 | 34 | import time | 34 | import time |
7 | 35 | 35 | ||
8 | 36 | from twisted.application import internet, service | ||
9 | 37 | from twisted.internet import defer | 36 | from twisted.internet import defer |
10 | 38 | from twisted.internet.threads import deferToThread | 37 | from twisted.internet.threads import deferToThread |
12 | 39 | from twisted.web import server, resource | 38 | from twisted.web import resource |
13 | 40 | from ubuntuone.devtools.handlers import MementoHandler | 39 | from ubuntuone.devtools.handlers import MementoHandler |
14 | 40 | from ubuntuone.devtools.testing.txwebserver import HTTPWebServer | ||
15 | 41 | 41 | ||
16 | 42 | from ubuntu_sso import utils | 42 | from ubuntu_sso import utils |
17 | 43 | from ubuntu_sso.tests import TestCase, EMAIL, TOKEN | 43 | from ubuntu_sso.tests import TestCase, EMAIL, TOKEN |
18 | @@ -306,30 +306,14 @@ | |||
19 | 306 | return "" | 306 | return "" |
20 | 307 | 307 | ||
21 | 308 | 308 | ||
23 | 309 | class MockWebServer(object): | 309 | class MockWebServer(HTTPWebServer): |
24 | 310 | """A mock webserver for testing.""" | 310 | """A mock webserver for testing.""" |
25 | 311 | 311 | ||
26 | 312 | def __init__(self): | 312 | def __init__(self): |
27 | 313 | """Start up this instance.""" | 313 | """Start up this instance.""" |
28 | 314 | # pylint: disable=E1101 | 314 | # pylint: disable=E1101 |
47 | 315 | self.root = RootResource() | 315 | root = RootResource() |
48 | 316 | site = server.Site(self.root) | 316 | super(MockWebServer, self).__init__(root) |
31 | 317 | application = service.Application('web') | ||
32 | 318 | self.service_collection = service.IServiceCollection(application) | ||
33 | 319 | self.tcpserver = internet.TCPServer(0, site) | ||
34 | 320 | self.tcpserver.setServiceParent(self.service_collection) | ||
35 | 321 | self.service_collection.startService() | ||
36 | 322 | |||
37 | 323 | def get_url(self): | ||
38 | 324 | """Build the url for this mock server.""" | ||
39 | 325 | # pylint: disable=W0212 | ||
40 | 326 | port_num = self.tcpserver._port.getHost().port | ||
41 | 327 | return "http://localhost:%d/" % port_num | ||
42 | 328 | |||
43 | 329 | def stop(self): | ||
44 | 330 | """Shut it down.""" | ||
45 | 331 | # pylint: disable=E1101 | ||
46 | 332 | self.service_collection.stopService() | ||
49 | 333 | 317 | ||
50 | 334 | 318 | ||
51 | 335 | class FakedError(Exception): | 319 | class FakedError(Exception): |
52 | @@ -344,8 +328,9 @@ | |||
53 | 344 | """Initialize a fake webserver.""" | 328 | """Initialize a fake webserver.""" |
54 | 345 | yield super(TimestampCheckerTestCase, self).setUp() | 329 | yield super(TimestampCheckerTestCase, self).setUp() |
55 | 346 | self.ws = MockWebServer() | 330 | self.ws = MockWebServer() |
56 | 331 | self.ws.start() | ||
57 | 347 | self.addCleanup(self.ws.stop) | 332 | self.addCleanup(self.ws.stop) |
59 | 348 | self.patch(SyncTimestampChecker, "SERVER_URL", self.ws.get_url()) | 333 | self.patch(SyncTimestampChecker, "SERVER_URL", self.ws.get_iri()) |
60 | 349 | 334 | ||
61 | 350 | @defer.inlineCallbacks | 335 | @defer.inlineCallbacks |
62 | 351 | def test_returned_value_is_int(self): | 336 | def test_returned_value_is_int(self): |
63 | 352 | 337 | ||
64 | === modified file 'ubuntu_sso/utils/webclient/tests/__init__.py' | |||
65 | --- ubuntu_sso/utils/webclient/tests/__init__.py 2012-04-09 17:38:24 +0000 | |||
66 | +++ ubuntu_sso/utils/webclient/tests/__init__.py 2012-04-17 20:34:24 +0000 | |||
67 | @@ -28,14 +28,6 @@ | |||
68 | 28 | # files in the program, then also delete it here. | 28 | # files in the program, then also delete it here. |
69 | 29 | """Tests for the proxy-aware webclient.""" | 29 | """Tests for the proxy-aware webclient.""" |
70 | 30 | 30 | ||
71 | 31 | from twisted.application import internet, service | ||
72 | 32 | from twisted.internet import ssl | ||
73 | 33 | from twisted.web import http, server | ||
74 | 34 | |||
75 | 35 | |||
76 | 36 | # Some settings are not used as described in: | ||
77 | 37 | # https://bugzilla.gnome.org/show_bug.cgi?id=648237 | ||
78 | 38 | |||
79 | 39 | TEMPLATE_GSETTINGS_OUTPUT = """\ | 31 | TEMPLATE_GSETTINGS_OUTPUT = """\ |
80 | 40 | org.gnome.system.proxy autoconfig-url '{autoconfig_url}' | 32 | org.gnome.system.proxy autoconfig-url '{autoconfig_url}' |
81 | 41 | org.gnome.system.proxy ignore-hosts {ignore_hosts:s} | 33 | org.gnome.system.proxy ignore-hosts {ignore_hosts:s} |
82 | @@ -69,89 +61,3 @@ | |||
83 | 69 | "socks_host": "", | 61 | "socks_host": "", |
84 | 70 | "socks_port": 0, | 62 | "socks_port": 0, |
85 | 71 | } | 63 | } |
86 | 72 | |||
87 | 73 | |||
88 | 74 | class SaveHTTPChannel(http.HTTPChannel): | ||
89 | 75 | """A save protocol to be used in tests.""" | ||
90 | 76 | |||
91 | 77 | protocolInstance = None | ||
92 | 78 | |||
93 | 79 | # pylint: disable=C0103 | ||
94 | 80 | def connectionMade(self): | ||
95 | 81 | """Keep track of the given protocol.""" | ||
96 | 82 | SaveHTTPChannel.protocolInstance = self | ||
97 | 83 | http.HTTPChannel.connectionMade(self) | ||
98 | 84 | |||
99 | 85 | |||
100 | 86 | class SaveSite(server.Site): | ||
101 | 87 | """A site that let us know when it closed.""" | ||
102 | 88 | |||
103 | 89 | protocol = SaveHTTPChannel | ||
104 | 90 | |||
105 | 91 | def __init__(self, *args, **kwargs): | ||
106 | 92 | """Create a new instance.""" | ||
107 | 93 | server.Site.__init__(self, *args, **kwargs) | ||
108 | 94 | # we disable the timeout in the tests, we will deal with it manually. | ||
109 | 95 | # pylint: disable=C0103 | ||
110 | 96 | self.timeOut = None | ||
111 | 97 | |||
112 | 98 | |||
113 | 99 | class BaseMockWebServer(object): | ||
114 | 100 | """A mock webserver for testing""" | ||
115 | 101 | |||
116 | 102 | def __init__(self, ssl_settings=None): | ||
117 | 103 | """Start up this instance.""" | ||
118 | 104 | self.root = self.get_root_resource() | ||
119 | 105 | self.site = SaveSite(self.root) | ||
120 | 106 | application = service.Application('web') | ||
121 | 107 | self.service_collection = service.IServiceCollection(application) | ||
122 | 108 | #pylint: disable=E1101 | ||
123 | 109 | ssl_context = None | ||
124 | 110 | if (ssl_settings is not None | ||
125 | 111 | and 'key' in ssl_settings | ||
126 | 112 | and 'cert' in ssl_settings): | ||
127 | 113 | ssl_context = ssl.DefaultOpenSSLContextFactory(ssl_settings['key'], | ||
128 | 114 | ssl_settings['cert']) | ||
129 | 115 | self.ssl_server = internet.SSLServer(0, self.site, ssl_context) | ||
130 | 116 | else: | ||
131 | 117 | self.ssl_server = None | ||
132 | 118 | self.server = internet.TCPServer(0, self.site) | ||
133 | 119 | self.server.setServiceParent(self.service_collection) | ||
134 | 120 | if self.ssl_server: | ||
135 | 121 | self.ssl_server.setServiceParent(self.service_collection) | ||
136 | 122 | self.service_collection.startService() | ||
137 | 123 | |||
138 | 124 | def get_root_resource(self): | ||
139 | 125 | """Get the root resource with all the children.""" | ||
140 | 126 | raise NotImplementedError | ||
141 | 127 | |||
142 | 128 | def get_iri(self): | ||
143 | 129 | """Build the iri for this mock server.""" | ||
144 | 130 | url = u"http://127.0.0.1:%d/" | ||
145 | 131 | return url % self.get_port() | ||
146 | 132 | |||
147 | 133 | def get_ssl_iri(self): | ||
148 | 134 | """Build the ssl iri for this mock server.""" | ||
149 | 135 | if self.ssl_server: | ||
150 | 136 | url = u"https://127.0.0.1:%d/" | ||
151 | 137 | return url % self.get_ssl_port() | ||
152 | 138 | |||
153 | 139 | def get_port(self): | ||
154 | 140 | """Return the port where we are listening.""" | ||
155 | 141 | # pylint: disable=W0212 | ||
156 | 142 | return self.server._port.getHost().port | ||
157 | 143 | # pylint: enable=W0212 | ||
158 | 144 | |||
159 | 145 | def get_ssl_port(self): | ||
160 | 146 | """Return the ssl port where we are listening.""" | ||
161 | 147 | # pylint: disable=W0212 | ||
162 | 148 | if self.ssl_server: | ||
163 | 149 | return self.ssl_server._port.getHost().port | ||
164 | 150 | # pylint: enable=W0212 | ||
165 | 151 | |||
166 | 152 | def stop(self): | ||
167 | 153 | """Shut it down.""" | ||
168 | 154 | #pylint: disable=E1101 | ||
169 | 155 | if self.site.protocol.protocolInstance: | ||
170 | 156 | self.site.protocol.protocolInstance.timeoutConnection() | ||
171 | 157 | return self.service_collection.stopService() | ||
172 | 158 | 64 | ||
173 | === modified file 'ubuntu_sso/utils/webclient/tests/test_timestamp.py' | |||
174 | --- ubuntu_sso/utils/webclient/tests/test_timestamp.py 2012-04-09 17:38:24 +0000 | |||
175 | +++ ubuntu_sso/utils/webclient/tests/test_timestamp.py 2012-04-17 20:34:24 +0000 | |||
176 | @@ -32,8 +32,9 @@ | |||
177 | 32 | from twisted.trial.unittest import TestCase | 32 | from twisted.trial.unittest import TestCase |
178 | 33 | from twisted.web import resource | 33 | from twisted.web import resource |
179 | 34 | 34 | ||
180 | 35 | from ubuntuone.devtools.testing.txwebserver import HTTPWebServer | ||
181 | 36 | |||
182 | 35 | from ubuntu_sso.utils.webclient import timestamp, webclient_module | 37 | from ubuntu_sso.utils.webclient import timestamp, webclient_module |
183 | 36 | from ubuntu_sso.utils.webclient.tests import BaseMockWebServer | ||
184 | 37 | 38 | ||
185 | 38 | 39 | ||
186 | 39 | class FakedError(Exception): | 40 | class FakedError(Exception): |
187 | @@ -59,12 +60,12 @@ | |||
188 | 59 | return "" | 60 | return "" |
189 | 60 | 61 | ||
190 | 61 | 62 | ||
192 | 62 | class MockWebServer(BaseMockWebServer): | 63 | class MockWebServer(HTTPWebServer): |
193 | 63 | """A mock webserver for testing.""" | 64 | """A mock webserver for testing.""" |
194 | 64 | 65 | ||
198 | 65 | def get_root_resource(self): | 66 | def __init__(self): |
199 | 66 | """Get the root resource with all the children.""" | 67 | """Create a new server.""" |
200 | 67 | return RootResource() | 68 | super(MockWebServer, self).__init__(RootResource()) |
201 | 68 | 69 | ||
202 | 69 | 70 | ||
203 | 70 | class TimestampCheckerTestCase(TestCase): | 71 | class TimestampCheckerTestCase(TestCase): |
204 | @@ -76,6 +77,7 @@ | |||
205 | 76 | def setUp(self): | 77 | def setUp(self): |
206 | 77 | yield super(TimestampCheckerTestCase, self).setUp() | 78 | yield super(TimestampCheckerTestCase, self).setUp() |
207 | 78 | self.ws = MockWebServer() | 79 | self.ws = MockWebServer() |
208 | 80 | self.ws.start() | ||
209 | 79 | self.addCleanup(self.ws.stop) | 81 | self.addCleanup(self.ws.stop) |
210 | 80 | self.webclient_class = webclient_module().WebClient | 82 | self.webclient_class = webclient_module().WebClient |
211 | 81 | self.patch(timestamp.TimestampChecker, "SERVER_IRI", self.ws.get_iri()) | 83 | self.patch(timestamp.TimestampChecker, "SERVER_IRI", self.ws.get_iri()) |
212 | 82 | 84 | ||
213 | === modified file 'ubuntu_sso/utils/webclient/tests/test_webclient.py' | |||
214 | --- ubuntu_sso/utils/webclient/tests/test_webclient.py 2012-04-12 12:07:46 +0000 | |||
215 | +++ ubuntu_sso/utils/webclient/tests/test_webclient.py 2012-04-17 20:34:24 +0000 | |||
216 | @@ -42,6 +42,11 @@ | |||
217 | 42 | 42 | ||
218 | 43 | from ubuntuone.devtools.testcases import TestCase | 43 | from ubuntuone.devtools.testcases import TestCase |
219 | 44 | from ubuntuone.devtools.testcases.squid import SquidTestCase | 44 | from ubuntuone.devtools.testcases.squid import SquidTestCase |
220 | 45 | from ubuntuone.devtools.testing.txwebserver import ( | ||
221 | 46 | HTTPWebServer, | ||
222 | 47 | HTTPSWebServer, | ||
223 | 48 | ) | ||
224 | 49 | |||
225 | 45 | 50 | ||
226 | 46 | from ubuntu_sso import ( | 51 | from ubuntu_sso import ( |
227 | 47 | keyring, | 52 | keyring, |
228 | @@ -53,7 +58,6 @@ | |||
229 | 53 | from ubuntu_sso.utils.ui import SSL_DETAILS_TEMPLATE | 58 | from ubuntu_sso.utils.ui import SSL_DETAILS_TEMPLATE |
230 | 54 | from ubuntu_sso.utils.webclient import gsettings, txweb | 59 | from ubuntu_sso.utils.webclient import gsettings, txweb |
231 | 55 | from ubuntu_sso.utils.webclient.common import BaseWebClient, HeaderDict, oauth | 60 | from ubuntu_sso.utils.webclient.common import BaseWebClient, HeaderDict, oauth |
232 | 56 | from ubuntu_sso.utils.webclient.tests import BaseMockWebServer | ||
233 | 57 | 61 | ||
234 | 58 | ANY_VALUE = object() | 62 | ANY_VALUE = object() |
235 | 59 | SAMPLE_KEY = "result" | 63 | SAMPLE_KEY = "result" |
236 | @@ -182,34 +186,49 @@ | |||
237 | 182 | return "ERROR: Expected OAuth header not present." | 186 | return "ERROR: Expected OAuth header not present." |
238 | 183 | 187 | ||
239 | 184 | 188 | ||
253 | 185 | class MockWebServer(BaseMockWebServer): | 189 | def get_root_resource(): |
254 | 186 | """A mock webserver for the webclient tests.""" | 190 | """Get the root resource with all the children.""" |
255 | 187 | 191 | root = resource.Resource() | |
256 | 188 | def get_root_resource(self): | 192 | root.putChild(SIMPLERESOURCE, SimpleResource()) |
257 | 189 | """Get the root resource with all the children.""" | 193 | root.putChild(BYTEZERORESOURCE, ByteZeroResource()) |
258 | 190 | root = resource.Resource() | 194 | root.putChild(POSTABLERESOURCE, PostableResource()) |
259 | 191 | root.putChild(SIMPLERESOURCE, SimpleResource()) | 195 | |
260 | 192 | root.putChild(BYTEZERORESOURCE, ByteZeroResource()) | 196 | root.putChild(THROWERROR, resource.NoResource()) |
261 | 193 | root.putChild(POSTABLERESOURCE, PostableResource()) | 197 | |
262 | 194 | 198 | unauthorized_resource = resource.ErrorPage(resource.http.UNAUTHORIZED, | |
250 | 195 | root.putChild(THROWERROR, resource.NoResource()) | ||
251 | 196 | |||
252 | 197 | unauthorized_resource = resource.ErrorPage(resource.http.UNAUTHORIZED, | ||
263 | 198 | "Unauthorized", "Unauthorized") | 199 | "Unauthorized", "Unauthorized") |
269 | 199 | root.putChild(UNAUTHORIZED, unauthorized_resource) | 200 | root.putChild(UNAUTHORIZED, unauthorized_resource) |
270 | 200 | root.putChild(HEADONLY, HeadOnlyResource()) | 201 | root.putChild(HEADONLY, HeadOnlyResource()) |
271 | 201 | root.putChild(VERIFYHEADERS, VerifyHeadersResource()) | 202 | root.putChild(VERIFYHEADERS, VerifyHeadersResource()) |
272 | 202 | root.putChild(VERIFYPOSTPARAMS, VerifyPostParameters()) | 203 | root.putChild(VERIFYPOSTPARAMS, VerifyPostParameters()) |
273 | 203 | root.putChild(OAUTHRESOURCE, OAuthCheckerResource()) | 204 | root.putChild(OAUTHRESOURCE, OAuthCheckerResource()) |
274 | 204 | 205 | ||
280 | 205 | db = checkers.InMemoryUsernamePasswordDatabaseDontUse() | 206 | db = checkers.InMemoryUsernamePasswordDatabaseDontUse() |
281 | 206 | db.addUser(SAMPLE_USERNAME, SAMPLE_PASSWORD) | 207 | db.addUser(SAMPLE_USERNAME, SAMPLE_PASSWORD) |
282 | 207 | test_portal = portal.Portal(SimpleRealm(), [db]) | 208 | test_portal = portal.Portal(SimpleRealm(), [db]) |
283 | 208 | cred_factory = guard.BasicCredentialFactory("example.org") | 209 | cred_factory = guard.BasicCredentialFactory("example.org") |
284 | 209 | guarded_resource = guard.HTTPAuthSessionWrapper(test_portal, | 210 | guarded_resource = guard.HTTPAuthSessionWrapper(test_portal, |
285 | 210 | [cred_factory]) | 211 | [cred_factory]) |
288 | 211 | root.putChild(GUARDED, guarded_resource) | 212 | root.putChild(GUARDED, guarded_resource) |
289 | 212 | return root | 213 | return root |
290 | 214 | |||
291 | 215 | |||
292 | 216 | class HTTPMockWebServer(HTTPWebServer): | ||
293 | 217 | """A mock webserver for the webclient tests.""" | ||
294 | 218 | |||
295 | 219 | def __init__(self): | ||
296 | 220 | """Create a new instance.""" | ||
297 | 221 | root = get_root_resource() | ||
298 | 222 | super(HTTPMockWebServer, self).__init__(root) | ||
299 | 223 | |||
300 | 224 | |||
301 | 225 | class HTTPSMockWebServer(HTTPSWebServer): | ||
302 | 226 | """A mock webserver for the webclient tests.""" | ||
303 | 227 | |||
304 | 228 | def __init__(self, ssl_settings): | ||
305 | 229 | """Create a new instance.""" | ||
306 | 230 | root = get_root_resource() | ||
307 | 231 | super(HTTPSMockWebServer, self).__init__(root, ssl_settings) | ||
308 | 213 | 232 | ||
309 | 214 | 233 | ||
310 | 215 | class FakeQApplication(object): | 234 | class FakeQApplication(object): |
311 | @@ -264,11 +283,12 @@ | |||
312 | 264 | @defer.inlineCallbacks | 283 | @defer.inlineCallbacks |
313 | 265 | def setUp(self): | 284 | def setUp(self): |
314 | 266 | yield super(WebClientTestCase, self).setUp() | 285 | yield super(WebClientTestCase, self).setUp() |
315 | 267 | self.ws = MockWebServer() | ||
316 | 268 | self.addCleanup(self.ws.stop) | ||
317 | 269 | self.base_iri = self.ws.get_iri() | ||
318 | 270 | self.wc = self.webclient_factory() | 286 | self.wc = self.webclient_factory() |
319 | 271 | self.addCleanup(self.wc.shutdown) | 287 | self.addCleanup(self.wc.shutdown) |
320 | 288 | self.ws = HTTPMockWebServer() | ||
321 | 289 | self.ws.start() | ||
322 | 290 | self.addCleanup(self.ws.stop) | ||
323 | 291 | self.base_iri = self.ws.get_iri() | ||
324 | 272 | 292 | ||
325 | 273 | @defer.inlineCallbacks | 293 | @defer.inlineCallbacks |
326 | 274 | def test_request_takes_an_iri(self): | 294 | def test_request_takes_an_iri(self): |
327 | @@ -420,6 +440,52 @@ | |||
328 | 420 | 440 | ||
329 | 421 | webclient_factory = txweb.WebClient | 441 | webclient_factory = txweb.WebClient |
330 | 422 | 442 | ||
331 | 443 | @defer.inlineCallbacks | ||
332 | 444 | def setUp(self): | ||
333 | 445 | """Set the diff tests.""" | ||
334 | 446 | # delay import, otherwise a default reactor gets installed | ||
335 | 447 | from twisted.web import client | ||
336 | 448 | self.factory = client.HTTPClientFactory | ||
337 | 449 | # set the factory to be used | ||
338 | 450 | # Hook the server's buildProtocol to make the protocol instance | ||
339 | 451 | # accessible to tests and ensure that the reactor is clean! | ||
340 | 452 | build_protocol = self.factory.buildProtocol | ||
341 | 453 | self.serverProtocol = None | ||
342 | 454 | |||
343 | 455 | def remember_protocol_instance(my_self, addr): | ||
344 | 456 | """Remember the protocol used in the test.""" | ||
345 | 457 | protocol = build_protocol(my_self, addr) | ||
346 | 458 | self.serverProtocol = protocol | ||
347 | 459 | on_connection_lost = defer.Deferred() | ||
348 | 460 | connection_lost = protocol.connectionLost | ||
349 | 461 | |||
350 | 462 | def defer_connection_lost(protocol, *a): | ||
351 | 463 | """Lost connection.""" | ||
352 | 464 | if not on_connection_lost.called: | ||
353 | 465 | on_connection_lost.callback(None) | ||
354 | 466 | connection_lost(protocol, *a) | ||
355 | 467 | |||
356 | 468 | self.patch(protocol, 'connectionLost', defer_connection_lost) | ||
357 | 469 | |||
358 | 470 | def cleanup(): | ||
359 | 471 | """Clean the connection.""" | ||
360 | 472 | if self.serverProtocol.transport is not None: | ||
361 | 473 | self.serverProtocol.transport.loseConnection() | ||
362 | 474 | return on_connection_lost | ||
363 | 475 | |||
364 | 476 | self.addCleanup(cleanup) | ||
365 | 477 | return protocol | ||
366 | 478 | |||
367 | 479 | self.factory.buildProtocol = remember_protocol_instance | ||
368 | 480 | self.addCleanup(self.set_build_protocol, build_protocol) | ||
369 | 481 | txweb.WebClient.client_factory = self.factory | ||
370 | 482 | |||
371 | 483 | yield super(TxWebClientTestCase, self).setUp() | ||
372 | 484 | |||
373 | 485 | def set_build_protocol(self, method): | ||
374 | 486 | """Set the method back.""" | ||
375 | 487 | self.factory.buildProtocol = method | ||
376 | 488 | |||
377 | 423 | 489 | ||
378 | 424 | class TxWebClientReactorReplaceableTestCase(TestCase): | 490 | class TxWebClientReactorReplaceableTestCase(TestCase): |
379 | 425 | """In the txweb client the reactor is replaceable.""" | 491 | """In the txweb client the reactor is replaceable.""" |
380 | @@ -478,7 +544,8 @@ | |||
381 | 478 | @defer.inlineCallbacks | 544 | @defer.inlineCallbacks |
382 | 479 | def setUp(self): | 545 | def setUp(self): |
383 | 480 | yield super(BasicProxyTestCase, self).setUp() | 546 | yield super(BasicProxyTestCase, self).setUp() |
385 | 481 | self.ws = MockWebServer() | 547 | self.ws = HTTPMockWebServer() |
386 | 548 | self.ws.start() | ||
387 | 482 | self.addCleanup(self.ws.stop) | 549 | self.addCleanup(self.ws.stop) |
388 | 483 | self.base_iri = self.ws.get_iri() | 550 | self.base_iri = self.ws.get_iri() |
389 | 484 | self.wc = webclient.webclient_factory() | 551 | self.wc = webclient.webclient_factory() |
390 | @@ -891,10 +958,10 @@ | |||
391 | 891 | self.cert_details) | 958 | self.cert_details) |
392 | 892 | self.addCleanup(self._clean_ssl_certificate_files) | 959 | self.addCleanup(self._clean_ssl_certificate_files) |
393 | 893 | 960 | ||
395 | 894 | self.ws = MockWebServer(self.ssl_settings) | 961 | self.ws = HTTPSMockWebServer(self.ssl_settings) |
396 | 962 | self.ws.start() | ||
397 | 895 | self.addCleanup(self.ws.stop) | 963 | self.addCleanup(self.ws.stop) |
398 | 896 | self.base_iri = self.ws.get_iri() | 964 | self.base_iri = self.ws.get_iri() |
399 | 897 | self.base_ssl_iri = self.ws.get_ssl_iri() | ||
400 | 898 | 965 | ||
401 | 899 | def _clean_ssl_certificate_files(self): | 966 | def _clean_ssl_certificate_files(self): |
402 | 900 | """Remove the certificate files.""" | 967 | """Remove the certificate files.""" |
403 | @@ -995,7 +1062,7 @@ | |||
404 | 995 | """ | 1062 | """ |
405 | 996 | # we fail due to the fake ssl cert | 1063 | # we fail due to the fake ssl cert |
406 | 997 | yield self.failUnlessFailure(self.wc.request( | 1064 | yield self.failUnlessFailure(self.wc.request( |
408 | 998 | self.base_ssl_iri + SIMPLERESOURCE), | 1065 | self.base_iri + SIMPLERESOURCE), |
409 | 999 | webclient.WebClientError) | 1066 | webclient.WebClientError) |
410 | 1000 | # https requests do not use the auth proxy therefore called should be | 1067 | # https requests do not use the auth proxy therefore called should be |
411 | 1001 | # empty. This asserts that we are using the correct settings for the | 1068 | # empty. This asserts that we are using the correct settings for the |
412 | @@ -1062,7 +1129,7 @@ | |||
413 | 1062 | self.return_code = USER_SUCCESS | 1129 | self.return_code = USER_SUCCESS |
414 | 1063 | if proxy_settings: | 1130 | if proxy_settings: |
415 | 1064 | self.wc.force_use_proxy(proxy_settings) | 1131 | self.wc.force_use_proxy(proxy_settings) |
417 | 1065 | yield self.wc.request(self.base_ssl_iri + SIMPLERESOURCE) | 1132 | yield self.wc.request(self.base_iri + SIMPLERESOURCE) |
418 | 1066 | details = SSL_DETAILS_TEMPLATE % self.cert_details | 1133 | details = SSL_DETAILS_TEMPLATE % self.cert_details |
419 | 1067 | self.assertIn(('_launch_ssl_dialog', gethostname(), details), | 1134 | self.assertIn(('_launch_ssl_dialog', gethostname(), details), |
420 | 1068 | self.called) | 1135 | self.called) |
+1