Files
curl-converter-cli/tests/test_parser.py
7000pctAUTO cec136d7e1
Some checks failed
CI / test (push) Has been cancelled
Fix CI issues: add workflow file and fix lint errors
2026-03-22 10:54:02 +00:00

121 lines
4.5 KiB
Python

{"""Tests for the parser module."""
import pytest
from curlconverter.parser import parse_curl, tokenize_command, ParsedCurl
class TestTokenizeCommand:
"""Tests for tokenize_command function."""
def test_simple_tokens(self):
"""Test simple command tokenization."""
tokens = tokenize_command("curl -X GET http://example.com")
assert tokens == ["curl", "-X", "GET", "http://example.com"]
def test_quoted_strings(self):
"""Test quoted string tokenization."""
tokens = tokenize_command('curl -H "Content-Type: application/json" http://example.com')
assert "-H" in tokens
assert 'Content-Type: application/json' in tokens
def test_single_quotes(self):
"""Test single quote tokenization."""
tokens = tokenize_command("curl -d 'hello world' http://example.com")
assert "-d" in tokens
assert "'hello world'" in tokens
class TestParseCurl:
"""Tests for parse_curl function."""
def test_basic_get_request(self):
"""Test basic GET request parsing."""
parsed = parse_curl("curl http://example.com")
assert parsed.url == "http://example.com"
assert parsed.method == "GET"
def test_url_with_protocol(self):
"""Test URL with protocol."""
parsed = parse_curl("curl https://api.example.com/endpoint")
assert parsed.url == "https://api.example.com/endpoint"
def test_post_request(self):
"""Test POST request parsing."""
parsed = parse_curl("curl -X POST http://example.com")
assert parsed.method == "POST"
def test_post_with_data(self):
"""Test POST with data."""
parsed = parse_curl("curl -d 'key=value' http://example.com")
assert parsed.data == "key=value"
assert parsed.method == "POST"
def test_post_with_data_raw(self):
"""Test POST with --data-raw."""
parsed = parse_curl("curl --data-raw '{"key": "value"}' http://example.com")
assert parsed.data == '{"key": "value"}'
assert parsed.method == "POST"
def test_headers(self):
"""Test header parsing."""
parsed = parse_curl('curl -H "Content-Type: application/json" http://example.com')
assert "Content-Type" in parsed.headers
assert parsed.headers["Content-Type"] == "application/json"
def test_basic_auth(self):
"""Test basic auth parsing."""
parsed = parse_curl("curl -u user:pass http://example.com")
assert parsed.auth == ("user", "pass")
def test_cookies(self):
"""Test cookie parsing."""
parsed = parse_curl("curl -b 'session=abc123' http://example.com")
assert parsed.cookies == "session=abc123"
def test_user_agent(self):
"""Test user-agent parsing."""
parsed = parse_curl("curl -A 'Mozilla/5.0' http://example.com")
assert parsed.user_agent == "Mozilla/5.0"
def test_full_command(self):
"""Test full curl command with all options."""
cmd = 'curl -X POST -H "Content-Type: application/json" -d \'{"key":"value"}\' -u user:pass -b "session=abc" -A "Mozilla" http://example.com/api'
parsed = parse_curl(cmd)
assert parsed.url == "http://example.com/api"
assert parsed.method == "POST"
assert "Content-Type" in parsed.headers
assert parsed.data == '{"key":"value"}'
assert parsed.auth == ("user", "pass")
assert parsed.cookies == "session=abc"
assert parsed.user_agent == "Mozilla"
def test_empty_command_raises_error(self):
"""Test empty command raises error."""
with pytest.raises(ValueError, match="Empty curl command"):
parse_curl("")
def test_no_url_raises_error(self):
"""Test command without URL raises error."""
with pytest.raises(ValueError, match="No URL found"):
parse_curl("curl -X POST")
def test_curl_prefix_optional(self):
"""Test that 'curl' prefix is optional."""
parsed = parse_curl("http://example.com")
assert parsed.url == "http://example.com"
class TestParsedCurl:
"""Tests for ParsedCurl dataclass."""
def test_default_values(self):
"""Test default values."""
parsed = ParsedCurl(url="http://example.com")
assert parsed.url == "http://example.com"
assert parsed.method == "GET"
assert parsed.headers == {}
assert parsed.data is None
assert parsed.auth is None
assert parsed.cookies is None
assert parsed.user_agent is None