Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/posting/importing/curl.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,19 @@ def __init__(self, curl_command: str):
action="store_true",
help="Use HTTP Digest Authentication",
)
parser.add_argument(
"-b", "--cookie", action="append", help="Send cookies"
)
parser.add_argument(
"-L",
"--location",
action="store_true",
help="Follow redirects",
)
parser.add_argument("--no-location", action="store_true")
parser.add_argument(
"-x", "--proxy", help="Use the specified proxy"
)
parser.add_argument("url", nargs="?")

args, extras = parser.parse_known_intermixed_args(tokens)
Expand Down
45 changes: 45 additions & 0 deletions tests/test_curl_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,48 @@ def test_curl_imports_max_time():
}"""
)
assert curl_import.user == "darren:"


def test_curl_with_cookies_before_url():
"""Test that --cookie flags before the URL don't clobber the URL.

Regression test for https://github.com/darrenburns/posting/issues/309
"""
curl_command = (
"curl "
"--cookie 'session=abc123' "
"--cookie 'token=xyz' "
"'http://example.com/api'"
)
curl_import = CurlImport(curl_command)
assert curl_import.url == "http://example.com/api"
assert curl_import.method == "GET"


def test_curl_with_cookies_and_headers_before_url():
"""Test that a complex curl command with cookies and headers before the URL
correctly identifies the URL."""
curl_command = (
"curl "
"-H 'Accept: application/json' "
"--cookie 'AEC=value1' "
"--cookie 'logged_in=no' "
"'http://google.com'"
)
curl_import = CurlImport(curl_command)
assert curl_import.url == "http://google.com"
assert curl_import.headers == [("Accept", "application/json")]


def test_curl_with_location_flag():
"""Test that -L/--location flag is recognized without breaking URL parsing."""
curl_command = "curl -L http://example.com"
curl_import = CurlImport(curl_command)
assert curl_import.url == "http://example.com"


def test_curl_with_proxy_flag():
"""Test that --proxy flag is recognized without breaking URL parsing."""
curl_command = "curl --proxy 'http://proxy:8080' http://example.com"
curl_import = CurlImport(curl_command)
assert curl_import.url == "http://example.com"