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

Side by Side Diff: third_party/google-endpoints/endpoints/test/api_backend_service_test.py

Issue 2666783008: Add google-endpoints to third_party/. (Closed)
Patch Set: Created 3 years, 10 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
(Empty)
1 # Copyright 2016 Google Inc. All Rights Reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 """Tests for endpoints.api_backend_service."""
16
17 import logging
18 import unittest
19
20 import endpoints.api_backend as api_backend
21 import endpoints.api_backend_service as api_backend_service
22 import endpoints.api_exceptions as api_exceptions
23 import mox
24 import test_util
25
26
27 class ModuleInterfaceTest(test_util.ModuleInterfaceTest,
28 unittest.TestCase):
29
30 MODULE = api_backend_service
31
32
33 class ApiConfigRegistryTest(unittest.TestCase):
34
35 def setUp(self):
36 super(ApiConfigRegistryTest, self).setUp()
37 self.registry = api_backend_service.ApiConfigRegistry()
38
39 def testApiMethodsMapped(self):
40 self.registry.register_backend(
41 '{"methods": {"method1": {"rosyMethod": "foo"}}}')
42 self.assertEquals('foo', self.registry.lookup_api_method('method1'))
43
44 def testAllApiConfigsWithTwoConfigs(self):
45 config1 = '{"methods": {"method1": {"rosyMethod": "c1.foo"}}}'
46 config2 = '{"methods": {"method2": {"rosyMethod": "c2.bar"}}}'
47 self.registry.register_backend(config1)
48 self.registry.register_backend(config2)
49 self.assertEquals('c1.foo', self.registry.lookup_api_method('method1'))
50 self.assertEquals('c2.bar', self.registry.lookup_api_method('method2'))
51 self.assertItemsEqual([config1, config2], self.registry.all_api_configs())
52
53 def testNoneApiConfigContent(self):
54 self.registry.register_backend(None)
55 self.assertIsNone(self.registry.lookup_api_method('method'))
56
57 def testUnparseableApiConfigContent(self):
58 config = '{"methods": {"method": {"rosyMethod": "foo"' # Unclosed {s
59 self.assertRaises(ValueError, self.registry.register_backend, config)
60 self.assertIsNone(self.registry.lookup_api_method('method'))
61
62 def testEmptyApiConfig(self):
63 config = '{}'
64 self.registry.register_backend(config)
65 self.assertIsNone(self.registry.lookup_api_method('method'))
66
67 def testApiConfigContentWithNoMethods(self):
68 config = '{"methods": {}}'
69 self.registry.register_backend(config)
70 self.assertIsNone(self.registry.lookup_api_method('method'))
71
72 def testApiConfigContentWithNoRosyMethod(self):
73 config = '{"methods": {"method": {}}}'
74 self.registry.register_backend(config)
75 self.assertIsNone(self.registry.lookup_api_method('method'))
76
77 def testRegisterSpiRootRepeatedError(self):
78 config1 = '{"methods": {"method1": {"rosyMethod": "MyClass.Func1"}}}'
79 config2 = '{"methods": {"method2": {"rosyMethod": "MyClass.Func2"}}}'
80 self.registry.register_backend(config1)
81 self.assertRaises(api_exceptions.ApiConfigurationError,
82 self.registry.register_backend, config2)
83 self.assertEquals('MyClass.Func1',
84 self.registry.lookup_api_method('method1'))
85 self.assertIsNone(self.registry.lookup_api_method('method2'))
86 self.assertEqual([config1], self.registry.all_api_configs())
87
88 def testRegisterSpiDifferentClasses(self):
89 """This can happen when multiple classes implement an API."""
90 config1 = ('{"methods": {'
91 ' "method1": {"rosyMethod": "MyClass.Func1"},'
92 ' "method2": {"rosyMethod": "OtherClass.Func2"}}}')
93 self.registry.register_backend(config1)
94 self.assertEquals('MyClass.Func1',
95 self.registry.lookup_api_method('method1'))
96 self.assertEquals('OtherClass.Func2',
97 self.registry.lookup_api_method('method2'))
98 self.assertEqual([config1], self.registry.all_api_configs())
99
100
101 class BackedServiceImplTest(unittest.TestCase):
102
103 def setUp(self):
104 self.service = api_backend_service.BackendServiceImpl(
105 api_backend_service.ApiConfigRegistry(), '1')
106 self.mox = mox.Mox()
107
108 def tearDown(self):
109 self.mox.UnsetStubs()
110
111 def testGetApiConfigsWithEmptyRequest(self):
112 request = api_backend.GetApiConfigsRequest()
113 self.assertEqual([], self.service.getApiConfigs(request).items)
114
115 def testGetApiConfigsWithCorrectRevision(self):
116 # TODO: there currently exists a bug in protorpc where non-unicode strings
117 # aren't validated correctly and so their values aren't set correctly.
118 # Remove 'u' this once that's fixed. This shouldn't affect production.
119 request = api_backend.GetApiConfigsRequest(appRevision=u'1')
120 self.assertEqual([], self.service.getApiConfigs(request).items)
121
122 def testGetApiConfigsWithIncorrectRevision(self):
123 # TODO: there currently exists a bug in protorpc where non-unicode strings
124 # aren't validated correctly and so their values aren't set correctly.
125 # Remove 'u' this once that's fixed. This shouldn't affect production.
126 request = api_backend.GetApiConfigsRequest(appRevision=u'2')
127 self.assertRaises(
128 api_exceptions.BadRequestException, self.service.getApiConfigs, request)
129
130 # pylint: disable=g-bad-name
131 def verifyLogLevels(self, levels):
132 Level = api_backend.LogMessagesRequest.LogMessage.Level
133 message = 'Test message.'
134 logger_name = api_backend_service.__name__
135
136 log = mox.MockObject(logging.Logger)
137 self.mox.StubOutWithMock(logging, 'getLogger')
138 logging.getLogger(logger_name).AndReturn(log)
139
140 for level in levels:
141 if level is None:
142 level = 'info'
143 record = logging.LogRecord(name=logger_name,
144 level=getattr(logging, level.upper()),
145 pathname='', lineno='', msg=message,
146 args=None, exc_info=None)
147 log.handle(record)
148 self.mox.ReplayAll()
149
150 requestMessages = []
151 for level in levels:
152 if level:
153 requestMessage = api_backend.LogMessagesRequest.LogMessage(
154 level=getattr(Level, level), message=message)
155 else:
156 requestMessage = api_backend.LogMessagesRequest.LogMessage(
157 message=message)
158 requestMessages.append(requestMessage)
159
160 request = api_backend.LogMessagesRequest(messages=requestMessages)
161 self.service.logMessages(request)
162 self.mox.VerifyAll()
163
164 def testLogMessagesUnspecifiedLevel(self):
165 self.verifyLogLevels([None])
166
167 def testLogMessagesDebug(self):
168 self.verifyLogLevels(['debug'])
169
170 def testLogMessagesInfo(self):
171 self.verifyLogLevels(['info'])
172
173 def testLogMessagesWarning(self):
174 self.verifyLogLevels(['warning'])
175
176 def testLogMessagesError(self):
177 self.verifyLogLevels(['error'])
178
179 def testLogMessagesCritical(self):
180 self.verifyLogLevels(['critical'])
181
182 def testLogMessagesAll(self):
183 self.verifyLogLevels([None, 'debug', 'info', 'warning', 'error',
184 'critical'])
185
186 def testLogMessagesRandom(self):
187 self.verifyLogLevels(['info', 'debug', 'info', 'info', 'warning', 'info',
188 'error', 'error', None, 'info', None, None,
189 'critical', 'critical', 'info', 'info', None])
190
191
192 if __name__ == '__main__':
193 unittest.main()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698