def test_prefer_most_specific_type(self): m = [ MediaType('application/json'), MediaType('application/*', 0.2), ] self.assertEquals( parse('application/*; q=0.2, application/json'), m )
def test_subtypes(self): self.assertEquals( MediaType('application/json').subtype, 'json' ) self.assertEquals( MediaType('application/*').subtype, '*' )
def test_special_characters(self): self.assertEquals( parse('application/*; test=_0-2'), [MediaType('application/*', params={"test": "_0-2"})] ) self.assertEquals( parse("application/*; test=_0-2'"), [MediaType('application/*', params={"test": "_0-2"})] )
def test_elaborate_accept_header(self): self.assertEquals( parse('text/*, text/html, text/html;level=1, */*'), [ MediaType('text/html', params={'level': '1'}), MediaType('text/html'), MediaType('text/*'), MediaType('*/*') ] )
def test_real_world_header(self): m = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' self.assertEquals( parse(m), [ MediaType('text/html'), MediaType('application/xhtml+xml'), MediaType('application/xml', q=0.9), MediaType('*/*', q=0.8) ] )
def test_unequal_media_types(self): self.assertNotEquals( MediaType('application/json'), MediaType('text/plain') ) self.assertNotEquals( MediaType('application/json', params={'test': '2'}), MediaType('text/plain', params={'test': '2'}) ) self.assertNotEquals( MediaType('application/json'), 'text/plain' )
def test_media_types(self): self.assertEquals( MediaType('application/json').mediatype, 'application' ) self.assertEquals( MediaType('application/*').mediatype, 'application' ) self.assertEquals( MediaType('*/*').mediatype, '*' )
def test_process_request_correct_accepted_types(self): request = Mock( META={ 'HTTP_ACCEPT': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' }) expected_types = [ MediaType('text/html'), MediaType('application/xhtml+xml'), MediaType('application/xml', 0.9), MediaType('*/*', 0.8) ] self.am.process_request(request) self.assertListEqual(request.accepted_types, expected_types)
def test_media_type_parameter_with_quotes(self): self.assertEquals( parse('application/*; q="0.2"'), [MediaType('application/*', 0.2)] ) self.assertEquals( parse("application/*; q='0.2'"), [MediaType('application/*', 0.2)] ) self.assertEquals( parse('application/*; q=0.2; test="moop"'), [MediaType('application/*', 0.2, {"test": "moop"})] ) self.assertEquals( parse("application/*; q=0.2; test='moop'"), [MediaType('application/*', 0.2, {"test": "moop"})] )
def test_more_specific(self): self.assertLess( MediaType('application/*'), MediaType('text/plain', q=0.8) ) self.assertLess( MediaType('application/json', params={'test': '2'}), MediaType('application/*') ) self.assertLess( MediaType('application/json', q=0.5, params={'test': '2'}), MediaType('application/json', q=0.5) ) self.assertLess( MediaType('application/json'), MediaType('application/*') ) self.assertLess( MediaType('application/*'), MediaType('*/*') )
def test_less_specific(self): self.assertGreater( MediaType('text/plain', q=0.8), MediaType('application/*') ) self.assertGreater( MediaType('application/*'), MediaType('application/json', params={'test': '2'}) ) self.assertGreater( MediaType('application/json', q=0.5), MediaType('application/json', q=0.5, params={'test': '2'}) ) self.assertGreater( MediaType('application/*'), MediaType('application/json') ) self.assertGreater( MediaType('*/*'), MediaType('application/*') )
def test_parse_broken_accept_header(self): header = ('text/xml,application/xml,application/xhtml+xml,') +\ ('text/html;q=0.9,text/plain;q=0.8,image/*,,*/*;q=0.5') self.assertEquals( parse(header), [ MediaType('text/xml'), MediaType('application/xml'), MediaType('application/xhtml+xml'), MediaType('image/*'), MediaType('text/html', q=0.9), MediaType('text/plain', q=0.8), MediaType('*/*', q=0.5) ] )
def test_all_types(self): self.assertFalse(MediaType('application/json').all_types) self.assertFalse(MediaType('application/*').all_types) self.assertTrue(MediaType('*/*').all_types)
def test_non_valid_q_value(self): self.assertEquals( parse('application/*; q=_0-2'), [MediaType('application/*', 1.0)] )
def test_matches_mediatypes_specific(self): ma = MediaType('text/*') self.assertFalse(ma.matches('application/*')) self.assertFalse(ma.matches('application/json')) self.assertTrue(ma.matches('text/*')) self.assertTrue(ma.matches('text/plain'))
def test_accept_header_still_included(self): m = [MediaType('application/json')] self.assertEquals(m, parse('Accept: application/json'))
def test_parse_simple_header(self): m = [MediaType('application/json')] self.assertEquals(m, parse('application/json'))
def test_getitem_param_none(self): m = MediaType('application/json') self.assertIsNone(m['test'])
def test_getitem_param_exists(self): m = MediaType('application/json', params={'test': '2'}) self.assertEqual(m['test'], '2')
def test_invalid_subtypes(self): with self.assertRaises(SubtypeValueError): MediaType('application/') with self.assertRaises(SubtypeValueError): MediaType('application')
def test_invalid_media_types(self): with self.assertRaises(MediaTypeValueError): MediaType('/json') with self.assertRaises(MediaTypeValueError): MediaType('/')
def test_matches_subtypes(self): ma = MediaType('image/png') self.assertFalse(ma.matches('application/json')) self.assertFalse(ma.matches('image/*')) self.assertFalse(ma.matches('image/jpeg')) self.assertTrue(ma.matches('image/png'))