124 lines
4.4 KiB
Python
124 lines
4.4 KiB
Python
import pytest
|
|
from src.http_convert.parsers import CurlParser, HTTPieParser, FetchParser, AxiosParser, Parser
|
|
|
|
|
|
class TestCurlParser:
|
|
def test_parse_simple_get(self):
|
|
curl = "curl 'https://api.example.com/users'"
|
|
result = CurlParser.parse(curl)
|
|
assert result.method == "GET"
|
|
assert result.url == "https://api.example.com/users"
|
|
assert result.headers == {}
|
|
assert result.body is None
|
|
|
|
def test_parse_get_no_quotes(self):
|
|
curl = "curl https://api.example.com/users"
|
|
result = CurlParser.parse(curl)
|
|
assert result.method == "GET"
|
|
assert result.url == "https://api.example.com/users"
|
|
|
|
def test_parse_post(self):
|
|
curl = "curl -X POST https://api.example.com/users"
|
|
result = CurlParser.parse(curl)
|
|
assert result.method == "POST"
|
|
assert result.url == "https://api.example.com/users"
|
|
|
|
def test_parse_with_request_flag(self):
|
|
curl = "curl --request POST https://api.example.com/users"
|
|
result = CurlParser.parse(curl)
|
|
assert result.method == "POST"
|
|
|
|
def test_parse_invalid_input(self):
|
|
with pytest.raises(ValueError):
|
|
CurlParser.parse("not a curl command")
|
|
|
|
def test_parse_missing_url(self):
|
|
with pytest.raises(ValueError):
|
|
CurlParser.parse("curl -X POST")
|
|
|
|
|
|
class TestHTTPieParser:
|
|
def test_parse_simple_get(self):
|
|
httpie = "http GET https://api.example.com/users"
|
|
result = HTTPieParser.parse(httpie)
|
|
assert result.method == "GET"
|
|
assert result.url == "https://api.example.com/users"
|
|
|
|
def test_parse_post(self):
|
|
httpie = "http POST https://api.example.com/users"
|
|
result = HTTPieParser.parse(httpie)
|
|
assert result.method == "POST"
|
|
|
|
def test_parse_with_query_params(self):
|
|
httpie = "http GET https://api.example.com/users page==1 limit==10"
|
|
result = HTTPieParser.parse(httpie)
|
|
assert result.params["page"] == "1"
|
|
assert result.params["limit"] == "10"
|
|
|
|
def test_parse_invalid_input(self):
|
|
with pytest.raises(ValueError):
|
|
HTTPieParser.parse("not httpie")
|
|
|
|
|
|
class TestFetchParser:
|
|
def test_parse_simple_fetch(self):
|
|
fetch = "fetch('https://api.example.com/users', { method: 'GET' })"
|
|
result = FetchParser.parse(fetch)
|
|
assert result.method == "GET"
|
|
assert result.url == "https://api.example.com/users"
|
|
|
|
def test_parse_fetch_post_oneline(self):
|
|
fetch = "fetch('https://api.example.com/users', {method: 'POST'})"
|
|
result = FetchParser.parse(fetch)
|
|
assert result.method == "POST"
|
|
assert result.url == "https://api.example.com/users"
|
|
|
|
def test_parse_invalid_input(self):
|
|
with pytest.raises(ValueError):
|
|
FetchParser.parse("not a fetch call")
|
|
|
|
|
|
class TestAxiosParser:
|
|
def test_parse_simple_axios(self):
|
|
axios = "axios({ method: 'GET', url: 'https://api.example.com/users' })"
|
|
result = AxiosParser.parse(axios)
|
|
assert result.method == "GET"
|
|
assert result.url == "https://api.example.com/users"
|
|
|
|
def test_parse_axios_with_post(self):
|
|
axios = "axios({ method: 'POST', url: 'https://api.example.com/users' })"
|
|
result = AxiosParser.parse(axios)
|
|
assert result.method == "POST"
|
|
|
|
def test_parse_axios_request(self):
|
|
axios = "axios.request({ method: 'GET', url: 'https://api.example.com/users' })"
|
|
result = AxiosParser.parse(axios)
|
|
assert result.method == "GET"
|
|
|
|
def test_parse_invalid_input(self):
|
|
with pytest.raises(ValueError):
|
|
AxiosParser.parse("not an axios call")
|
|
|
|
|
|
class TestParser:
|
|
def test_detect_curl(self):
|
|
assert Parser.detect_format("curl 'https://example.com'") == "curl"
|
|
|
|
def test_detect_httpie(self):
|
|
assert Parser.detect_format("http GET https://example.com") == "httpie"
|
|
|
|
def test_detect_fetch(self):
|
|
assert Parser.detect_format("fetch('https://example.com')") == "fetch"
|
|
|
|
def test_detect_axios(self):
|
|
assert Parser.detect_format("axios({url: 'https://example.com'})") == "axios"
|
|
|
|
def test_detect_unknown(self):
|
|
with pytest.raises(ValueError):
|
|
Parser.detect_format("unknown format")
|
|
|
|
def test_parse_with_explicit_format(self):
|
|
curl = "curl 'https://api.example.com/users'"
|
|
result = Parser.parse(curl, "curl")
|
|
assert result.url == "https://api.example.com/users"
|