def test_binary_stdin(self, httpbin): with open(BIN_FILE_PATH, 'rb') as stdin: env = _TestEnvironment(stdin=stdin, stdin_isatty=False, stdout_isatty=False) r = http('--print=B', 'POST', httpbin.url + '/post', env=env) assert r == BIN_FILE_CONTENT
def test_ignore_stdin(self, httpbin): with open(FILE_PATH) as f: env = _TestEnvironment(stdin=f, stdin_isatty=False) r = http('--ignore-stdin', '--verbose', httpbin.url + '/get', env=env) assert HTTP_OK in r assert 'GET /get HTTP' in r, "Don't default to POST." assert FILE_CONTENT not in r, "Don't send stdin data."
def test_3xx_check_status_exits_3_and_stderr_when_stdout_redirected( httpbin): env = _TestEnvironment(stdout_isatty=False) r = http('--check-status', '--headers', 'GET', httpbin.url + '/status/301', env=env, error_exit_ok=True) assert '301 MOVED PERMANENTLY' in r assert r.exit_status == ExitStatus.ERROR_HTTP_3XX assert '301 moved permanently' in r.stderr.lower()
def test_request_body_from_file_by_path_no_field_name_allowed( self, httpbin): env = _TestEnvironment(stdin_isatty=True) r = http('POST', httpbin.url + '/post', 'field-name@' + FILE_PATH_ARG, env=env, error_exit_ok=True) assert 'perhaps you meant --form?' in r.stderr
def test_binary_file_form(self, httpbin): env = _TestEnvironment(stdin_isatty=True, stdout_isatty=False) r = http('--print=B', '--form', 'POST', httpbin.url + '/post', 'test@' + BIN_FILE_PATH_ARG, env=env) assert bytes(BIN_FILE_CONTENT) in bytes(r)
def test_actual_download(self, httpbin_both, httpbin): robots_txt = '/robots.txt' body = urlopen(httpbin + robots_txt).read().decode() env = _TestEnvironment(stdin_isatty=True, stdout_isatty=False) r = http('--download', httpbin_both.url + robots_txt, env=env) assert 'Downloading' in r.stderr assert '[K' in r.stderr assert 'Done' in r.stderr assert body == r
def test_only_username_in_url(url): """ https://github.com/jakubroztocil/httpie/issues/242 """ args = httpie.cli.parser.parse_args(args=[url], env=_TestEnvironment()) assert args.auth assert args.auth.username == 'username' assert args.auth.password == ''
def test_force_pretty(self, httpbin): env = _TestEnvironment(stdout_isatty=False, colors=256) r = http( '--pretty=all', 'GET', httpbin.url + '/get', env=env, ) assert COLOR in r
def test_request_body_from_file_by_path_no_data_items_allowed( self, httpbin): env = _TestEnvironment(stdin_isatty=False) r = http('POST', httpbin.url + '/post', '@' + FILE_PATH_ARG, 'foo=bar', env=env, error_exit_ok=True) assert 'cannot be mixed' in r.stderr
def test_binary_file_path(self, httpbin): env = _TestEnvironment(stdin_isatty=True, stdout_isatty=False) r = http( '--print=B', 'POST', httpbin.url + '/post', '@' + BIN_FILE_PATH_ARG, env=env, ) assert r == BIN_FILE_CONTENT
def env(self): """ Return an environment. Each environment created withing a test method will share the same config_dir. It is necessary for session files being reused. """ return _TestEnvironment(config_dir=self.config_dir)
def test_format_option(self, httpbin): env = _TestEnvironment(colors=256) r = http('--print=B', '--pretty=format', 'GET', httpbin.url + '/get', 'a=b', env=env) # Tests that the JSON data is formatted. assert r.strip().count('\n') == 2 assert COLOR not in r
def test_encoded_stream(httpbin): """Test that --stream works with non-prettified redirected terminal output.""" with open(BIN_FILE_PATH, 'rb') as f: env = _TestEnvironment(stdin=f, stdin_isatty=False) r = http('--pretty=none', '--stream', '--verbose', 'GET', httpbin.url + '/get', env=env) assert BINARY_SUPPRESSED_NOTICE.decode() in r
def test_subtype_based_pygments_lexer_match(self, httpbin): """Test that media subtype is used if type/subtype doesn't match any lexer. """ env = _TestEnvironment(colors=256) r = http('--print=B', '--pretty=all', httpbin.url + '/post', 'Content-Type:text/foo+json', 'a=b', env=env) assert COLOR in r
def test_output_file_pretty_not_allowed_on_windows(self, httpbin): env = _TestEnvironment(is_windows=True) output_file = os.path.join( tempfile.gettempdir(), self.test_output_file_pretty_not_allowed_on_windows.__name__) r = http('--output', output_file, '--pretty=all', 'GET', httpbin.url + '/get', env=env, error_exit_ok=True) assert 'Only terminal output can be colorized on Windows' in r.stderr
def test_guess_when_method_not_set(self): self.parser.args = argparse.Namespace() self.parser.args.method = None self.parser.args.url = 'http://example.com/' self.parser.args.items = [] self.parser.args.ignore_stdin = False self.parser.env = _TestEnvironment() self.parser._guess_method() assert self.parser.args.method == 'GET' assert self.parser.args.url == 'http://example.com/' assert self.parser.args.items == []
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']
def test_redirected_stream(httpbin): """Test that --stream works with non-prettified redirected terminal output.""" with open(BIN_FILE_PATH, 'rb') as f: env = _TestEnvironment(stdout_isatty=False, stdin_isatty=False, stdin=f) r = http('--pretty=none', '--stream', '--verbose', 'GET', httpbin.url + '/get', env=env) assert BIN_FILE_CONTENT in r
def test_output_option(httpbin, stdout_isatty): output_filename = os.path.join(gettempdir(), test_output_option.__name__) url = httpbin + '/robots.txt' r = http('--output', output_filename, url, env=_TestEnvironment(stdout_isatty=stdout_isatty)) assert r == '' expected_body = urlopen(url).read().decode() with open(output_filename, 'r') as f: actual_body = f.read() assert actual_body == expected_body
def test_guess_when_method_set_but_invalid_and_data_field(self): self.parser.args = argparse.Namespace() self.parser.args.method = 'http://example.com/' self.parser.args.url = 'data=field' self.parser.args.items = [] self.parser.args.ignore_stdin = False self.parser.env = _TestEnvironment() self.parser._guess_method() assert self.parser.args.method == 'POST' assert self.parser.args.url == 'http://example.com/' assert self.parser.args.items == [ KeyValue(key='data', value='field', sep='=', orig='data=field') ]
def test_guess_when_method_set_but_invalid_and_item_exists(self): self.parser.args = argparse.Namespace() self.parser.args.method = 'http://example.com/' self.parser.args.url = 'new_item=a' self.parser.args.items = [ KeyValue( key='old_item', value='b', sep='=', orig='old_item=b') ] self.parser.args.ignore_stdin = False self.parser.env = _TestEnvironment() self.parser._guess_method() assert self.parser.args.items, [ KeyValue(key='new_item', value='a', sep='=', orig='new_item=a'), KeyValue( key='old_item', value='b', sep='=', orig='old_item=b'), ]
def test_guess_when_method_set_but_invalid_and_header_field(self): self.parser.args = argparse.Namespace() self.parser.args.method = 'http://example.com/' self.parser.args.url = 'test:header' self.parser.args.items = [] self.parser.args.ignore_stdin = False self.parser.env = _TestEnvironment() self.parser._guess_method() assert self.parser.args.method == 'GET' assert self.parser.args.url == 'http://example.com/' assert self.parser.args.items, [ KeyValue(key='test', value='header', sep=':', orig='test:header') ]
def test_binary_suppresses_when_not_terminal_but_pretty(self): env = _TestEnvironment(stdin_isatty=True, stdout_isatty=False) r = http('--pretty=all', 'GET', self.url, env=env) assert BINARY_SUPPRESSED_NOTICE.decode() in r
def test_print_only_body_when_stdout_redirected_by_default(self, httpbin): env = _TestEnvironment(stdin_isatty=True, stdout_isatty=False) r = http('GET', httpbin.url + '/get', env=env) assert 'HTTP/' not in r
def test_expand_localhost_shorthand_with_slash(self): args = parser.parse_args(args=[':/'], env=_TestEnvironment()) assert args.url == 'http://localhost/'
def test_dont_expand_full_ipv6_as_shorthand(self): args = parser.parse_args( args=['0000:0000:0000:0000:0000:0000:0000:0001'], env=_TestEnvironment() ) assert args.url == 'http://0000:0000:0000:0000:0000:0000:0000:0001'
def test_dont_expand_longer_ipv6_as_shorthand(self): args = parser.parse_args( args=['::ffff:c000:0280'], env=_TestEnvironment() ) assert args.url == 'http://::ffff:c000:0280'
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'
def test_binary_included_and_correct_when_suitable(self): env = _TestEnvironment(stdin_isatty=True, stdout_isatty=False) r = http('GET', self.url, env=env) assert r == self.bindata
def test_pretty_enabled_by_default(self, httpbin): env = _TestEnvironment(colors=256) r = http('GET', httpbin.url + '/get', env=env) assert COLOR in r