Mercurial > p > roundup > code
comparison test/test_liveserver.py @ 6655:a193653d6fa4
Test more range error cases.
check content-range and content-length where applicable
cases:
invalid if-range etag should return whole file with a 200 exit code
invalid range with invalid etag return whole file 200 exit code
invalid range with valid etag return whole file 200 exit code
invalid range with no etag return 416 unable to satify and check valid
content-range.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Mon, 02 May 2022 15:29:12 -0400 |
| parents | da6c9050a79e |
| children | 9a1f5e496e6c |
comparison
equal
deleted
inserted
replaced
| 6654:5986ddd0d2e7 | 6655:a193653d6fa4 |
|---|---|
| 173 Range: 10- | 173 Range: 10- |
| 174 | 174 |
| 175 Also If-Range only supports strong etags not dates or weak etags. | 175 Also If-Range only supports strong etags not dates or weak etags. |
| 176 | 176 |
| 177 """ | 177 """ |
| 178 # check with Accept-Language header | 178 |
| 179 # get whole file uncompressed. Extract content length and etag | |
| 180 # for future use | |
| 181 f = requests.get(self.url_base() + "/@@file/style.css", | |
| 182 headers = {"Accept-Encoding": "identity"}) | |
| 183 # store etag for condition range testing | |
| 184 etag = f.headers['etag'] | |
| 185 expected_length = f.headers['content-length'] | |
| 186 | |
| 187 # get first 11 bytes unconditionally (0 index really??) | |
| 179 hdrs = {"Range": "bytes=0-10"} | 188 hdrs = {"Range": "bytes=0-10"} |
| 180 f = requests.get(self.url_base() + "/@@file/style.css", headers=hdrs) | 189 f = requests.get(self.url_base() + "/@@file/style.css", headers=hdrs) |
| 181 self.assertEqual(f.status_code, 206) | 190 self.assertEqual(f.status_code, 206) |
| 182 self.assertEqual(f.content, b"/* main pag") | 191 self.assertEqual(f.content, b"/* main pag") |
| 183 | 192 # compression disabled for length < 100, so we can use 11 here |
| 184 etag = f.headers['etag'] | 193 self.assertEqual(f.headers['content-length'], '11') |
| 194 self.assertEqual(f.headers['content-range'], | |
| 195 "bytes 0-10/%s"%expected_length) | |
| 196 | |
| 197 # conditional request 11 bytes since etag matches 206 code | |
| 185 hdrs['If-Range'] = etag | 198 hdrs['If-Range'] = etag |
| 186 f = requests.get(self.url_base() + "/@@file/style.css", headers=hdrs) | 199 f = requests.get(self.url_base() + "/@@file/style.css", headers=hdrs) |
| 187 self.assertEqual(f.status_code, 206) | 200 self.assertEqual(f.status_code, 206) |
| 188 self.assertEqual(f.content, b"/* main pag") | 201 self.assertEqual(f.content, b"/* main pag") |
| 189 | 202 # compression disabled for length < 100, so we can use 11 here |
| 190 etag = f.headers['etag'] | 203 self.assertEqual(f.headers['content-length'], '11') |
| 204 self.assertEqual(f.headers['content-range'], | |
| 205 "bytes 0-10/%s"%expected_length) | |
| 206 | |
| 207 # conditional request returns all bytes as etag isn't correct 200 code | |
| 191 hdrs['If-Range'] = etag[2:] # bad tag | 208 hdrs['If-Range'] = etag[2:] # bad tag |
| 192 f = requests.get(self.url_base() + "/@@file/style.css", headers=hdrs) | 209 f = requests.get(self.url_base() + "/@@file/style.css", headers=hdrs) |
| 193 self.assertEqual(f.status_code, 200) | 210 self.assertEqual(f.status_code, 200) |
| 211 # not checking content length since it could be compressed | |
| 212 self.assertNotIn('content-range', f.headers, 'content-range should not be present') | |
| 213 | |
| 214 # range is too large, but etag is bad also, return whole file 200 code | |
| 215 hdrs['Range'] = "0-99999" # too large | |
| 216 hdrs['If-Range'] = etag[2:] # bad tag | |
| 217 f = requests.get(self.url_base() + "/@@file/style.css", headers=hdrs) | |
| 218 self.assertEqual(f.status_code, 200) | |
| 219 # not checking content length since it could be compressed | |
| 220 self.assertNotIn('content-range', f.headers, 'content-range should not be present') | |
| 221 | |
| 222 # range is too large, but etag is specified so return whole file | |
| 223 # 200 code | |
| 224 hdrs['Range'] = "bytes=0-99999" # too large | |
| 225 hdrs['If-Range'] = etag # any tag works | |
| 226 f = requests.get(self.url_base() + "/@@file/style.css", headers=hdrs) | |
| 227 self.assertEqual(f.status_code, 200) | |
| 228 # not checking content length since it could be compressed | |
| 229 self.assertNotIn('content-range', f.headers, 'content-range should not be present') | |
| 230 | |
| 231 # range too large, not if-range so error code 416 | |
| 232 hdrs['Range'] = "bytes=0-99999" # too large | |
| 233 del(hdrs['If-Range']) | |
| 234 print(hdrs) | |
| 235 f = requests.get(self.url_base() + "/@@file/style.css", headers=hdrs) | |
| 236 self.assertEqual(f.status_code, 416) | |
| 237 self.assertEqual(f.headers['content-range'], | |
| 238 "bytes */%s"%expected_length) | |
| 194 | 239 |
| 195 def test_rest_invalid_method_collection(self): | 240 def test_rest_invalid_method_collection(self): |
| 196 # use basic auth for rest endpoint | 241 # use basic auth for rest endpoint |
| 197 f = requests.put(self.url_base() + '/rest/data/user', | 242 f = requests.put(self.url_base() + '/rest/data/user', |
| 198 auth=('admin', 'sekrit'), | 243 auth=('admin', 'sekrit'), |
