109 lines
4.1 KiB
Python
109 lines
4.1 KiB
Python
import base64
|
|
import json
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
from tools.analyze_region_ticket import analyze_har
|
|
|
|
|
|
class AnalyzeRegionTicketTests(unittest.TestCase):
|
|
def test_distinguishes_response_issue_from_later_cookie_use(self):
|
|
issued = "RT_issued_ticket"
|
|
har = {
|
|
"log": {
|
|
"entries": [
|
|
{
|
|
"startedDateTime": "2026-07-27T00:00:00.000Z",
|
|
"request": {
|
|
"method": "GET",
|
|
"url": "https://HOST/rest/bootstrap",
|
|
"headers": [],
|
|
},
|
|
"response": {
|
|
"status": 200,
|
|
"headers": [],
|
|
"content": {
|
|
"text": json.dumps(
|
|
{
|
|
"result": 1,
|
|
"region": {
|
|
"uid": "0",
|
|
"name": "cn",
|
|
"ticket": issued,
|
|
},
|
|
}
|
|
)
|
|
},
|
|
},
|
|
},
|
|
{
|
|
"startedDateTime": "2026-07-27T00:00:01.000Z",
|
|
"request": {
|
|
"method": "POST",
|
|
"url": "https://HOST/rest/next",
|
|
"headers": [
|
|
{
|
|
"name": "Cookie",
|
|
"value": f"foo=1; region_ticket={issued}; __NSWJ=",
|
|
}
|
|
],
|
|
},
|
|
"response": {"status": 200, "headers": [], "content": {}},
|
|
},
|
|
]
|
|
}
|
|
}
|
|
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
path = Path(tmp) / "sample.har"
|
|
path.write_text(json.dumps(har), encoding="utf-8")
|
|
report = analyze_har(path)
|
|
|
|
self.assertEqual(report["entry_count"], 2)
|
|
self.assertEqual(len(report["response_regions"]), 1)
|
|
self.assertEqual(report["response_regions"][0]["json_path"], "$.region")
|
|
self.assertEqual(report["response_regions"][0]["uid"], "0")
|
|
self.assertEqual(len(report["request_cookies"]), 1)
|
|
self.assertEqual(report["request_cookies"][0]["url"], "https://HOST/rest/next")
|
|
self.assertNotIn(issued, json.dumps(report))
|
|
|
|
def test_reads_base64_response_and_set_cookie(self):
|
|
ticket = "RT_base64_ticket"
|
|
body = json.dumps({"data": {"region": {"ticket": ticket}}}).encode()
|
|
har = {
|
|
"log": {
|
|
"entries": [
|
|
{
|
|
"request": {"method": "GET", "url": "https://HOST/config"},
|
|
"response": {
|
|
"status": 200,
|
|
"headers": [
|
|
{
|
|
"name": "Set-Cookie",
|
|
"value": f"region_ticket={ticket}; Path=/; Secure",
|
|
}
|
|
],
|
|
"content": {
|
|
"encoding": "base64",
|
|
"text": base64.b64encode(body).decode(),
|
|
},
|
|
},
|
|
}
|
|
]
|
|
}
|
|
}
|
|
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
path = Path(tmp) / "sample.har"
|
|
path.write_text(json.dumps(har), encoding="utf-8")
|
|
report = analyze_har(path)
|
|
|
|
self.assertEqual(report["response_regions"][0]["json_path"], "$.data.region")
|
|
self.assertEqual(len(report["response_set_cookies"]), 1)
|
|
self.assertNotIn(ticket, json.dumps(report))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|