1 # Copyright (c) 2008, 2012 testtools developers. See LICENSE for details.
3 from testtools import TestCase
4 from testtools.matchers import Equals, MatchesException, Raises
5 from testtools.content_type import (
12 class TestContentType(TestCase):
14 def test___init___None_errors(self):
15 raises_value_error = Raises(MatchesException(ValueError))
16 self.assertThat(lambda:ContentType(None, None), raises_value_error)
17 self.assertThat(lambda:ContentType(None, "traceback"),
19 self.assertThat(lambda:ContentType("text", None), raises_value_error)
21 def test___init___sets_ivars(self):
22 content_type = ContentType("foo", "bar")
23 self.assertEqual("foo", content_type.type)
24 self.assertEqual("bar", content_type.subtype)
25 self.assertEqual({}, content_type.parameters)
27 def test___init___with_parameters(self):
28 content_type = ContentType("foo", "bar", {"quux": "thing"})
29 self.assertEqual({"quux": "thing"}, content_type.parameters)
31 def test___eq__(self):
32 content_type1 = ContentType("foo", "bar", {"quux": "thing"})
33 content_type2 = ContentType("foo", "bar", {"quux": "thing"})
34 content_type3 = ContentType("foo", "bar", {"quux": "thing2"})
35 self.assertTrue(content_type1.__eq__(content_type2))
36 self.assertFalse(content_type1.__eq__(content_type3))
38 def test_basic_repr(self):
39 content_type = ContentType('text', 'plain')
40 self.assertThat(repr(content_type), Equals('text/plain'))
42 def test_extended_repr(self):
43 content_type = ContentType(
44 'text', 'plain', {'foo': 'bar', 'baz': 'qux'})
46 repr(content_type), Equals('text/plain; baz="qux", foo="bar"'))
49 class TestBuiltinContentTypes(TestCase):
51 def test_plain_text(self):
52 # The UTF8_TEXT content type represents UTF-8 encoded text/plain.
53 self.assertThat(UTF8_TEXT.type, Equals('text'))
54 self.assertThat(UTF8_TEXT.subtype, Equals('plain'))
55 self.assertThat(UTF8_TEXT.parameters, Equals({'charset': 'utf8'}))
57 def test_json_content(self):
58 # The JSON content type represents implictly UTF-8 application/json.
59 self.assertThat(JSON.type, Equals('application'))
60 self.assertThat(JSON.subtype, Equals('json'))
61 self.assertThat(JSON.parameters, Equals({}))
65 from unittest import TestLoader
66 return TestLoader().loadTestsFromName(__name__)