Handle OpenAPI models correctly for GET requests (#4274)
* Handle OpenAPI models correctly for GET requests * Update basehandlers_test.py
This commit is contained in:
Родитель
51b4926879
Коммит
10ff0673d1
|
@ -197,7 +197,9 @@ class APIHandler(BaseHandler):
|
|||
"""Handle an incoming HTTP GET request."""
|
||||
headers = self.get_headers()
|
||||
handler_data = self.do_get(*args, **kwargs)
|
||||
if isinstance(handler_data, Model):
|
||||
# OpenAPI models have a to_dict attribute that should be used for
|
||||
# converting to JSON.
|
||||
if hasattr(handler_data, 'to_dict'):
|
||||
handler_data = handler_data.to_dict()
|
||||
return self.defensive_jsonify(handler_data), headers
|
||||
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from gen.py.chromestatus_openapi.chromestatus_openapi.models.feature_links_response import FeatureLinksResponse
|
||||
import testing_config # Must be imported before the module under test.
|
||||
|
||||
import json
|
||||
|
@ -441,6 +442,29 @@ class APIHandlerTests(testing_config.CustomTestCase):
|
|||
self.handler = TestableAPIHandler()
|
||||
self.check_http_method_handler(self.handler.get, 'done get')
|
||||
|
||||
@mock.patch('framework.basehandlers.APIHandler.do_get')
|
||||
def test_get__dict(self, mock_do_get):
|
||||
"""get() should return a JSON response if the do_get() return value is a
|
||||
dict."""
|
||||
mock_do_get.return_value = {'key': 'value'}
|
||||
with test_app.test_request_context('/path'):
|
||||
response, _ = self.handler.get()
|
||||
self.assertEqual(basehandlers.XSSI_PREFIX + '{"key": "value"}',
|
||||
response.get_data().decode('utf-8'))
|
||||
|
||||
@mock.patch('framework.basehandlers.APIHandler.do_get')
|
||||
def test_get__openapi_model(self, mock_do_get):
|
||||
"""get() should return a JSON response if the do_get() return value is an
|
||||
OpenAPI model."""
|
||||
mock_do_get.return_value = FeatureLinksResponse(data='data',
|
||||
has_stale_links=True)
|
||||
with test_app.test_request_context('/path'):
|
||||
response, _ = self.handler.get()
|
||||
self.assertEqual(basehandlers.XSSI_PREFIX +
|
||||
'{"data": "data", "has_stale_links": true}',
|
||||
response.get_data().decode('utf-8'))
|
||||
|
||||
|
||||
def test_post(self):
|
||||
"""If a subclass has do_post(), post() should return a JSON response."""
|
||||
self.handler = TestableAPIHandler()
|
||||
|
|
Загрузка…
Ссылка в новой задаче