Пример #1
0
 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
     )
Пример #2
0
 def test_subtypes(self):
     self.assertEquals(
         MediaType('application/json').subtype,
         'json'
     )
     self.assertEquals(
         MediaType('application/*').subtype,
         '*'
     )
Пример #3
0
 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"})]
     )
Пример #4
0
 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('*/*')
         ]
     )
Пример #5
0
 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)
         ]
     )
Пример #6
0
 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'
     )
Пример #7
0
 def test_media_types(self):
     self.assertEquals(
         MediaType('application/json').mediatype,
         'application'
     )
     self.assertEquals(
         MediaType('application/*').mediatype,
         'application'
     )
     self.assertEquals(
         MediaType('*/*').mediatype,
         '*'
     )
Пример #8
0
 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)
Пример #9
0
 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"})]
     )
Пример #10
0
 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('*/*')
     )
Пример #11
0
 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/*')
     )
Пример #12
0
 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)
         ]
     )
Пример #13
0
 def test_all_types(self):
     self.assertFalse(MediaType('application/json').all_types)
     self.assertFalse(MediaType('application/*').all_types)
     self.assertTrue(MediaType('*/*').all_types)
Пример #14
0
 def test_non_valid_q_value(self):
     self.assertEquals(
         parse('application/*; q=_0-2'),
         [MediaType('application/*', 1.0)]
     )
Пример #15
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'))
Пример #16
0
 def test_accept_header_still_included(self):
     m = [MediaType('application/json')]
     self.assertEquals(m, parse('Accept: application/json'))
Пример #17
0
 def test_parse_simple_header(self):
     m = [MediaType('application/json')]
     self.assertEquals(m, parse('application/json'))
Пример #18
0
 def test_getitem_param_none(self):
     m = MediaType('application/json')
     self.assertIsNone(m['test'])
Пример #19
0
 def test_getitem_param_exists(self):
     m = MediaType('application/json', params={'test': '2'})
     self.assertEqual(m['test'], '2')
Пример #20
0
 def test_invalid_subtypes(self):
     with self.assertRaises(SubtypeValueError):
         MediaType('application/')
     with self.assertRaises(SubtypeValueError):
         MediaType('application')
Пример #21
0
 def test_invalid_media_types(self):
     with self.assertRaises(MediaTypeValueError):
         MediaType('/json')
     with self.assertRaises(MediaTypeValueError):
         MediaType('/')
Пример #22
0
 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'))