1. import json
  2. import difflib
  3. def diff_http_responses(file1_path, file2_path):
  4. """
  5. Diffs two datasets of HTTP responses stored as JSON files.
  6. """
  7. try:
  8. with open(file1_path, 'r') as f1:
  9. data1 = json.load(f1)
  10. with open(file2_path, 'r') as f2:
  11. data2 = json.load(f2)
  12. except FileNotFoundError as e:
  13. print(f"Error: File not found: {e}")
  14. return
  15. if not isinstance(data1, list) or not isinstance(data2, list):
  16. print("Error: Input files must contain lists of HTTP response dictionaries.")
  17. return
  18. if len(data1) != len(data2):
  19. print("Warning: Number of responses differs between files.")
  20. diff = difflib.unified_diff(
  21. [json.dumps(resp) for resp in data1],
  22. [json.dumps(resp) for resp in data2],
  23. fromfile=file1_path,
  24. tofile=file2_path,
  25. lineterm=''
  26. )
  27. for line in diff:
  28. print(line)
  29. if __name__ == '__main__':
  30. # Example usage
  31. # Create dummy JSON files for testing
  32. with open("response1.json", "w") as f:
  33. json.dump([{"status_code": 200, "content": "OK"}, {"status_code": 404, "content": "Not Found"}], f, indent=4)
  34. with open("response2.json", "w") as f:
  35. json.dump([{"status_code": 200, "content": "OK"}, {"status_code": 500, "content": "Internal Server Error"}], f, indent=4)
  36. diff_http_responses("response1.json", "response2.json")

Add your comment