|
15 | 15 |
|
16 | 16 | import atexit |
17 | 17 | import os |
18 | | -from unittest.mock import Mock, patch |
19 | | -from urllib import parse as urlparse |
20 | | - |
21 | | -from twisted.internet import defer |
22 | 18 |
|
23 | 19 | from synapse.api.constants import EventTypes |
24 | | -from synapse.api.errors import CodeMessageException, cs_error |
25 | 20 | from synapse.api.room_versions import RoomVersions |
26 | 21 | from synapse.config.homeserver import HomeServerConfig |
27 | 22 | from synapse.config.server import DEFAULT_ROOM_VERSION |
@@ -187,111 +182,6 @@ def getRawHeaders(name, default=None): |
187 | 182 | return getRawHeaders |
188 | 183 |
|
189 | 184 |
|
190 | | -# This is a mock /resource/ not an entire server |
191 | | -class MockHttpResource: |
192 | | - def __init__(self, prefix=""): |
193 | | - self.callbacks = [] # 3-tuple of method/pattern/function |
194 | | - self.prefix = prefix |
195 | | - |
196 | | - def trigger_get(self, path): |
197 | | - return self.trigger(b"GET", path, None) |
198 | | - |
199 | | - @patch("twisted.web.http.Request") |
200 | | - @defer.inlineCallbacks |
201 | | - def trigger( |
202 | | - self, http_method, path, content, mock_request, federation_auth_origin=None |
203 | | - ): |
204 | | - """Fire an HTTP event. |
205 | | -
|
206 | | - Args: |
207 | | - http_method : The HTTP method |
208 | | - path : The HTTP path |
209 | | - content : The HTTP body |
210 | | - mock_request : Mocked request to pass to the event so it can get |
211 | | - content. |
212 | | - federation_auth_origin (bytes|None): domain to authenticate as, for federation |
213 | | - Returns: |
214 | | - A tuple of (code, response) |
215 | | - Raises: |
216 | | - KeyError If no event is found which will handle the path. |
217 | | - """ |
218 | | - path = self.prefix + path |
219 | | - |
220 | | - # annoyingly we return a twisted http request which has chained calls |
221 | | - # to get at the http content, hence mock it here. |
222 | | - mock_content = Mock() |
223 | | - config = {"read.return_value": content} |
224 | | - mock_content.configure_mock(**config) |
225 | | - mock_request.content = mock_content |
226 | | - |
227 | | - mock_request.method = http_method.encode("ascii") |
228 | | - mock_request.uri = path.encode("ascii") |
229 | | - |
230 | | - mock_request.getClientIP.return_value = "-" |
231 | | - |
232 | | - headers = {} |
233 | | - if federation_auth_origin is not None: |
234 | | - headers[b"Authorization"] = [ |
235 | | - b"X-Matrix origin=%s,key=,sig=" % (federation_auth_origin,) |
236 | | - ] |
237 | | - mock_request.requestHeaders.getRawHeaders = mock_getRawHeaders(headers) |
238 | | - |
239 | | - # return the right path if the event requires it |
240 | | - mock_request.path = path |
241 | | - |
242 | | - # add in query params to the right place |
243 | | - try: |
244 | | - mock_request.args = urlparse.parse_qs(path.split("?")[1]) |
245 | | - mock_request.path = path.split("?")[0] |
246 | | - path = mock_request.path |
247 | | - except Exception: |
248 | | - pass |
249 | | - |
250 | | - if isinstance(path, bytes): |
251 | | - path = path.decode("utf8") |
252 | | - |
253 | | - for (method, pattern, func) in self.callbacks: |
254 | | - if http_method != method: |
255 | | - continue |
256 | | - |
257 | | - matcher = pattern.match(path) |
258 | | - if matcher: |
259 | | - try: |
260 | | - args = [urlparse.unquote(u) for u in matcher.groups()] |
261 | | - |
262 | | - (code, response) = yield defer.ensureDeferred( |
263 | | - func(mock_request, *args) |
264 | | - ) |
265 | | - return code, response |
266 | | - except CodeMessageException as e: |
267 | | - return e.code, cs_error(e.msg, code=e.errcode) |
268 | | - |
269 | | - raise KeyError("No event can handle %s" % path) |
270 | | - |
271 | | - def register_paths(self, method, path_patterns, callback, servlet_name): |
272 | | - for path_pattern in path_patterns: |
273 | | - self.callbacks.append((method, path_pattern, callback)) |
274 | | - |
275 | | - |
276 | | -class MockKey: |
277 | | - alg = "mock_alg" |
278 | | - version = "mock_version" |
279 | | - signature = b"\x9a\x87$" |
280 | | - |
281 | | - @property |
282 | | - def verify_key(self): |
283 | | - return self |
284 | | - |
285 | | - def sign(self, message): |
286 | | - return self |
287 | | - |
288 | | - def verify(self, message, sig): |
289 | | - assert sig == b"\x9a\x87$" |
290 | | - |
291 | | - def encode(self): |
292 | | - return b"<fake_encoded_key>" |
293 | | - |
294 | | - |
295 | 185 | class MockClock: |
296 | 186 | now = 1000 |
297 | 187 |
|
|
0 commit comments