Skip to content

Commit

Permalink
Merge e7553b3 into 1ded5c2
Browse files Browse the repository at this point in the history
  • Loading branch information
nrocco committed Oct 22, 2015
2 parents 1ded5c2 + e7553b3 commit 14af8bb
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS.rst
Expand Up @@ -33,3 +33,4 @@ Patches and ideas
* `Matthias Lehmann <https://github.com/matleh>`_
* `Dennis Brakhane <https://github.com/brakhane>`_
* `Matt Layman <https://github.com/mblayman>`_
* `Nico Di Rocco <https://github.com/nrocco>`_
30 changes: 30 additions & 0 deletions README.rst
Expand Up @@ -277,6 +277,36 @@ If the port is omitted, then port 80 is assumed.
GET / HTTP/1.1
Host: localhost
Finally, you can use the base URL feature. If a base URL has been configured
through an environment variable ``HTTPIE_BASEURL``, request URLs such as
``/todos`` will get the base URL automatically prepended.
.. code-block:: bash
$ export HTTPIE_BASEURL=http://example.com
$ http /todos
.. code-block:: http
GET /todos HTTP/1.1
Host: example.com
.. code-block:: bash
$ http POST /
.. code-block:: http
POST / HTTP/1.1
Host: example.com
If the ``HTTPIE_BASEURL`` does not start with a scheme, the default scheme
``http://`` is used.
If you find yourself manually constructing URLs with **querystring parameters**
on the terminal, you may appreciate the ``param==value`` syntax for appending
URL parameters so that you don't have to worry about escaping the ``&``
Expand Down
2 changes: 2 additions & 0 deletions httpie/context.py
@@ -1,4 +1,5 @@
import sys
import os

from httpie.compat import is_windows
from httpie.config import DEFAULT_CONFIG_DIR, Config
Expand All @@ -15,6 +16,7 @@ class Environment(object):
"""
is_windows = is_windows
base_url = os.environ.get('HTTPIE_BASEURL', '')
config_dir = DEFAULT_CONFIG_DIR
stdin = sys.stdin
stdin_isatty = stdin.isatty()
Expand Down
8 changes: 8 additions & 0 deletions httpie/input.py
Expand Up @@ -141,13 +141,21 @@ def parse_args(self, env, args=None, namespace=None):

# See if we're using curl style shorthand for localhost (:3000/foo)
shorthand = re.match(r'^:(?!:)(\d*)(/?.*)$', self.args.url)
base_url = re.match(r'^/.*$', self.args.url)
if shorthand:
port = shorthand.group(1)
rest = shorthand.group(2)
self.args.url = scheme + 'localhost'
if port:
self.args.url += ':' + port
self.args.url += rest
elif base_url:
if not self.env.base_url:
raise ParseError('No HTTPIE_BASEURL environment '
'variable configured.')
self.args.url = self.env.base_url.rstrip('/') + self.args.url
if not (self.args.url.startswith((HTTP, HTTPS))):
self.args.url = scheme + self.args.url
else:
self.args.url = scheme + self.args.url
self._process_auth()
Expand Down
21 changes: 20 additions & 1 deletion tests/test_cli.py
Expand Up @@ -7,7 +7,7 @@
from requests.exceptions import InvalidSchema

from httpie import input
from httpie.input import KeyValue, KeyValueArgType, DataDict
from httpie.input import KeyValue, KeyValueArgType, DataDict, ParseError
from httpie import ExitStatus
from httpie.cli import parser
from utils import TestEnvironment, http, HTTP_OK
Expand Down Expand Up @@ -175,6 +175,25 @@ def test_expand_localhost_shorthand_with_port_and_slash(self):
args = parser.parse_args(args=[':3000/'], env=TestEnvironment())
assert args.url == 'http://localhost:3000/'

def test_base_url_env_var(self):
env = TestEnvironment(base_url='http://example.com')
args = parser.parse_args(args=['/path'], env=env)
assert args.url == 'http://example.com/path'

def test_base_url_env_var_not_present(self):
with pytest.raises(ParseError):
args = parser.parse_args(args=['/path'], env=TestEnvironment())

def test_base_url_env_var_without_scheme(self):
env = TestEnvironment(base_url='example.com')
args = parser.parse_args(args=['/path'], env=env)
assert args.url == 'http://example.com/path'

def test_base_url_env_var_with_trailing_slash(self):
env = TestEnvironment(base_url='http://example.com/')
args = parser.parse_args(args=['/path'], env=env)
assert args.url == 'http://example.com/path'

def test_expand_localhost_shorthand_with_port_and_path(self):
args = parser.parse_args(args=[':3000/path'], env=TestEnvironment())
assert args.url == 'http://localhost:3000/path'
Expand Down

0 comments on commit 14af8bb

Please sign in to comment.