import pytest from curl_to_code.parser import parse_curl_command, curl_tokenize from curl_to_code.parser.models import RequestMethod, AuthType, DataType class TestCurlTokenizer: def test_simple_curl(self): tokens = curl_tokenize("curl https://example.com") assert "curl" in tokens assert "https://example.com" in tokens def test_curl_with_headers(self): tokens = curl_tokenize('curl -H "Accept: application/json" https://example.com') assert len(tokens) >= 4 def test_curl_with_quoted_values(self): tokens = curl_tokenize('curl -H "Content-Type: application/json" https://example.com') assert any('"Content-Type: application/json"' in t or 'Content-Type: application/json' in t for t in tokens) class TestCurlParser: def test_parse_simple_get(self): result = parse_curl_command("curl https://api.example.com/users") assert result.url == "https://api.example.com/users" assert result.method == RequestMethod.GET def test_parse_post_method(self): result = parse_curl_command("curl -X POST https://api.example.com/users") assert result.method == RequestMethod.POST def test_parse_put_method(self): result = parse_curl_command("curl --request PUT https://api.example.com/users/1") assert result.method == RequestMethod.PUT def test_parse_delete_method(self): result = parse_curl_command("curl -X DELETE https://api.example.com/users/1") assert result.method == RequestMethod.DELETE def test_parse_header(self): result = parse_curl_command('curl -H "Accept: application/json" https://api.example.com') assert len(result.headers) >= 1 header = result.headers[0] assert header.key == "Accept" assert header.value == "application/json" def test_parse_multiple_headers(self): result = parse_curl_command( 'curl -H "Accept: application/json" -H "X-Custom-Header: value" https://api.example.com' ) assert len(result.headers) >= 2 def test_parse_basic_auth(self): result = parse_curl_command("curl -u user:pass https://api.example.com") assert result.auth is not None assert result.auth.username == "user" assert result.auth.password == "pass" def test_parse_post_data(self): result = parse_curl_command("curl -d 'name=test' https://api.example.com") assert result.data is not None assert result.data.content == "name=test" def test_parse_json_data(self): result = parse_curl_command("curl -d '{\"name\":\"test\"}' https://api.example.com") assert result.data is not None assert result.data.data_type == DataType.JSON assert result.data.json_data == {"name": "test"} def test_parse_url_flag(self): result = parse_curl_command("curl --url https://api.example.com") assert result.url == "https://api.example.com" def test_parse_user_agent(self): result = parse_curl_command("curl -A 'Mozilla/5.0' https://api.example.com") assert result.user_agent == "Mozilla/5.0" def test_parse_cookie(self): result = parse_curl_command("curl -b 'session=abc123' https://api.example.com") assert "session" in result.cookies assert result.cookies["session"] == "abc123" def test_parse_timeout(self): result = parse_curl_command("curl --connect-timeout 30 https://api.example.com") assert result.timeout == 30 def test_parse_follow_redirects(self): result = parse_curl_command("curl -L https://api.example.com") assert result.follow_redirects is True def test_complex_curl_command(self): cmd = '''curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer token123" -d '{"name":"John","age":30}' https://api.example.com/users''' result = parse_curl_command(cmd) assert result.method == RequestMethod.POST assert result.url == "https://api.example.com/users" assert len(result.headers) >= 1 assert result.data is not None assert result.data.data_type == DataType.JSON def test_curl_with_data_raw(self): result = parse_curl_command("curl --data-raw 'name=test' https://api.example.com") assert result.data is not None assert result.data.content == "name=test" def test_curl_with_data_binary(self): result = parse_curl_command("curl --data-binary '@file.txt' https://api.example.com") assert result.data is not None def test_bearer_token_auth(self): result = parse_curl_command("-H \"Authorization: Bearer mytoken123\" https://api.example.com") assert result.auth is None auth_header = next((h for h in result.headers if h.key == "Authorization"), None) assert auth_header is not None assert "Bearer" in auth_header.value