From 6812a1d2c1fe9a66f289edd4bda91c3b40465593 Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Thu, 29 Jan 2026 11:34:27 +0000 Subject: [PATCH] Initial commit: Add http-convert project --- tests/test_parsers.py | 123 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 tests/test_parsers.py diff --git a/tests/test_parsers.py b/tests/test_parsers.py new file mode 100644 index 0000000..ff15881 --- /dev/null +++ b/tests/test_parsers.py @@ -0,0 +1,123 @@ +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"