From 7be397689095249af7b65dca6636cebad0ab7a0a Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 12 Aug 2014 09:04:43 +0200 Subject: [PATCH] tests: Add tests for tests.utils.http --- qutebrowser/test/utils/http/test_http.py | 109 +++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 qutebrowser/test/utils/http/test_http.py diff --git a/qutebrowser/test/utils/http/test_http.py b/qutebrowser/test/utils/http/test_http.py new file mode 100644 index 000000000..ebfe3ad9e --- /dev/null +++ b/qutebrowser/test/utils/http/test_http.py @@ -0,0 +1,109 @@ +# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: + +# Copyright 2014 Florian Bruhin (The Compiler) +# +# This file is part of qutebrowser. +# +# qutebrowser is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# qutebrowser is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with qutebrowser. If not, see . + +"""Tests for qutebrowser.utils.http. + +Note that tests for parse_content_disposition are in their own +test_content_disposition.py file. +""" + +import unittest + +import qutebrowser.utils.http as httputils +from qutebrowser.test.stubs import FakeNetworkReply + + +class ParseContentTypeTests(unittest.TestCase): + + """Test for parse_content_type.""" + + # pylint: disable=unpacking-non-sequence + + def test_not_existing(self): + """Test without any Content-Type header.""" + reply = FakeNetworkReply() + self.assertIsNone(httputils.parse_content_type(reply)) + + def test_mimetype(self): + """Test with simple Content-Type header.""" + reply = FakeNetworkReply(headers={'Content-Type': 'image/example'}) + mimetype, rest = httputils.parse_content_type(reply) + self.assertEqual(mimetype, 'image/example') + self.assertIsNone(rest) + + def test_empty(self): + """Test with empty Content-Type header.""" + reply = FakeNetworkReply(headers={'Content-Type': ''}) + mimetype, rest = httputils.parse_content_type(reply) + self.assertEqual(mimetype, '') + self.assertIsNone(rest) + + def test_additional(self): + """Test with Content-Type header with additional informations.""" + reply = FakeNetworkReply( + headers={'Content-Type': 'image/example; encoding=UTF-8'}) + mimetype, rest = httputils.parse_content_type(reply) + self.assertEqual(mimetype, 'image/example') + self.assertEqual(rest, ' encoding=UTF-8') + + +class ChangeContentTypeTests(unittest.TestCase): + + """Test for change_content_type.""" + + def test_not_existing(self): + """Test without any Content-Type header.""" + reply = FakeNetworkReply() + httputils.change_content_type(reply, {'image/example': 'image/jpeg'}) + + def test_mimetype(self): + """Test with simple Content-Type header.""" + reply = FakeNetworkReply(headers={'Content-Type': 'image/example'}) + httputils.change_content_type(reply, {'image/example': 'image/jpeg'}) + self.assertEqual(reply.headers['Content-Type'], 'image/jpeg') + + def test_empty(self): + """Test with empty Content-Type header.""" + reply = FakeNetworkReply(headers={'Content-Type': ''}) + httputils.change_content_type(reply, {'image/example': 'image/jpeg'}) + self.assertEqual(reply.headers['Content-Type'], '') + + def test_additional(self): + """Test with Content-Type header with additional informations.""" + reply = FakeNetworkReply( + headers={'Content-Type': 'image/example; encoding=UTF-8'}) + httputils.change_content_type(reply, {'image/example': 'image/jpeg'}) + self.assertEqual(reply.headers['Content-Type'], + 'image/jpeg; encoding=UTF-8') + + def test_wrong_mapping(self): + """Test with a mapping which doesn't match the header.""" + reply = FakeNetworkReply(headers={'Content-Type': 'image/example'}) + httputils.change_content_type(reply, {'image/foo': 'image/bar'}) + self.assertEqual(reply.headers['Content-Type'], 'image/example') + + def test_empty_mapping(self): + """Test with a mapping which is empty.""" + reply = FakeNetworkReply(headers={'Content-Type': 'image/example'}) + httputils.change_content_type(reply, {}) + self.assertEqual(reply.headers['Content-Type'], 'image/example') + + +if __name__ == '__main__': + unittest.main()