1import unittest 2import unit 3 4class TestUnitHTTPHeader(unit.TestUnitApplicationPython): 5 6 def setUpClass(): 7 unit.TestUnit().check_modules('python') 8 9 def test_http_header_value_leading_sp(self): 10 self.load('custom_header') 11 12 resp = self.get(headers={ 13 'Custom-Header': ' ,' 14 }) 15 16 self.assertEqual(resp['status'], 200, 'value leading sp status') 17 self.assertEqual(resp['headers']['Custom-Header'], ',', 18 'value leading sp custom header') 19 20 def test_http_header_value_leading_htab(self): 21 self.load('custom_header') 22 23 resp = self.get(headers={ 24 'Custom-Header': '\t,' 25 }) 26 27 self.assertEqual(resp['status'], 200, 'value leading htab status') 28 self.assertEqual(resp['headers']['Custom-Header'], ',', 29 'value leading htab custom header') 30 31 def test_http_header_value_trailing_sp(self): 32 self.load('custom_header') 33 34 resp = self.get(headers={ 35 'Custom-Header': ', ' 36 }) 37 38 self.assertEqual(resp['status'], 200, 'value trailing sp status') 39 self.assertEqual(resp['headers']['Custom-Header'], ',', 40 'value trailing sp custom header') 41 42 def test_http_header_value_trailing_htab(self): 43 self.load('custom_header') 44 45 resp = self.get(headers={ 46 'Custom-Header': ',\t' 47 }) 48 49 self.assertEqual(resp['status'], 200, 'value trailing htab status') 50 self.assertEqual(resp['headers']['Custom-Header'], ',', 51 'value trailing htab custom header') 52 53 def test_http_header_value_both_sp(self): 54 self.load('custom_header') 55 56 resp = self.get(headers={ 57 'Custom-Header': ' , ' 58 }) 59 60 self.assertEqual(resp['status'], 200, 'value both sp status') 61 self.assertEqual(resp['headers']['Custom-Header'], ',', 62 'value both sp custom header') 63 64 def test_http_header_value_both_htab(self): 65 self.load('custom_header') 66 67 resp = self.get(headers={ 68 'Custom-Header': '\t,\t' 69 }) 70 71 self.assertEqual(resp['status'], 200, 'value both htab status') 72 self.assertEqual(resp['headers']['Custom-Header'], ',', 73 'value both htab custom header') 74 75 def test_http_header_value_chars(self): 76 self.load('custom_header') 77 78 resp = self.get(headers={ 79 'Custom-Header': '(),/:;<=>?@[\]{}\t !#$%&\'*+-.^_`|~' 80 }) 81 82 self.assertEqual(resp['status'], 200, 'value chars status') 83 self.assertEqual(resp['headers']['Custom-Header'], 84 '(),/:;<=>?@[\]{}\t !#$%&\'*+-.^_`|~', 'value chars custom header') 85 86 def test_http_header_value_chars_edge(self): 87 self.load('custom_header') 88 89 resp = self.http(b"""GET / HTTP/1.1 90Host: localhost 91Custom-Header: \x20\xFF 92Connection: close 93 94""", raw=True, encoding='latin1') 95 96 self.assertEqual(resp['status'], 200, 'value chars edge status') 97 self.assertEqual(resp['headers']['Custom-Header'], '\xFF', 98 'value chars edge') 99 100 def test_http_header_value_chars_below(self): 101 self.load('custom_header') 102 103 resp = self.http(b"""GET / HTTP/1.1 104Host: localhost 105Custom-Header: \x1F 106Connection: close 107 108""", raw=True) 109 110 self.assertEqual(resp['status'], 400, 'value chars below') 111 112 def test_http_header_field_leading_sp(self): 113 self.load('empty') 114 115 resp = self.get(headers={ 116 ' Custom-Header': 'blah' 117 }) 118 119 self.assertEqual(resp['status'], 400, 'field leading sp') 120 121 def test_http_header_field_leading_htab(self): 122 self.load('empty') 123 124 resp = self.get(headers={ 125 '\tCustom-Header': 'blah' 126 }) 127 128 self.assertEqual(resp['status'], 400, 'field leading htab') 129 130 def test_http_header_field_trailing_sp(self): 131 self.load('empty') 132 133 resp = self.get(headers={ 134 'Custom-Header ': 'blah' 135 }) 136 137 self.assertEqual(resp['status'], 400, 'field trailing sp') 138 139 def test_http_header_field_trailing_htab(self): 140 self.load('empty') 141 142 resp = self.get(headers={ 143 'Custom-Header\t': 'blah' 144 }) 145 146 self.assertEqual(resp['status'], 400, 'field trailing htab') 147 148 @unittest.expectedFailure 149 def test_http_header_transfer_encoding_chunked(self): 150 self.load('empty') 151 152 resp = self.http(b"""GET / HTTP/1.1 153Host: localhost 154Transfer-Encoding: chunked 155Connection: close 156 157a 1580123456789 1590 160 161""", raw=True) 162 163 self.assertEqual(resp['status'], 200, 'transfer encoding chunked') 164 165if __name__ == '__main__': 166 unittest.main() 167