From 9a4267373d01b3adb38b0a7a872ec8ea269a0cf4 Mon Sep 17 00:00:00 2001 From: Ian Cooke Date: Thu, 2 Mar 2023 13:48:55 -0500 Subject: [PATCH] fix(cli): header parsing when multiple equals present --- CHANGELOG.md | 4 ++++ pystac_client/cli.py | 8 +++++--- tests/test_cli.py | 27 +++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f99ec05d..ad74be52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +### Fixed + +- Fix parse fail when header has multiple '=' characters [#440](https://github.com/stac-utils/pystac-client/pull/440) + ## [v0.6.0] - 2023-01-27 ### Added diff --git a/pystac_client/cli.py b/pystac_client/cli.py index cbe50974..06633135 100644 --- a/pystac_client/cli.py +++ b/pystac_client/cli.py @@ -2,6 +2,7 @@ import json import logging import os +import re import sys from typing import Any, Dict, List, Optional @@ -191,10 +192,11 @@ def parse_args(args: List[str]) -> Dict[str, Any]: # if headers provided, parse it if "headers" in parsed_args: new_headers = {} + splitter = re.compile("^([^=]+)=(.+)$") for head in parsed_args["headers"]: - parts = head.split("=") - if len(parts) == 2: - new_headers[parts[0]] = parts[1] + match = splitter.match(head) + if match: + new_headers[match.group(1)] = match.group(2) else: logger.warning(f"Unable to parse header {head}") parsed_args["headers"] = new_headers diff --git a/tests/test_cli.py b/tests/test_cli.py index 14d1292f..9994cd46 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,6 +1,9 @@ +from typing import List + import pytest from pytest_console_scripts import ScriptRunner +import pystac_client.cli from tests.helpers import STAC_URLS @@ -19,6 +22,30 @@ def test_item_search(self, script_runner: ScriptRunner) -> None: result = script_runner.run(*args, print_result=False) assert result.success + @pytest.mark.parametrize( + "headers,good_header_count", + [ + (["kick=flip", "home=run"], 2), + (["mad=pow"], 1), + (["=no-var"], 0), + (["no-val="], 0), + (["good=header", "bad-header"], 1), + (["header=value-with-three-=-signs-=", "plain=jane"], 2), + ], + ) + def test_headers(self, headers: List[str], good_header_count: int) -> None: + args = [ + "search", + STAC_URLS["PLANETARY-COMPUTER"], + "-c", + "naip", + "--max-items", + "20", + "--headers", + ] + headers + pargs = pystac_client.cli.parse_args(args) + assert len(pargs["headers"]) == good_header_count + def test_no_arguments(self, script_runner: ScriptRunner) -> None: args = ["stac-client"] result = script_runner.run(*args, print_result=False)