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 def test_http_header_content_length_big(self): 149 self.load('empty') 150 151 self.assertEqual(self.post(headers={ 152 'Content-Length': str(2 ** 64), 153 'Connection': 'close', 154 'Host': 'localhost' 155 }, body='X' * 1000)['status'], 400, 'Content-Length big') 156 157 def test_http_header_content_length_negative(self): 158 self.load('empty') 159 160 self.assertEqual(self.post(headers={ 161 'Content-Length': '-100', 162 'Connection': 'close', 163 'Host': 'localhost' 164 }, body='X' * 1000)['status'], 400, 'Content-Length negative') 165 166 def test_http_header_content_length_text(self): 167 self.load('empty') 168 169 self.assertEqual(self.post(headers={ 170 'Content-Length': 'blah', 171 'Connection': 'close', 172 'Host': 'localhost' 173 }, body='X' * 1000)['status'], 400, 'Content-Length text') 174 175 def test_http_header_content_length_multiple_values(self): 176 self.load('empty') 177 178 self.assertEqual(self.post(headers={ 179 'Content-Length': '41, 42', 180 'Connection': 'close', 181 'Host': 'localhost' 182 }, body='X' * 1000)['status'], 400, 'Content-Length multiple value') 183 184 def test_http_header_content_length_multiple_fields(self): 185 self.load('empty') 186 187 self.assertEqual(self.post(headers={ 188 'Content-Length': ['41', '42'], 189 'Connection': 'close', 190 'Host': 'localhost' 191 }, body='X' * 1000)['status'], 400, 'Content-Length multiple fields') 192 193if __name__ == '__main__': 194 TestUnitHTTPHeader.main() 195