Initial upload: curl-converter-cli with CI/CD workflow
This commit is contained in:
140
tests/test_parser.py
Normal file
140
tests/test_parser.py
Normal file
@@ -0,0 +1,140 @@
|
||||
"""Tests for the curl parser module."""
|
||||
|
||||
import pytest
|
||||
from curlconverter.parser import parse_curl, ParsedCurl, tokenize_command
|
||||
|
||||
|
||||
class TestTokenizeCommand:
|
||||
"""Tests for tokenize_command function."""
|
||||
|
||||
def test_simple_tokens(self):
|
||||
"""Test simple command tokenization."""
|
||||
tokens = tokenize_command("curl -X POST https://example.com")
|
||||
assert "curl" in tokens
|
||||
assert "-X" in tokens
|
||||
assert "POST" in tokens
|
||||
assert "https://example.com" in tokens
|
||||
|
||||
def test_quoted_strings(self):
|
||||
"""Test handling of quoted strings."""
|
||||
tokens = tokenize_command('-H "Content-Type: application/json"')
|
||||
assert '-H' in tokens
|
||||
assert 'Content-Type: application/json' in tokens
|
||||
|
||||
def test_single_quotes(self):
|
||||
"""Test handling of single quotes."""
|
||||
tokens = tokenize_command("-d '{\"key\": \"value\"}'")
|
||||
assert "-d" in tokens
|
||||
assert '{"key": "value"}' in tokens
|
||||
|
||||
|
||||
class TestParseCurl:
|
||||
"""Tests for parse_curl function."""
|
||||
|
||||
def test_basic_get_request(self):
|
||||
"""Test parsing a basic GET request."""
|
||||
curl = "curl https://example.com"
|
||||
result = parse_curl(curl)
|
||||
assert result.url == "https://example.com"
|
||||
assert result.method == "GET"
|
||||
|
||||
def test_url_with_protocol(self):
|
||||
"""Test URL with http protocol."""
|
||||
curl = "curl http://example.com"
|
||||
result = parse_curl(curl)
|
||||
assert result.url == "http://example.com"
|
||||
|
||||
def test_post_request(self):
|
||||
"""Test parsing POST request with -X."""
|
||||
curl = "curl -X POST https://api.example.com/users"
|
||||
result = parse_curl(curl)
|
||||
assert result.url == "https://api.example.com/users"
|
||||
assert result.method == "POST"
|
||||
|
||||
def test_post_with_data(self):
|
||||
"""Test parsing POST with -d flag."""
|
||||
curl = "curl -d 'name=test' https://api.example.com/users"
|
||||
result = parse_curl(curl)
|
||||
assert result.method == "POST"
|
||||
assert result.data == "name=test"
|
||||
|
||||
def test_post_with_data_raw(self):
|
||||
"""Test parsing POST with --data-raw flag."""
|
||||
curl = 'curl --data-raw {"name":"test"} https://api.example.com/users'
|
||||
result = parse_curl(curl)
|
||||
assert result.method == "POST"
|
||||
assert result.data == '{name:test}'
|
||||
|
||||
def test_headers(self):
|
||||
"""Test parsing headers."""
|
||||
curl = 'curl -H "Content-Type: application/json" -H "Authorization: Bearer token" https://api.example.com'
|
||||
result = parse_curl(curl)
|
||||
assert "Content-Type" in result.headers
|
||||
assert result.headers["Content-Type"] == "application/json"
|
||||
assert "Authorization" in result.headers
|
||||
|
||||
def test_basic_auth(self):
|
||||
"""Test parsing basic authentication."""
|
||||
curl = "curl -u user:pass https://api.example.com"
|
||||
result = parse_curl(curl)
|
||||
assert result.auth == ("user", "pass")
|
||||
|
||||
def test_cookies(self):
|
||||
"""Test parsing cookies."""
|
||||
curl = "curl -b 'session=abc123' https://example.com"
|
||||
result = parse_curl(curl)
|
||||
assert result.cookies == "session=abc123"
|
||||
|
||||
def test_user_agent(self):
|
||||
"""Test parsing user agent."""
|
||||
curl = "curl -A 'Mozilla/5.0' https://example.com"
|
||||
result = parse_curl(curl)
|
||||
assert result.user_agent == "Mozilla/5.0"
|
||||
|
||||
def test_full_command(self):
|
||||
"""Test parsing a complete curl command."""
|
||||
curl = '''curl -X POST \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Authorization: Bearer token" \
|
||||
-d '{\"name\":\"test\"}' \
|
||||
-u user:pass \
|
||||
-b "session=abc" \
|
||||
https://api.example.com/users'''
|
||||
result = parse_curl(curl)
|
||||
assert result.url == "https://api.example.com/users"
|
||||
assert result.method == "POST"
|
||||
assert "Content-Type" in result.headers
|
||||
assert result.data == '{"name":"test"}'
|
||||
assert result.auth == ("user", "pass")
|
||||
assert result.cookies == "session=abc"
|
||||
|
||||
def test_empty_command_raises_error(self):
|
||||
"""Test that empty command raises ValueError."""
|
||||
with pytest.raises(ValueError):
|
||||
parse_curl("")
|
||||
|
||||
def test_no_url_raises_error(self):
|
||||
"""Test that command without URL raises ValueError."""
|
||||
with pytest.raises(ValueError):
|
||||
parse_curl("curl -X POST")
|
||||
|
||||
def test_curl_prefix_optional(self):
|
||||
"""Test that 'curl' prefix is optional."""
|
||||
result = parse_curl("https://example.com")
|
||||
assert result.url == "https://example.com"
|
||||
assert result.method == "GET"
|
||||
|
||||
|
||||
class TestParsedCurl:
|
||||
"""Tests for ParsedCurl dataclass."""
|
||||
|
||||
def test_default_values(self):
|
||||
"""Test default values."""
|
||||
curl = ParsedCurl(url="https://example.com")
|
||||
assert curl.url == "https://example.com"
|
||||
assert curl.method == "GET"
|
||||
assert curl.headers == {}
|
||||
assert curl.data is None
|
||||
assert curl.auth is None
|
||||
assert curl.cookies is None
|
||||
assert curl.user_agent is None
|
||||
Reference in New Issue
Block a user