This commit is contained in:
71
tests/test_web.py
Normal file
71
tests/test_web.py
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
import pytest
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
from src.http_convert.web import app
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def client():
|
||||||
|
return TestClient(app)
|
||||||
|
|
||||||
|
|
||||||
|
class TestWebAPI:
|
||||||
|
def test_root(self, client):
|
||||||
|
response = client.get("/")
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert "HTTP Convert" in response.text
|
||||||
|
|
||||||
|
def test_convert_endpoint(self, client):
|
||||||
|
response = client.post("/api/convert", json={
|
||||||
|
"method": "GET",
|
||||||
|
"url": "https://api.example.com/users",
|
||||||
|
"headers": {"Accept": "application/json"},
|
||||||
|
"format": "curl"
|
||||||
|
})
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert "curl" in response.json()["result"]
|
||||||
|
|
||||||
|
def test_convert_with_body(self, client):
|
||||||
|
response = client.post("/api/convert", json={
|
||||||
|
"method": "POST",
|
||||||
|
"url": "https://api.example.com/users",
|
||||||
|
"headers": {"Content-Type": "application/json"},
|
||||||
|
"body": '{"name": "John"}',
|
||||||
|
"format": "fetch"
|
||||||
|
})
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert "fetch" in response.json()["result"]
|
||||||
|
|
||||||
|
def test_parse_endpoint(self, client):
|
||||||
|
response = client.post("/api/parse", json={
|
||||||
|
"input": "curl 'https://api.example.com/users'",
|
||||||
|
"format": "curl"
|
||||||
|
})
|
||||||
|
assert response.status_code == 200
|
||||||
|
data = response.json()
|
||||||
|
assert data["method"] == "GET"
|
||||||
|
assert data["url"] == "https://api.example.com/users"
|
||||||
|
|
||||||
|
def test_parse_invalid_input(self, client):
|
||||||
|
response = client.post("/api/parse", json={
|
||||||
|
"input": "not a valid curl command",
|
||||||
|
"format": "curl"
|
||||||
|
})
|
||||||
|
assert response.status_code == 400
|
||||||
|
|
||||||
|
def test_get_formats(self, client):
|
||||||
|
response = client.get("/api/formats")
|
||||||
|
assert response.status_code == 200
|
||||||
|
data = response.json()
|
||||||
|
assert "curl" in data["formats"]
|
||||||
|
assert "httpie" in data["formats"]
|
||||||
|
assert "fetch" in data["formats"]
|
||||||
|
assert "axios" in data["formats"]
|
||||||
|
|
||||||
|
def test_convert_all_formats(self, client):
|
||||||
|
for fmt in ["curl", "httpie", "fetch", "axios"]:
|
||||||
|
response = client.post("/api/convert", json={
|
||||||
|
"method": "GET",
|
||||||
|
"url": "https://api.example.com",
|
||||||
|
"format": fmt
|
||||||
|
})
|
||||||
|
assert response.status_code == 200, f"Failed for format: {fmt}"
|
||||||
Reference in New Issue
Block a user