Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(306)

Side by Side Diff: appengine/components/test_support/test_case.py

Issue 2117833003: endpoints -> webapp2 adapter (Closed) Base URL: https://chromium.googlesource.com/external/github.com/luci/luci-py@master
Patch Set: decode bool field Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 # Copyright 2013 The LUCI Authors. All rights reserved. 1 # Copyright 2013 The LUCI Authors. All rights reserved.
2 # Use of this source code is governed under the Apache License, Version 2.0 2 # Use of this source code is governed under the Apache License, Version 2.0
3 # that can be found in the LICENSE file. 3 # that can be found in the LICENSE file.
4 4
5 import base64 5 import base64
6 import contextlib 6 import contextlib
7 import datetime 7 import datetime
8 import json
8 import logging 9 import logging
9 import time 10 import time
10 import urllib 11 import urllib
11 12
12 import endpoints 13 import endpoints
14 import webapp2
13 import webtest 15 import webtest
14 16
15 from google.appengine.datastore import datastore_stub_util 17 from google.appengine.datastore import datastore_stub_util
16 from google.appengine.ext import ndb 18 from google.appengine.ext import ndb
17 from google.appengine.ext import testbed 19 from google.appengine.ext import testbed
18 20
21 from components import endpoints_webapp2
19 from components import utils 22 from components import utils
20 from depot_tools import auto_stub 23 from depot_tools import auto_stub
21 24
22 # W0212: Access to a protected member XXX of a client class 25 # W0212: Access to a protected member XXX of a client class
23 # pylint: disable=W0212 26 # pylint: disable=W0212
24 27
25 28
26 def mock_now(test, now, seconds): 29 def mock_now(test, now, seconds):
27 """Mocks utcnow() and ndb properties. 30 """Mocks utcnow() and ndb properties.
28 31
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 yield 232 yield
230 except AssertionError: 233 except AssertionError:
231 # Assertion can happen if tests are running on GAE < 1.9.31, where 234 # Assertion can happen if tests are running on GAE < 1.9.31, where
232 # endpoints bug still exists (and causes webapp guts to raise assertion). 235 # endpoints bug still exists (and causes webapp guts to raise assertion).
233 # It should be rare (since we are switching to GAE >= 1.9.31), so don't 236 # It should be rare (since we are switching to GAE >= 1.9.31), so don't
234 # bother to check that assertion was indeed raised. Just skip it if it 237 # bother to check that assertion was indeed raised. Just skip it if it
235 # did. 238 # did.
236 pass 239 pass
237 finally: 240 finally:
238 self.expected_fail_status = None 241 self.expected_fail_status = None
242
243
244 class Webapp2EndpointsTestCase(TestCase):
245 """Base class for a test case that tests Webapp2 Cloud Endpoints Service.
246
247 Webapp2 handlers for such service can be derived using endpoints_webapp2.
248
249 Usage is same as EndpointsTestCase, except call_should_fail is not supported
250 and status parameter in call_api should be used instead.
251 """
252 # Should be set in subclasses to a subclass of remote.Service.
253 api_service_cls = None
254 _app = None
255
256 def setUp(self):
257 super(Webapp2EndpointsTestCase, self).setUp()
258 self._app = webtest.TestApp(
259 webapp2.WSGIApplication(
260 endpoints_webapp2.api_routes(self.api_service_cls),
261 debug=True),
262 extra_environ={'REMOTE_ADDR': '127.0.0.1'})
263
264 def call_api(self, method_name, body=None, status=200):
265 body = body or {}
266 method = getattr(self.api_service_cls, method_name)
267
268 container = endpoints.ResourceContainer.get_request_message(method.remote)
269 method_path = method.method_info.get_path(self.api_service_cls.api_info)
270 url_params = {}
271
272 if isinstance(container, endpoints.ResourceContainer):
273 # If request is a resource container, some data passed in body should be
274 # moved to URL.
275 body = body.copy()
276 for f in container.parameters_message_class.all_fields():
277 if f.name not in body:
278 continue
279 value = body.pop(f.name)
280 path_param = '{%s}' % f.name
281 if path_param in method_path:
282 method_path = method_path.replace(path_param, str(value))
283 else:
284 url_params[f.name] = value
285
286 url = '/api/%s/%s/%s?%s' % (
287 self.api_service_cls.api_info.name,
288 self.api_service_cls.api_info.version,
289 method_path,
290 urllib.urlencode(url_params, doseq=True)
291 )
292
293 return self._app.request(
294 url,
295 method=method.method_info.http_method,
296 body=json.dumps(body),
297 status=status)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698