| 1 | # GNU MediaGoblin -- federated, autonomous media hosting |
| 2 | # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. |
| 3 | # |
| 4 | # This program is free software: you can redistribute it and/or modify |
| 5 | # it under the terms of the GNU Affero General Public License as published by |
| 6 | # the Free Software Foundation, either version 3 of the License, or |
| 7 | # (at your option) any later version. |
| 8 | # |
| 9 | # This program is distributed in the hope that it will be useful, |
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | # GNU Affero General Public License for more details. |
| 13 | # |
| 14 | # You should have received a copy of the GNU Affero General Public License |
| 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | |
| 17 | from __future__ import absolute_import, unicode_literals |
| 18 | |
| 19 | from werkzeug.wrappers import Request |
| 20 | from werkzeug.test import EnvironBuilder |
| 21 | |
| 22 | from mediagoblin.tools.request import decode_request |
| 23 | |
| 24 | class TestDecodeRequest(object): |
| 25 | """Test the decode_request function.""" |
| 26 | |
| 27 | def test_form_type(self): |
| 28 | """Try a normal form-urlencoded request.""" |
| 29 | builder = EnvironBuilder(method='POST', data={'foo': 'bar'}) |
| 30 | request = Request(builder.get_environ()) |
| 31 | data = decode_request(request) |
| 32 | assert data['foo'] == 'bar' |
| 33 | |
| 34 | def test_json_type(self): |
| 35 | """Try a normal JSON request.""" |
| 36 | builder = EnvironBuilder( |
| 37 | method='POST', content_type='application/json', |
| 38 | data='{"foo": "bar"}') |
| 39 | request = Request(builder.get_environ()) |
| 40 | data = decode_request(request) |
| 41 | assert data['foo'] == 'bar' |
| 42 | |
| 43 | def test_content_type_with_options(self): |
| 44 | """Content-Type can also have options.""" |
| 45 | builder = EnvironBuilder( |
| 46 | method='POST', |
| 47 | content_type='application/x-www-form-urlencoded; charset=utf-8') |
| 48 | request = Request(builder.get_environ()) |
| 49 | # Must populate form field manually with non-default content-type. |
| 50 | request.form = {'foo': 'bar'} |
| 51 | data = decode_request(request) |
| 52 | assert data['foo'] == 'bar' |
| 53 | |
| 54 | def test_form_type_is_default(self): |
| 55 | """Assume form-urlencoded if blank in the request.""" |
| 56 | builder = EnvironBuilder(method='POST', content_type='') |
| 57 | request = Request(builder.get_environ()) |
| 58 | # Must populate form field manually with non-default content-type. |
| 59 | request.form = {'foo': 'bar'} |
| 60 | data = decode_request(request) |
| 61 | assert data['foo'] == 'bar' |