Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 # This will attempt to import the actual App Engine modules, and if it fails, | 5 # This will attempt to import the actual App Engine modules, and if it fails, |
| 6 # they will be replaced with fake modules. This is useful during testing. | 6 # they will be replaced with fake modules. This is useful during testing. |
| 7 try: | 7 try: |
| 8 import google.appengine.ext.webapp as webapp | 8 import google.appengine.ext.webapp as webapp |
| 9 import google.appengine.api.memcache as memcache | 9 import google.appengine.api.memcache as memcache |
| 10 import google.appengine.api.urlfetch as urlfetch | 10 import google.appengine.api.urlfetch as urlfetch |
| 11 except ImportError: | 11 except ImportError: |
| 12 import re | |
| 13 | |
| 14 FAKE_URL_FETCHER_CONFIGURATION = None | |
| 15 | |
| 16 def ConfigureFakeUrlFetch(configuration): | |
| 17 global FAKE_URL_FETCHER_CONFIGURATION | |
| 18 FAKE_URL_FETCHER_CONFIGURATION = dict( | |
| 19 (re.compile(k), v) for k, v in configuration.iteritems()) | |
| 20 | |
| 21 def GetConfiguration(key): | |
|
not at google - send to devlin
2012/08/13 22:36:46
add like
if not FAKE_URL_FETCHER_CONFIGURATION:
cduvall
2012/08/13 23:17:22
Done.
| |
| 22 for k, v in FAKE_URL_FETCHER_CONFIGURATION.iteritems(): | |
| 23 if k.match(key): | |
| 24 return v | |
| 25 return None | |
| 26 | |
| 12 class FakeUrlFetch(object): | 27 class FakeUrlFetch(object): |
| 13 """A fake urlfetch module that errors for all method calls. | 28 """A fake urlfetch module that errors for all method calls. |
|
not at google - send to devlin
2012/08/13 22:36:46
comment is out of date
cduvall
2012/08/13 23:17:22
Done.
| |
| 14 """ | 29 """ |
| 30 class _Response(object): | |
| 31 def __init__(self, content): | |
| 32 self.content = content | |
| 33 self.headers = { 'content-type': 'none' } | |
| 34 self.status_code = 200 | |
| 35 | |
| 36 class _RPC(object): | |
| 37 def __init__(self): | |
| 38 self.result = None | |
| 39 | |
| 40 def wait(self): | |
| 41 pass | |
| 42 | |
| 43 def get_result(self): | |
| 44 return self.result | |
| 45 | |
| 15 def fetch(self, url): | 46 def fetch(self, url): |
| 16 raise NotImplementedError() | 47 return self._Response(GetConfiguration(url).fetch(url)) |
| 17 | 48 |
| 18 def create_rpc(self): | 49 def create_rpc(self): |
| 19 raise NotImplementedError() | 50 return self._RPC() |
| 20 | 51 |
| 21 def make_fetch_call(self): | 52 def make_fetch_call(self, rpc, url): |
| 22 raise NotImplementedError() | 53 rpc.result = self.fetch(url) |
| 23 urlfetch = FakeUrlFetch() | 54 urlfetch = FakeUrlFetch() |
| 24 | 55 |
| 25 class InMemoryMemcache(object): | 56 class InMemoryMemcache(object): |
| 26 """A memcache that stores items in memory instead of using the memcache | 57 """A memcache that stores items in memory instead of using the memcache |
| 27 module. | 58 module. |
| 28 """ | 59 """ |
| 29 def __init__(self): | 60 def __init__(self): |
| 30 self._cache = {} | 61 self._cache = {} |
| 31 | 62 |
| 32 def set(self, key, value, namespace, time=60): | 63 def set(self, key, value, namespace, time=60): |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 46 | 77 |
| 47 # A fake webapp.RequestHandler class for Handler to extend. | 78 # A fake webapp.RequestHandler class for Handler to extend. |
| 48 class webapp(object): | 79 class webapp(object): |
| 49 class RequestHandler(object): | 80 class RequestHandler(object): |
| 50 def __init__(self, request, response): | 81 def __init__(self, request, response): |
| 51 self.request = request | 82 self.request = request |
| 52 self.response = response | 83 self.response = response |
| 53 | 84 |
| 54 def redirect(self, path): | 85 def redirect(self, path): |
| 55 self.request.path = path | 86 self.request.path = path |
| OLD | NEW |