"""Tests for exporters.""" import json import pytest from datetime import datetime from http_log_explorer.exporters import CodeExporter, CurlExporter, JSONExporter from http_log_explorer.models import HTTPEntry, Request, Response def make_entry( method: str = "GET", url: str = "https://api.example.com/test", status: int = 200, req_headers: dict | None = None, resp_body: str | None = None, ) -> HTTPEntry: """Create a test HTTPEntry.""" return HTTPEntry( id=f"entry-{method}-{status}", request=Request( method=method, url=url, headers=req_headers or {"Content-Type": "application/json"}, body='{"key": "value"}', ), response=Response( status=status, status_text="OK", headers={"Content-Type": "application/json"}, body=resp_body or '{"result": "success"}', content_type="application/json", ), timestamp=datetime.now(), ) @pytest.fixture def sample_entries(): """Create sample entries for testing.""" return [ make_entry("GET", "https://api.example.com/users", 200), make_entry("POST", "https://api.example.com/users", 201), make_entry("GET", "https://api.example.com/posts", 404), ] class TestJSONExporter: """Tests for JSONExporter.""" def test_export(self, sample_entries): """Test JSON export.""" exporter = JSONExporter() result = exporter.export(sample_entries) data = json.loads(result) assert len(data) == 3 assert data[0]["request"]["method"] == "GET" def test_export_compact(self, sample_entries): """Test compact JSON export.""" exporter = JSONExporter() result = exporter.export_compact(sample_entries) assert "\n" not in result assert '"id"' in result def test_export_summary(self, sample_entries): """Test summary export.""" exporter = JSONExporter() result = exporter.export_summary(sample_entries) data = json.loads(result) assert len(data) == 3 assert "id" in data[0] assert "method" in data[0] assert "status" in data[0] def test_export_single_entry(self): """Test exporting single entry.""" exporter = JSONExporter() entry = make_entry() result = exporter.export([entry]) data = json.loads(result) assert len(data) == 1 class TestCurlExporter: """Tests for CurlExporter.""" def test_export_single(self): """Test exporting single entry as curl.""" exporter = CurlExporter() entry = make_entry("GET", "https://api.example.com/users") result = exporter.export(entry) assert "curl" in result assert "-X GET" in result assert "https://api.example.com/users" in result def test_export_with_headers(self): """Test exporting with headers.""" exporter = CurlExporter() entry = make_entry(req_headers={"Authorization": "Bearer token"}) result = exporter.export(entry) assert "-H" in result assert "Authorization" in result def test_export_post_with_body(self): """Test exporting POST with body.""" exporter = CurlExporter() entry = make_entry("POST", "https://api.example.com/users") entry.request.body = '{"name": "Test"}' result = exporter.export(entry) assert "-X POST" in result assert "-d" in result def test_export_batch(self, sample_entries): """Test batch export.""" exporter = CurlExporter() results = exporter.export_batch(sample_entries) assert len(results) == 3 assert all("curl" in r for r in results) class TestCodeExporter: """Tests for CodeExporter.""" def test_export_python(self): """Test Python export.""" exporter = CodeExporter() entry = make_entry() result = exporter.export_python(entry) assert "import requests" in result assert "requests.get" in result or "requests.post" in result def test_export_python_post(self): """Test Python export for POST.""" exporter = CodeExporter() entry = make_entry("POST", "https://api.example.com/users") result = exporter.export_python(entry) assert "requests.post" in result def test_export_python_with_body(self): """Test Python export with request body.""" exporter = CodeExporter() entry = make_entry("POST", "https://api.example.com/users") entry.request.body = '{"name": "Test"}' result = exporter.export_python(entry) assert "data=" in result or "json=" in result def test_export_javascript(self): """Test JavaScript export.""" exporter = CodeExporter() entry = make_entry() result = exporter.export_javascript(entry) assert "axios" in result assert "const config" in result def test_export_go(self): """Test Go export.""" exporter = CodeExporter() entry = make_entry() result = exporter.export_go(entry) assert "package main" in result assert "net/http" in result assert "http.NewRequest" in result def test_export_batch_python(self, sample_entries): """Test batch Python export.""" exporter = CodeExporter() results = exporter.export_batch(sample_entries, "python") assert len(results) == 3 assert all("import requests" in r for r in results) def test_export_unsupported_language(self, sample_entries): """Test error on unsupported language.""" exporter = CodeExporter() with pytest.raises(ValueError, match="Unsupported language"): exporter.export_batch(sample_entries, "ruby")