Fix CI issues: add workflow file and fix lint errors
Some checks failed
CI / test (push) Has been cancelled

This commit is contained in:
2026-03-22 10:54:02 +00:00
parent cdf9436a2e
commit cec136d7e1

View File

@@ -1,7 +1,7 @@
"""Tests for the curl parser module."""
{"""Tests for the parser module."""
import pytest
from curlconverter.parser import parse_curl, ParsedCurl, tokenize_command
from curlconverter.parser import parse_curl, tokenize_command, ParsedCurl
class TestTokenizeCommand:
@@ -9,120 +9,101 @@ class TestTokenizeCommand:
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
tokens = tokenize_command("curl -X GET http://example.com")
assert tokens == ["curl", "-X", "GET", "http://example.com"]
def test_quoted_strings(self):
"""Test handling of quoted strings."""
tokens = tokenize_command('-H "Content-Type: application/json"')
assert '-H' in tokens
"""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 handling of single quotes."""
tokens = tokenize_command("-d '{\"key\": \"value\"}'")
"""Test single quote tokenization."""
tokens = tokenize_command("curl -d 'hello world' http://example.com")
assert "-d" in tokens
assert '{"key": "value"}' in tokens
assert "'hello world'" 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"
"""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 http protocol."""
curl = "curl http://example.com"
result = parse_curl(curl)
assert result.url == "http://example.com"
"""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 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"
"""Test POST request parsing."""
parsed = parse_curl("curl -X POST http://example.com")
assert parsed.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"
"""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 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}'
"""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 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
"""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 parsing basic authentication."""
curl = "curl -u user:pass https://api.example.com"
result = parse_curl(curl)
assert result.auth == ("user", "pass")
"""Test basic auth parsing."""
parsed = parse_curl("curl -u user:pass http://example.com")
assert parsed.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"
"""Test cookie parsing."""
parsed = parse_curl("curl -b 'session=abc123' http://example.com")
assert parsed.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"
"""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 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"
"""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 that empty command raises ValueError."""
with pytest.raises(ValueError):
"""Test empty command raises error."""
with pytest.raises(ValueError, match="Empty curl command"):
parse_curl("")
def test_no_url_raises_error(self):
"""Test that command without URL raises ValueError."""
with pytest.raises(ValueError):
"""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."""
result = parse_curl("https://example.com")
assert result.url == "https://example.com"
assert result.method == "GET"
parsed = parse_curl("http://example.com")
assert parsed.url == "http://example.com"
class TestParsedCurl:
@@ -130,11 +111,11 @@ class TestParsedCurl:
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
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