test_request_context.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import unittest
  2. from services.request_context import RequestContextParser
  3. class TestRequestContextParser(unittest.TestCase):
  4. def setUp(self):
  5. self.parser = RequestContextParser()
  6. def test_parse_from_header(self):
  7. headers = {'X-Gateway-Session': 'GWS_abc123'}
  8. context = self.parser.parse(headers)
  9. self.assertTrue(context.has_session())
  10. self.assertEqual(context.gateway_session_id, 'GWS_abc123')
  11. self.assertEqual(context.source, 'header')
  12. def test_parse_from_authorization_bearer(self):
  13. headers = {'Authorization': 'Bearer GWS_xyz789'}
  14. context = self.parser.parse(headers)
  15. self.assertTrue(context.has_session())
  16. self.assertEqual(context.gateway_session_id, 'GWS_xyz789')
  17. self.assertEqual(context.source, 'authorization')
  18. def test_parse_from_cookie(self):
  19. headers = {'Cookie': 'gateway_session_id=GWS_cookie123; other=value'}
  20. context = self.parser.parse(headers)
  21. self.assertTrue(context.has_session())
  22. self.assertEqual(context.gateway_session_id, 'GWS_cookie123')
  23. self.assertEqual(context.source, 'cookie')
  24. def test_parse_case_insensitive_headers(self):
  25. # Test with different case
  26. headers = {'x-gateway-session': 'GWS_test', 'AUTHORIZATION': 'Bearer GWS_test2'}
  27. context = self.parser.parse(headers)
  28. # Should prioritize header over authorization
  29. self.assertEqual(context.gateway_session_id, 'GWS_test')
  30. self.assertEqual(context.source, 'header')
  31. def test_parse_priority_header_over_authorization(self):
  32. headers = {
  33. 'X-Gateway-Session': 'GWS_from_header',
  34. 'Authorization': 'Bearer GWS_from_auth'
  35. }
  36. context = self.parser.parse(headers)
  37. self.assertEqual(context.gateway_session_id, 'GWS_from_header')
  38. self.assertEqual(context.source, 'header')
  39. def test_parse_priority_authorization_over_cookie(self):
  40. headers = {
  41. 'Authorization': 'Bearer GWS_from_auth',
  42. 'Cookie': 'gateway_session_id=GWS_from_cookie'
  43. }
  44. context = self.parser.parse(headers)
  45. self.assertEqual(context.gateway_session_id, 'GWS_from_auth')
  46. self.assertEqual(context.source, 'authorization')
  47. def test_parse_no_session(self):
  48. headers = {'Content-Type': 'application/json'}
  49. context = self.parser.parse(headers)
  50. self.assertFalse(context.has_session())
  51. self.assertEqual(context.gateway_session_id, '')
  52. self.assertEqual(context.source, '')
  53. def test_parse_invalid_session_id_format(self):
  54. # Session ID doesn't start with GWS_
  55. headers = {'X-Gateway-Session': 'INVALID_123'}
  56. context = self.parser.parse(headers)
  57. self.assertFalse(context.has_session())
  58. def test_parse_authorization_without_bearer_prefix(self):
  59. headers = {'Authorization': 'Basic xyz123'}
  60. context = self.parser.parse(headers)
  61. self.assertFalse(context.has_session())
  62. def test_parse_authorization_with_non_gws_token(self):
  63. headers = {'Authorization': 'Bearer MT_regular_token'}
  64. context = self.parser.parse(headers)
  65. self.assertFalse(context.has_session())
  66. def test_parse_empty_headers(self):
  67. context = self.parser.parse({})
  68. self.assertFalse(context.has_session())
  69. def test_parse_none_headers(self):
  70. context = self.parser.parse(None)
  71. self.assertFalse(context.has_session())
  72. def test_parse_cookie_with_multiple_values(self):
  73. headers = {'Cookie': 'session=abc; gateway_session_id=GWS_multi; lang=en'}
  74. context = self.parser.parse(headers)
  75. self.assertTrue(context.has_session())
  76. self.assertEqual(context.gateway_session_id, 'GWS_multi')
  77. def test_parse_whitespace_handling(self):
  78. headers = {'Authorization': ' Bearer GWS_spaces '}
  79. context = self.parser.parse(headers)
  80. self.assertTrue(context.has_session())
  81. self.assertEqual(context.gateway_session_id, 'GWS_spaces')
  82. def test_parse_cookie_with_invalid_session_id_format(self):
  83. """Test cookie with non-GWS_ prefixed session ID"""
  84. headers = {'Cookie': 'gateway_session_id=INVALID_123; other=value'}
  85. context = self.parser.parse(headers)
  86. self.assertFalse(context.has_session())
  87. self.assertEqual(context.gateway_session_id, '')
  88. if __name__ == '__main__':
  89. unittest.main()