From 861b8b36a88a83371f8d2a97593aa7b3798ee2b3 Mon Sep 17 00:00:00 2001 From: Jakub Roztocil Date: Fri, 5 Nov 2021 13:59:23 +0100 Subject: [PATCH] Strip leading `://` from URLs to allow quick conversion of a pasted URL to calls (#1197) * Strip leading `://` from URLs to allow quick conversion of a pasted URL to calls Closes #1195 * Markdown lint * Cleanup * Cleanup * Drop extraneous space * Fix example --- CHANGELOG.md | 2 +- docs/README.md | 17 ++++++++++++++--- httpie/cli/argparser.py | 3 +++ tests/test_cli.py | 10 ++++++++++ 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d2a73c9..74b4956c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ This project adheres to [Semantic Versioning](https://semver.org/). ## [2.7.0.dev0](https://github.com/httpie/httpie/compare/2.6.0...master) (unreleased) - Added support for sending multiple HTTP headers with the same name. ([#130](https://github.com/httpie/httpie/issues/130)) +- Added support for keeping `://` in the URL argument to allow quick conversions of pasted URLs into HTTPie calls just by adding a space after the protocol name (`$ https ://pie.dev` → `https://pie.dev`). ([#1195](https://github.com/httpie/httpie/issues/1195)) ## [2.6.0](https://github.com/httpie/httpie/compare/2.5.0...2.6.0) (2021-10-14) @@ -31,7 +32,6 @@ This project adheres to [Semantic Versioning](https://semver.org/). - Fixed `--verbose` HTTP 307 redirects with streamed request body. ([#1088](https://github.com/httpie/httpie/issues/1088)) - Fixed handling of session files with `Cookie:` followed by other headers. ([#1126](https://github.com/httpie/httpie/issues/1126)) - ## [2.4.0](https://github.com/httpie/httpie/compare/2.3.0...2.4.0) (2021-02-06) - Added support for `--session` cookie expiration based on `Set-Cookie: max-age=`. ([#1029](https://github.com/httpie/httpie/issues/1029)) diff --git a/docs/README.md b/docs/README.md index ff6fbad1..4490ee95 100644 --- a/docs/README.md +++ b/docs/README.md @@ -482,14 +482,26 @@ The default scheme is `http://` and can be omitted from the argument: ```bash $ http example.org -# => http://example.org +# → http://example.org ``` HTTPie also installs an `https` executable, where the default scheme is `https://`: ```bash $ https example.org -# => https://example.org +# → https://example.org +``` + +When you paste a URL into the terminal, you can even keep the `://` bit in the URL argument to quickly convert the URL into an HTTPie call just by adding a space after the protocol name. + +```bash +$ https ://example.org +# → https://example.org +``` + +```bash +$ http ://example.org +# → http://example.org ``` ### Querystring parameters @@ -1947,7 +1959,6 @@ HTTPie has the following community channels: - [Discord server](https://httpie.io/discord) to ask questions, discuss features, and for general API development discussion - [StackOverflow](https://stackoverflow.com) to ask questions (make sure to use the [httpie](https://stackoverflow.com/questions/tagged/httpie) tag) - ### Related projects #### Dependencies diff --git a/httpie/cli/argparser.py b/httpie/cli/argparser.py index 0b689410..54c89965 100644 --- a/httpie/cli/argparser.py +++ b/httpie/cli/argparser.py @@ -120,6 +120,9 @@ class HTTPieArgumentParser(argparse.ArgumentParser): } def _process_url(self): + if self.args.url.startswith('://'): + # Paste URL & add space shortcut: `http ://pie.dev` → `http://pie.dev` + self.args.url = self.args.url[3:] if not URL_SCHEME_RE.match(self.args.url): if os.path.basename(self.env.program_name) == 'https': scheme = 'https://' diff --git a/tests/test_cli.py b/tests/test_cli.py index e0f52147..3754a74a 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -169,6 +169,16 @@ class TestQuerystring: assert f'"url": "{url}"' in r +@pytest.mark.parametrize(['program_name', 'url_arg', 'parsed_url'], [ + ('http', '://pie.dev/get', 'http://pie.dev/get'), + ('https', '://pie.dev/get', 'https://pie.dev/get'), +]) +def test_url_leading_colon_slash_slash(program_name, url_arg, parsed_url): + env = MockEnvironment(program_name=program_name) + args = parser.parse_args(args=[url_arg], env=env) + assert args.url == parsed_url + + class TestLocalhostShorthand: def test_expand_localhost_shorthand(self): args = parser.parse_args(args=[':'], env=MockEnvironment())