|
| 1 | +# Copyright 2015 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 | +import unittest |
| 16 | + |
| 17 | +from mock import patch |
| 18 | +from werkzeug.test import Client |
| 19 | +from werkzeug.wrappers import Request |
| 20 | +from werkzeug.wrappers import Response |
| 21 | + |
| 22 | +from wsgi_config import app_for_script |
| 23 | + |
| 24 | + |
| 25 | +@Request.application |
| 26 | +def salutation_world(request): |
| 27 | + salutation = request.args.get('salutation', 'Hello') |
| 28 | + return Response('%s World!' % salutation) |
| 29 | + |
| 30 | + |
| 31 | +def goodbye_world_middleware(app): |
| 32 | + def goodbye_wrapper(wsgi_env, start_response): |
| 33 | + wsgi_env['QUERY_STRING'] = 'salutation=Goodbye' |
| 34 | + return app(wsgi_env, start_response) |
| 35 | + return goodbye_wrapper |
| 36 | + |
| 37 | + |
| 38 | +class AppConfigTestCase(unittest.TestCase): |
| 39 | + |
| 40 | + def test_app_for_script(self): |
| 41 | + with patch('wsgi_config.get_add_middleware_from_appengine_config', |
| 42 | + return_value=None): |
| 43 | + app = app_for_script('wsgi_config_test.salutation_world') |
| 44 | + client = Client(app, Response) |
| 45 | + response = client.get('/?salutation=Hello') |
| 46 | + self.assertEqual(response.status_code, 200) |
| 47 | + self.assertEqual(response.data, 'Hello World!') |
| 48 | + |
| 49 | + def test_app_for_script_with_middleware(self): |
| 50 | + with patch('wsgi_config.get_add_middleware_from_appengine_config', |
| 51 | + return_value=goodbye_world_middleware): |
| 52 | + app = app_for_script('wsgi_config_test.salutation_world') |
| 53 | + client = Client(app, Response) |
| 54 | + response = client.get('/?salutation=Hello') |
| 55 | + self.assertEqual(response.status_code, 200) |
| 56 | + self.assertEqual(response.data, 'Goodbye World!') |
0 commit comments