def test_binary_suppresses_when_not_terminal_but_pretty(self, httpbin): env = MockEnvironment(stdin_isatty=True, stdout_isatty=False) r = http('--pretty=all', 'GET', httpbin + '/bytes/1024?seed=1', env=env) assert BINARY_SUPPRESSED_NOTICE.decode() in r
def test_raw_body(): r = http('--print=B', '--offline', 'pie.dev', 'AAA=BBB', env=MockEnvironment(stdout_isatty=False)) assert_output_matches(r, RAW_BODY)
def test_quiet_with_output_redirection(self, httpbin, with_download): url = httpbin + '/robots.txt' output_path = Path('output.txt') env = MockEnvironment() orig_cwd = os.getcwd() output = requests.get(url).text extra_args = ['--download'] if with_download else [] with tempfile.TemporaryDirectory() as tmp_dirname: os.chdir(tmp_dirname) try: assert os.listdir('.') == [] r = http('--quiet', '--output', str(output_path), *extra_args, url, env=env) assert os.listdir('.') == [str(output_path)] assert r == '' assert r.stderr == '' assert env.stderr is env.devnull if with_download: assert env.stdout is env.devnull else: assert env.stdout is not env.devnull # --output swaps stdout. assert output_path.read_text() == output finally: os.chdir(orig_cwd)
def test_raw_exchange(httpbin): r = http('--verbose', httpbin + '/post', 'a=b', env=MockEnvironment(stdout_isatty=False)) assert HTTP_OK in r assert_output_matches(r, RAW_EXCHANGE)
def test_implicit_POST_stdin(self, httpbin): env = MockEnvironment( stdin_isatty=False, stdin=BytesIO(FILE_PATH.read_bytes()) ) r = http('--form', httpbin.url + '/post', env=env) assert HTTP_OK in r
def test_binary_stdin(self, httpbin): with open(BIN_FILE_PATH, 'rb') as stdin: env = MockEnvironment(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_quiet_with_explicit_output_options(self, httpbin, argument_name): env = MockEnvironment(stdin_isatty=True, stdout_isatty=True) r = http('--quiet', argument_name, httpbin.url + '/get', env=env) assert env.stdout is env.devnull assert env.stderr is env.devnull assert r == '' assert r.stderr == ''
def test_quiet_with_check_status_non_zero_pipe(self, httpbin): r = http('--quiet', '--check-status', httpbin + '/status/500', tolerate_error_exit_status=True, env=MockEnvironment(stdout_isatty=False)) assert 'http: warning: HTTP 500' in r.stderr
def test_config_dir_not_writeable(httpbin): r = http(httpbin + '/get', env=MockEnvironment( config_dir='/', create_temp_config_dir=False, )) assert HTTP_OK in r
def test_POST_stdin(httpbin_both): env = MockEnvironment( stdin=StdinBytesIO(FILE_PATH.read_bytes()), stdin_isatty=False, ) r = http('--form', 'POST', httpbin_both + '/post', env=env) assert HTTP_OK in r assert FILE_CONTENT in r
def test_format_option(self, httpbin): env = MockEnvironment(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_ignore_stdin(self, httpbin): with open(FILE_PATH) as f: env = MockEnvironment(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_actual_download(self, httpbin_both, httpbin): robots_txt = '/robots.txt' body = urlopen(httpbin + robots_txt).read().decode() env = MockEnvironment(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_force_pretty(self, httpbin): env = MockEnvironment(stdout_isatty=False, colors=256) r = http( '--pretty=all', 'GET', httpbin.url + '/get', env=env, ) assert COLOR in r
def test_only_username_in_url(url): """ https://github.com/jakubroztocil/httpie/issues/242 """ args = httpie.cli.parser.parse_args(args=[url], env=MockEnvironment()) assert args.auth assert args.auth.username == 'username' assert args.auth.password == ''
def test_config_file_not_valid(httpbin): env = MockEnvironment() env.create_temp_config_dir() with (env.config_dir / Config.FILENAME).open('w') as f: f.write('{invalid json}') r = http(httpbin + '/get', env=env) assert HTTP_OK in r assert 'http: warning' in r.stderr assert 'invalid config file' in r.stderr
def test_raw_headers_and_body(): r = http( '--print=HB', '--offline', 'pie.dev', 'AAA=BBB', env=MockEnvironment(stdout_isatty=False), ) assert_output_matches(r, RAW_REQUEST)
def test_subtype_based_pygments_lexer_match(self, httpbin): """Test that media subtype is used if type/subtype doesn't match any lexer. """ env = MockEnvironment(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_3xx_check_status_exits_3_and_stderr_when_stdout_redirected( httpbin): env = MockEnvironment(stdout_isatty=False) r = http('--check-status', '--headers', 'GET', httpbin.url + '/status/301', env=env, tolerate_error_exit_status=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 = MockEnvironment(stdin_isatty=True) r = http('POST', httpbin.url + '/post', 'field-name@' + FILE_PATH_ARG, env=env, tolerate_error_exit_status=True) assert 'perhaps you meant --form?' in r.stderr
def test_binary_file_form(self, httpbin): env = MockEnvironment(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 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 MockEnvironment(config_dir=self.config_dir)
def test_request_body_from_file_by_path_no_data_items_allowed( self, httpbin): env = MockEnvironment(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 = MockEnvironment(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 test_redirected_headers_multipart_no_separator(): r = http( '--print=HB', '--multipart', '--offline', 'pie.dev', 'AAA=BBB', env=MockEnvironment(stdout_isatty=False), ) assert_output_matches(r, RAW_REQUEST)
def test_quiet(self, httpbin, argument_name): env = MockEnvironment(stdin_isatty=True, stdout_isatty=True, devnull=io.BytesIO()) r = http(argument_name, 'GET', httpbin.url + '/get', env=env) assert env.stdout is env.devnull assert env.stderr is env.devnull assert HTTP_OK in r.devnull assert r == '' assert r.stderr == ''
def test_config_file_inaccessible(httpbin): env = MockEnvironment() env.create_temp_config_dir() config_path = env.config_dir / Config.FILENAME assert not config_path.exists() config_path.touch(0o000) assert config_path.exists() r = http(httpbin + '/get', env=env) assert HTTP_OK in r assert 'http: warning' in r.stderr assert 'cannot read config file' in r.stderr
def test_chunked_stdin(): r = http('--verbose', '--chunked', HTTPBIN_WITH_CHUNKED_SUPPORT + '/post', env=MockEnvironment( stdin=StdinBytesIO(FILE_PATH.read_bytes()), stdin_isatty=False, )) assert HTTP_OK in r assert 'Transfer-Encoding: chunked' in r assert r.count(FILE_CONTENT) == 2
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.request_items = [] self.parser.args.ignore_stdin = False self.parser.env = MockEnvironment() self.parser._guess_method() assert self.parser.args.method == 'GET' assert self.parser.args.url == 'http://example.com/' assert self.parser.args.request_items == []
def test_verbose_redirected_stdout_separator(httpbin): """ <https://github.com/httpie/httpie/issues/1006> """ r = http( '-v', httpbin.url + '/post', 'a=b', env=MockEnvironment(stdout_isatty=False), ) assert '}HTTP/' not in r