Mercurial > p > roundup > code
comparison test/rest_common.py @ 7156:6f09103a6522
[issue2551263] expose headers to rest clients
Expose headers for with rate limiting (X-RateLimiting*, Retry-After),
marking obsolete api endpoints (Sunset), and listing methods available
on an endpoint (Allow).
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Thu, 23 Feb 2023 15:34:44 -0500 |
| parents | 89a59e46b3af |
| children | 2de72f75f2f8 |
comparison
equal
deleted
inserted
replaced
| 7155:89a59e46b3af | 7156:6f09103a6522 |
|---|---|
| 3466 results = results['data'] | 3466 results = results['data'] |
| 3467 self.assertEqual(self.dummy_client.response_code, 200) | 3467 self.assertEqual(self.dummy_client.response_code, 200) |
| 3468 self.assertEqual(len(results['attributes']['nosy']), 0) | 3468 self.assertEqual(len(results['attributes']['nosy']), 0) |
| 3469 self.assertListEqual(results['attributes']['nosy'], []) | 3469 self.assertListEqual(results['attributes']['nosy'], []) |
| 3470 | 3470 |
| 3471 def testRestExposeHeaders(self): | |
| 3472 | |
| 3473 local_client = self.server.client | |
| 3474 body = b'{ "data": "Joe Doe 1" }' | |
| 3475 env = { "CONTENT_TYPE": "application/json", | |
| 3476 "CONTENT_LENGTH": len(body), | |
| 3477 "REQUEST_METHOD": "PUT", | |
| 3478 "HTTP_ORIGIN": "http://tracker.example" | |
| 3479 } | |
| 3480 local_client.env.update(env) | |
| 3481 | |
| 3482 local_client.db.config["WEB_ALLOWED_API_ORIGINS"] = " * " | |
| 3483 | |
| 3484 headers={"accept": "application/json; version=1", | |
| 3485 "content-type": env['CONTENT_TYPE'], | |
| 3486 "content-length": env['CONTENT_LENGTH'], | |
| 3487 "origin": env['HTTP_ORIGIN'] | |
| 3488 } | |
| 3489 self.headers=headers | |
| 3490 # we need to generate a FieldStorage the looks like | |
| 3491 # FieldStorage(None, None, 'string') rather than | |
| 3492 # FieldStorage(None, None, []) | |
| 3493 body_file=BytesIO(body) # FieldStorage needs a file | |
| 3494 form = client.BinaryFieldStorage(body_file, | |
| 3495 headers=headers, | |
| 3496 environ=env) | |
| 3497 local_client.request.headers.get=self.get_header | |
| 3498 results = self.server.dispatch('PUT', | |
| 3499 "/rest/data/user/%s/realname"%self.joeid, | |
| 3500 form) | |
| 3501 | |
| 3502 for header in [ "X-RateLimit-Limit", | |
| 3503 "X-RateLimit-Remaining", | |
| 3504 "X-RateLimit-Reset", | |
| 3505 "X-RateLimit-Limit-Period", | |
| 3506 "Retry-After", | |
| 3507 "Sunset", | |
| 3508 "Allow", | |
| 3509 ]: | |
| 3510 self.assertIn( | |
| 3511 header, | |
| 3512 self.server.client.additional_headers[ | |
| 3513 "Access-Control-Expose-Headers"]) | |
| 3471 | 3514 |
| 3472 def testRestMatchWildcardOrigin(self): | 3515 def testRestMatchWildcardOrigin(self): |
| 3473 # cribbed from testDispatch #1 | 3516 # cribbed from testDispatch #1 |
| 3474 # PUT: joe's 'realname' using json data. | 3517 # PUT: joe's 'realname' using json data. |
| 3475 # simulate: /rest/data/user/<id>/realname | 3518 # simulate: /rest/data/user/<id>/realname |
