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 from in_memory_memcache import InMemoryMemcache | 5 # This will attempt to import the actual App Engine modules, and if it fails, |
6 | |
7 # This will attempt to import the actual Appengine modules, and if it fails, | |
8 # 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. |
9 try: | 7 try: |
10 import google.appengine.ext.webapp as webapp | 8 import google.appengine.ext.webapp as webapp |
11 import google.appengine.api.memcache as memcache | 9 import google.appengine.api.memcache as memcache |
12 import google.appengine.api.urlfetch as urlfetch | 10 import google.appengine.api.urlfetch as urlfetch |
13 except ImportError: | 11 except ImportError: |
14 class FakeUrlFetch(object): | 12 class FakeUrlFetch(object): |
| 13 """A fake urlfetch module that errors for all method calls. |
| 14 """ |
15 def fetch(self, url): | 15 def fetch(self, url): |
16 raise NotImplementedError() | 16 raise NotImplementedError() |
17 | 17 |
18 def create_rpc(self): | 18 def create_rpc(self): |
19 raise NotImplementedError() | 19 raise NotImplementedError() |
20 | 20 |
21 def make_fetch_call(self): | 21 def make_fetch_call(self): |
22 raise NotImplementedError() | 22 raise NotImplementedError() |
23 urlfetch = FakeUrlFetch() | 23 urlfetch = FakeUrlFetch() |
24 | 24 |
25 # Use an in-memory memcache if Appengine memcache isn't available. | 25 class InMemoryMemcache(object): |
| 26 """A memcache that stores items in memory instead of using the memcache |
| 27 module. |
| 28 """ |
| 29 def __init__(self): |
| 30 self._cache = {} |
| 31 |
| 32 def set(self, key, value, namespace, time=60): |
| 33 if namespace not in self._cache: |
| 34 self._cache[namespace] = {} |
| 35 self._cache[namespace][key] = value |
| 36 |
| 37 def get(self, key, namespace): |
| 38 if namespace not in self._cache: |
| 39 return None |
| 40 return self._cache[namespace].get(key, None) |
| 41 |
| 42 def delete(self, key, namespace): |
| 43 if namespace in self._cache: |
| 44 self._cache[namespace].pop(key) |
26 memcache = InMemoryMemcache() | 45 memcache = InMemoryMemcache() |
27 | 46 |
28 # A fake webapp.RequestHandler class for Handler to extend. | 47 # A fake webapp.RequestHandler class for Handler to extend. |
29 class webapp(object): | 48 class webapp(object): |
30 class RequestHandler(object): | 49 class RequestHandler(object): |
31 def __init__(self, request, response): | 50 def __init__(self, request, response): |
32 self.request = request | 51 self.request = request |
33 self.response = response | 52 self.response = response |
| 53 |
| 54 def redirect(self, path): |
| 55 self.request.path = path |
OLD | NEW |