def test_unicode(self): # test adding unicode values in all the different ways h = HttpHeaders([('a', 'แด')]) self.assertEqual('แด', h.get('a')) h.set('b', '๐ฆ') self.assertEqual('๐ฆ', h.get('b')) h.add('c', '๐๐๐') self.assertEqual('๐๐๐', h.get('c')) h.add_pairs([('d', 'โคลฃแธโปโฝ')]) self.assertEqual('โคลฃแธโปโฝ', h.get('d'))
def _validate_successful_get_response(self, put_object): self.assertEqual(self.response_status_code, 200, "status code is not 200") headers = HttpHeaders(self.response_headers) self.assertIsNone(headers.get("Content-Range")) body_length = headers.get("Content-Length") if not put_object: self.assertIsNotNone(body_length, "Content-Length is missing from headers") if body_length: self.assertEqual( int(body_length), self.received_body_len, "Received body length does not match the Content-Length header" )
def test_remove(self): h = HttpHeaders() self.assertRaises(KeyError, h.remove, 'Non-Existent') h.add('Host', 'example.org') h.remove('Host') self.assertIsNone(h.get('Host'))
def test_get_is_case_insensitive(self): h = HttpHeaders() h.set('Cookie', 'a=1') h.add_pairs([('cookie', 'b=2'), ('COOKIE', 'c=3')]) h.add(u'CoOkIe', 'd=4') # note: unicode self.assertEqual('a=1', h.get(u'COOKIE')) self.assertEqual(['a=1', 'b=2', 'c=3', 'd=4'], list(h.get_values('Cookie')))
def test_add_pairs(self): h = HttpHeaders() h.add_pairs([ ('Host', 'example.org'), ('Cookie', 'a=1'), ('Cookie', 'b=2'), ]) self.assertEqual('example.org', h.get('Host')) self.assertEqual(['a=1', 'b=2'], list(h.get_values('Cookie')))
def test_remove_value(self): h = HttpHeaders() self.assertRaises(ValueError, h.remove_value, 'Non-Existent', 'Nope') # header with 1 value h.add('Host', 'example.org') self.assertRaises(ValueError, h.remove_value, 'Host', 'wrong-value') h.remove_value('Host', 'example.org') self.assertIsNone(h.get('Host')) # pluck out a duplicate value [1,2,2] -> [1,2] h.add_pairs([('Dupes', '1'), ('DUPES', '2'), ('dupes', '2')]) h.remove_value('Dupes', '2') self.assertEqual(['1', '2'], list(h.get_values('Dupes')))
def test_get_none(self): h = HttpHeaders() self.assertIsNone(h.get('Non-Existent')) self.assertEqual('Banana', h.get('Non-Existent', 'Banana')) self.assertEqual([], list(h.get_values('Non-Existent')))
def test_add_multi_values(self): h = HttpHeaders() h.add('Cookie', 'a=1') h.add('Cookie', 'b=2') self.assertEqual('a=1', h.get('Cookie')) self.assertEqual(['a=1', 'b=2'], list(h.get_values('Cookie')))
def test_add(self): h = HttpHeaders() h.add('Host', 'example.org') self.assertEqual('example.org', h.get('Host')) self.assertEqual(['example.org'], list(h.get_values('Host')))