72 lines
2.6 KiB
Python
72 lines
2.6 KiB
Python
import pytest
|
|
from src.http_convert.models import HTTPRequest, HttpMethod, OutputFormat, InputFormat
|
|
|
|
|
|
class TestHTTPRequest:
|
|
def test_default_values(self):
|
|
request = HTTPRequest(url="https://example.com")
|
|
assert request.method == HttpMethod.GET
|
|
assert request.url == "https://example.com"
|
|
assert request.headers == {}
|
|
assert request.params == {}
|
|
assert request.body is None
|
|
|
|
def test_custom_values(self):
|
|
request = HTTPRequest(
|
|
method=HttpMethod.POST,
|
|
url="https://api.example.com/users",
|
|
headers={"Content-Type": "application/json"},
|
|
params={"page": "1"},
|
|
body='{"name": "test"}'
|
|
)
|
|
assert request.method == HttpMethod.POST
|
|
assert request.url == "https://api.example.com/users"
|
|
assert request.headers["Content-Type"] == "application/json"
|
|
assert request.params["page"] == "1"
|
|
assert request.body == '{"name": "test"}'
|
|
|
|
def test_get_final_url_no_params(self):
|
|
request = HTTPRequest(url="https://example.com/users")
|
|
assert request.get_final_url() == "https://example.com/users"
|
|
|
|
def test_get_final_url_with_params(self):
|
|
request = HTTPRequest(
|
|
url="https://example.com/users",
|
|
params={"page": "1", "limit": "10"}
|
|
)
|
|
result = request.get_final_url()
|
|
assert "https://example.com/users?page=1&limit=10" == result or \
|
|
"https://example.com/users?limit=10&page=1" == result
|
|
|
|
def test_get_final_url_with_existing_query(self):
|
|
request = HTTPRequest(
|
|
url="https://example.com/users?active=true",
|
|
params={"page": "1"}
|
|
)
|
|
result = request.get_final_url()
|
|
assert "page=1" in result
|
|
assert "active=true" in result
|
|
|
|
|
|
class TestHttpMethod:
|
|
def test_all_methods(self):
|
|
assert HttpMethod.GET.value == "GET"
|
|
assert HttpMethod.POST.value == "POST"
|
|
assert HttpMethod.PUT.value == "PUT"
|
|
assert HttpMethod.PATCH.value == "PATCH"
|
|
assert HttpMethod.DELETE.value == "DELETE"
|
|
assert HttpMethod.HEAD.value == "HEAD"
|
|
assert HttpMethod.OPTIONS.value == "OPTIONS"
|
|
|
|
def test_method_from_string(self):
|
|
assert HttpMethod("GET") == HttpMethod.GET
|
|
assert HttpMethod("POST") == HttpMethod.POST
|
|
|
|
|
|
class TestOutputFormat:
|
|
def test_all_formats(self):
|
|
assert OutputFormat.CURL.value == "curl"
|
|
assert OutputFormat.HTTPie.value == "httpie"
|
|
assert OutputFormat.FETCH.value == "fetch"
|
|
assert OutputFormat.AXIOS.value == "axios"
|