import json
import difflib
def diff_http_responses(file1_path, file2_path):
"""
Diffs two datasets of HTTP responses stored as JSON files.
"""
try:
with open(file1_path, 'r') as f1:
data1 = json.load(f1)
with open(file2_path, 'r') as f2:
data2 = json.load(f2)
except FileNotFoundError as e:
print(f"Error: File not found: {e}")
return
if not isinstance(data1, list) or not isinstance(data2, list):
print("Error: Input files must contain lists of HTTP response dictionaries.")
return
if len(data1) != len(data2):
print("Warning: Number of responses differs between files.")
diff = difflib.unified_diff(
[json.dumps(resp) for resp in data1],
[json.dumps(resp) for resp in data2],
fromfile=file1_path,
tofile=file2_path,
lineterm=''
)
for line in diff:
print(line)
if __name__ == '__main__':
# Example usage
# Create dummy JSON files for testing
with open("response1.json", "w") as f:
json.dump([{"status_code": 200, "content": "OK"}, {"status_code": 404, "content": "Not Found"}], f, indent=4)
with open("response2.json", "w") as f:
json.dump([{"status_code": 200, "content": "OK"}, {"status_code": 500, "content": "Internal Server Error"}], f, indent=4)
diff_http_responses("response1.json", "response2.json")
Add your comment