diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 2c11f058..03c90cee 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -18,6 +18,8 @@ This project adheres to `Semantic Versioning `_. * Added ``--max-redirects`` (default 30) * Added ``-A`` as short name for ``--auth-type`` * Added ``-F`` as short name for ``--follow`` +* Removed the ``"implicit_content_type" config option + (use ``"default_options": ["--form"]`` instead) * Redirected ``stdout`` doesn't trigger an error anymore when ``--output FILE`` is set * Changed the default ``--style`` back to ``solarized`` for better support diff --git a/README.rst b/README.rst index 97fc2dbb..c66d0f5f 100644 --- a/README.rst +++ b/README.rst @@ -1230,31 +1230,32 @@ Config HTTPie uses a simple configuration file that contains a JSON object with the following keys: -========================= ================================================= -``__meta__`` HTTPie automatically stores some metadata here. - Do not change. -``implicit_content_type`` A ``String`` specifying the implicit content type - for request data. The default value for this - option is ``json`` and can be changed to - ``form``. +``__meta__`` +------------ +HTTPie automatically stores some of its metadata here. Do not change. -``default_options`` An ``Array`` (by default empty) of options - that should be applied to every request. - For instance, you can use this option to change - the default style and output options: - ``"default_options": ["--style=fruity", "--body"]`` +``default_options`` +------------------- + +An ``Array`` (by default empty) of default options that should be applied to +every invocation of HTTPie. + +For instance, you can use this option to change the default style and output +options: ``"default_options": ["--style=fruity", "--body"]`` + +Another useful default option is ``"--session=default"`` to make HTTPie always +use `sessions`_. + +Or you could change the implicit request content type from JSON to form by +adding the ``--form``. + +Default options from config file can be unset for a particular invocation via +``--no-OPTION`` arguments passed on the command line (e.g., ``--no-style`` +or ``--no-session``). - Another useful default option is - ``"--session=default"`` to make HTTPie always - use `sessions`_. - Default options from config file can be unset - for a particular invocation via - ``--no-OPTION`` arguments passed on the - command line (e.g., ``--no-style`` - or ``--no-session``). ========================= ================================================= The default location of the configuration file is ``~/.httpie/config.json`` diff --git a/httpie/config.py b/httpie/config.py index 64f72b2b..f7282387 100644 --- a/httpie/config.py +++ b/httpie/config.py @@ -84,7 +84,6 @@ class Config(BaseConfigDict): about = 'HTTPie configuration file' DEFAULTS = { - 'implicit_content_type': 'json', 'default_options': [] } @@ -93,5 +92,21 @@ class Config(BaseConfigDict): self.update(self.DEFAULTS) self.directory = directory + def load(self): + super(Config, self).load() + self._migrate_implicit_content_type() + def _get_path(self): return os.path.join(self.directory, self.name + '.json') + + def _migrate_implicit_content_type(self): + """Migrate the removed implicit_content_type config option""" + try: + implicit_content_type = self.pop('implicit_content_type') + except KeyError: + pass + else: + if implicit_content_type == 'form': + self['default_options'].insert(0, '--form') + self.save() + self.load() diff --git a/httpie/input.py b/httpie/input.py index 96c7945a..c622b65b 100644 --- a/httpie/input.py +++ b/httpie/input.py @@ -142,7 +142,6 @@ class HTTPieArgumentParser(ArgumentParser): # Arguments processing and environment setup. self._apply_no_options(no_options) - self._apply_config() self._validate_download_options() self._setup_standard_streams() self._process_output_options() @@ -214,11 +213,6 @@ class HTTPieArgumentParser(ArgumentParser): self.env.stdout = self.args.output_file self.env.stdout_isatty = False - def _apply_config(self): - if (not self.args.json - and self.env.config.implicit_content_type == 'form'): - self.args.form = True - def _process_auth(self): """ If only a username provided via --auth, then ask for a password. diff --git a/tests/test_config.py b/tests/test_config.py new file mode 100644 index 00000000..c3091173 --- /dev/null +++ b/tests/test_config.py @@ -0,0 +1,33 @@ +from utils import TestEnvironment, http + + +def test_default_options(httpbin): + env = TestEnvironment() + env.config['default_options'] = ['--form'] + env.config.save() + r = http(httpbin.url + '/post', 'foo=bar', env=env) + assert r.json['form'] == {"foo": "bar"} + + +def test_default_options_overwrite(httpbin): + env = TestEnvironment() + env.config['default_options'] = ['--form'] + env.config.save() + r = http('--json', httpbin.url + '/post', 'foo=bar', env=env) + assert r.json['json'] == {"foo": "bar"} + + +def test_migrate_implicit_content_type(): + config = TestEnvironment().config + + config['implicit_content_type'] = 'json' + config.save() + config.load() + assert 'implicit_content_type' not in config + assert not config['default_options'] + + config['implicit_content_type'] = 'form' + config.save() + config.load() + assert 'implicit_content_type' not in config + assert config['default_options'] == ['--form'] diff --git a/tests/utils.py b/tests/utils.py index e90e7502..feede790 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -189,13 +189,14 @@ def http(*args, **kwargs): stderr = env.stderr args = list(args) - extra_args = [] - if '--debug' not in args: - if '--traceback' not in args: - extra_args.append('--traceback') - if not any('--timeout' in arg for arg in args): - extra_args.append('--timeout=3') - args = extra_args + args + args_with_config_defaults = args + env.config.default_options + add_to_args = [] + if '--debug' not in args_with_config_defaults: + if '--traceback' not in args_with_config_defaults: + add_to_args.append('--traceback') + if not any('--timeout' in arg for arg in args_with_config_defaults): + add_to_args.append('--timeout=3') + args = add_to_args + args def dump_stderr(): stderr.seek(0)