def test_session_read_only(self): # Get a response from the original session. r2 = http("--session=test", "GET", httpbin("/get"), env=self.env()) assert HTTP_OK in r2 # Make a request modifying the session data but # with --session-read-only. r3 = http( "--follow", "--session-read-only=test", "--auth=username:password2", "GET", httpbin("/cookies/set?hello=world2"), "Hello:World2", env=self.env(), ) assert HTTP_OK in r3 # Get a response from the updated session. r4 = http("--session=test", "GET", httpbin("/get"), env=self.env()) assert HTTP_OK in r4 # Origin can differ on Travis. del r2.json["origin"], r4.json["origin"] # Different for each request. del r2.json["headers"]["X-Request-Id"] del r4.json["headers"]["X-Request-Id"] # Should be the same as before r3. assert r2.json == r4.json
def test_session_read_only(self, httpbin): self.start_session(httpbin) # Get a response from the original session. r2 = http('--session=test', 'GET', httpbin.url + '/get', env=self.env()) assert HTTP_OK in r2 # Make a request modifying the session data but # with --session-read-only. r3 = http('--follow', '--session-read-only=test', '--auth=username:password2', 'GET', httpbin.url + '/cookies/set?hello=world2', 'Hello:World2', env=self.env()) assert HTTP_OK in r3 # Get a response from the updated session. r4 = http('--session=test', 'GET', httpbin.url + '/get', env=self.env()) assert HTTP_OK in r4 # Origin can differ on Travis. del r2.json['origin'], r4.json['origin'] # Different for each request. # Should be the same as before r3. assert r2.json == r4.json
def test_session_by_path(self): session_path = os.path.join(self.config_dir, "session-by-path.json") r1 = http("--session=" + session_path, "GET", httpbin("/get"), "Foo:Bar", env=self.env()) assert HTTP_OK in r1 r2 = http("--session=" + session_path, "GET", httpbin("/get"), env=self.env()) assert HTTP_OK in r2 assert r2.json["headers"]["Foo"] == "Bar"
def test_session_default_header_value_overwritten(self): # https://github.com/jakubroztocil/httpie/issues/180 r1 = http("--session=test", httpbin("/headers"), "User-Agent:custom", env=self.env()) assert HTTP_OK in r1 assert r1.json["headers"]["User-Agent"] == "custom" r2 = http("--session=test", httpbin("/headers"), env=self.env()) assert HTTP_OK in r2 assert r2.json["headers"]["User-Agent"] == "custom"
def test_session_unicode(self): r1 = http( "--session=test", "--auth", u"test:" + UNICODE, "GET", httpbin("/get"), u"Test:%s" % UNICODE, env=self.env() ) assert HTTP_OK in r1 r2 = http("--session=test", "GET", httpbin("/get"), env=self.env()) assert HTTP_OK in r2 assert r2.json["headers"]["Authorization"] == HTTPBasicAuth.make_header(u"test", UNICODE) assert r2.json["headers"]["Test"] == UNICODE
def test_session_by_path(self): session_path = os.path.join(self.config_dir, 'session-by-path.json') r1 = http('--session=' + session_path, 'GET', httpbin('/get'), 'Foo:Bar', env=self.env()) assert HTTP_OK in r1 r2 = http('--session=' + session_path, 'GET', httpbin('/get'), env=self.env()) assert HTTP_OK in r2 assert r2.json['headers']['Foo'] == 'Bar'
def test_session_default_header_value_overwritten(self): # https://github.com/jakubroztocil/httpie/issues/180 r1 = http('--session=test', httpbin('/headers'), 'User-Agent:custom', env=self.env()) assert HTTP_OK in r1 assert r1.json['headers']['User-Agent'] == 'custom' r2 = http('--session=test', httpbin('/headers'), env=self.env()) assert HTTP_OK in r2 assert r2.json['headers']['User-Agent'] == 'custom'
def test_download_in_session(self, httpbin): # https://github.com/jakubroztocil/httpie/issues/412 self.start_session(httpbin) cwd = os.getcwd() os.chdir(gettempdir()) try: http('--session=test', '--download', httpbin.url + '/get', env=self.env()) finally: os.chdir(cwd)
def test_session_ignored_header_prefixes(self): r1 = http('--session=test', 'GET', httpbin('/get'), 'Content-Type: text/plain', 'If-Unmodified-Since: Sat, 29 Oct 1994 19:43:31 GMT', env=self.env()) assert HTTP_OK in r1 r2 = http('--session=test', 'GET', httpbin('/get'), env=self.env()) assert HTTP_OK in r2 assert 'Content-Type' not in r2.json['headers'] assert 'If-Unmodified-Since' not in r2.json['headers']
def test_session_unicode(self): r1 = http('--session=test', '--auth', u'test:' + UNICODE, 'GET', httpbin('/get'), u'Test:%s' % UNICODE, env=self.env()) assert HTTP_OK in r1 r2 = http('--session=test', 'GET', httpbin('/get'), env=self.env()) assert HTTP_OK in r2 assert (r2.json['headers']['Authorization'] == HTTPBasicAuth.make_header(u'test', UNICODE)) assert r2.json['headers']['Test'] == UNICODE
def test_session_ignored_header_prefixes(self, httpbin): self.start_session(httpbin) r1 = http('--session=test', 'GET', httpbin.url + '/get', 'Content-Type: text/plain', 'If-Unmodified-Since: Sat, 29 Oct 1994 19:43:31 GMT', env=self.env()) assert HTTP_OK in r1 r2 = http('--session=test', 'GET', httpbin.url + '/get', env=self.env()) assert HTTP_OK in r2 assert no_content_type(r2.json['headers']) assert 'If-Unmodified-Since' not in r2.json['headers']
def test_session_default_header_value_overwritten(self, httpbin): self.start_session(httpbin) # https://github.com/jkbrzt/httpie/issues/180 r1 = http('--session=test', httpbin.url + '/headers', 'User-Agent:custom', env=self.env()) assert HTTP_OK in r1 assert r1.json['headers']['User-Agent'] == 'custom' r2 = http('--session=test', httpbin.url + '/headers', env=self.env()) assert HTTP_OK in r2 assert r2.json['headers']['User-Agent'] == 'custom'
def test_session_ignored_header_prefixes(self): r1 = http( "--session=test", "GET", httpbin("/get"), "Content-Type: text/plain", "If-Unmodified-Since: Sat, 29 Oct 1994 19:43:31 GMT", env=self.env(), ) assert HTTP_OK in r1 r2 = http("--session=test", "GET", httpbin("/get"), env=self.env()) assert HTTP_OK in r2 assert "Content-Type" not in r2.json["headers"] assert "If-Unmodified-Since" not in r2.json["headers"]
def test_ignore_stdin(self): with open(FILE_PATH) as f: env = TestEnvironment(stdin=f, stdin_isatty=False) r = http('--ignore-stdin', '--verbose', httpbin('/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_cert_file_not_found(self, httpbin_secure): r = http(httpbin_secure + '/get', '--verify', CA_BUNDLE, '--cert', '/__not_found__', error_exit_ok=True) assert r.exit_status == ExitStatus.ERROR assert 'No such file or directory' in r.stderr
def test_POST_explicit_JSON_auto_JSON_accept(self, httpbin): r = http('--json', 'POST', httpbin.url + '/post') assert HTTP_OK in r assert r.json['headers']['Accept'] == JSON_ACCEPT # Make sure Content-Type gets set even with no data. # https://github.com/jakubroztocil/httpie/issues/137 assert 'application/json' in r.json['headers']['Content-Type']
def test_query_string_params_in_url(self): r = http('--print=Hhb', 'GET', httpbin('/get?a=1&b=2')) path = '/get?a=1&b=2' url = httpbin(path) assert HTTP_OK in r assert 'GET %s HTTP/1.1' % path in r assert '"url": "%s"' % url in r
def test_GET_explicit_JSON_explicit_headers(self, httpbin): r = http('--json', 'GET', httpbin.url + '/headers', 'Accept:application/xml', 'Content-Type:application/xml') assert HTTP_OK in r assert '"Accept": "application/xml"' in r assert '"Content-Type": "application/xml"' in r
def test_3xx_check_status_redirects_allowed_exits_0(httpbin): r = http('--check-status', '--follow', 'GET', httpbin.url + '/status/301', error_exit_ok=True) # The redirect will be followed so 200 is expected. assert HTTP_OK in r assert r.exit_status == ExitStatus.OK
def test_4xx_check_status_exits_4(httpbin): r = http('--check-status', 'GET', httpbin.url + '/status/401', error_exit_ok=True) assert '401 UNAUTHORIZED' in r assert r.exit_status == ExitStatus.ERROR_HTTP_4XX # Also stderr should be empty since stdout isn't redirected. assert not r.stderr
def test_4xx_check_status_exits_4(self): r = http('--check-status', 'GET', httpbin('/status/401'), error_exit_ok=True) assert 'HTTP/1.1 401' in r assert r.exit_status == ExitStatus.ERROR_HTTP_4XX # Also stderr should be empty since stdout isn't redirected. assert not r.stderr
def test_credentials_in_url_auth_flag_has_priority(self): """When credentials are passed in URL and via -a at the same time, then the ones from -a are used.""" url = httpbin('/basic-auth/user/password', auth='user:wrong') r = http('--auth=user:password', 'GET', url) assert HTTP_OK in r assert r.json == {'authenticated': True, 'user': '******'}
def test_format_option(self): env = TestEnvironment(colors=256) r = http('--print=B', '--pretty=format', 'GET', httpbin('/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 = 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(self): env = TestEnvironment(stdout_isatty=False) r = http('--check-status', '--headers', 'GET', httpbin('/status/301'), env=env, error_exit_ok=True) assert 'HTTP/1.1 301' in r assert r.exit_status == ExitStatus.ERROR_HTTP_3XX assert '301 moved permanently' in r.stderr.lower()
def test_query_string_params_items(self, httpbin): r = http('--print=Hhb', 'GET', httpbin.url + '/get', 'a==1') path = '/get?a=1' url = httpbin.url + path assert HTTP_OK in r assert 'GET %s HTTP/1.1' % path in r assert '"url": "%s"' % url in r
def test_query_string_params_in_url(self, httpbin): r = http("--print=Hhb", "GET", httpbin.url + "/get?a=1&b=2") path = "/get?a=1&b=2" url = httpbin.url + path assert HTTP_OK in r assert "GET %s HTTP/1.1" % path in r assert '"url": "%s"' % url in r
def test_query_string_params_in_url_and_items_with_duplicates(self, httpbin): r = http("--print=Hhb", "GET", httpbin.url + "/get?a=1&a=1", "a==1", "a==1") path = "/get?a=1&a=1&a=1&a=1" url = httpbin.url + path assert HTTP_OK in r assert "GET %s HTTP/1.1" % path in r assert '"url": "%s"' % url in r
def test_session_created_and_reused(self): # Verify that the session created in setup_method() has been used. r2 = http("--session=test", "GET", httpbin("/get"), env=self.env()) assert HTTP_OK in r2 assert r2.json["headers"]["Hello"] == "World" assert r2.json["headers"]["Cookie"] == "hello=world" assert "Basic " in r2.json["headers"]["Authorization"]
def test_session_created_and_reused(self): # Verify that the session created in setup_method() has been used. r2 = http('--session=test', 'GET', httpbin('/get'), env=self.env()) assert HTTP_OK in r2 assert r2.json['headers']['Hello'] == 'World' assert r2.json['headers']['Cookie'] == 'hello=world' assert 'Basic ' in r2.json['headers']['Authorization']
def test_headers(httpbin_both): r = http('GET', httpbin_both + '/headers', 'Foo:bar') assert HTTP_OK in r assert '"User-Agent": "HTTPie' in r, r assert '"Foo": "bar"' in r
def test_headers_unset(httpbin_both): r = http('GET', httpbin_both + '/headers') assert 'Accept' in r.json['headers'] # default Accept present r = http('GET', httpbin_both + '/headers', 'Accept:') assert 'Accept' not in r.json['headers'] # default Accept unset
def test_debug(): r = http('--debug') assert r.exit_status == ExitStatus.SUCCESS assert 'HTTPie %s' % httpie.__version__ in r.stderr
def test_cert_file_invalid(self, httpbin_secure): with pytest.raises(SSLError): http(httpbin_secure + '/get', '--cert', __file__)
def test_verify_custom_ca_bundle_invalid_path(self, httpbin_secure): with pytest.raises(SSLError): http(httpbin_secure.url + '/get', '--verify', '/__not_found__')
def test_headers_empty_value(httpbin_both): r = http('GET', httpbin_both + '/headers') assert r.json['headers']['Accept'] # default Accept has value r = http('GET', httpbin_both + '/headers', 'Accept;') assert r.json['headers']['Accept'] == '' # Accept has no value
def test_DELETE(httpbin_both): r = http('DELETE', httpbin_both + '/delete') assert HTTP_OK in r
def test_PUT(httpbin_both): r = http('PUT', httpbin_both + '/put', 'foo=bar') assert HTTP_OK in r assert r.json['json']['foo'] == 'bar'
def test_force_ugly(self, httpbin): r = http('--pretty=none', 'GET', httpbin.url + '/get') assert COLOR not in r
def test_verify_custom_ca_bundle_invalid_bundle(self, httpbin_secure): with pytest.raises(SSLError): http(httpbin_secure.url + '/get', '--verify', __file__)
def test_unicode_url(httpbin): r = http(httpbin.url + u'/get?test=' + UNICODE) assert HTTP_OK in r assert r.json['args'] == {'test': UNICODE}
def test_verify_custom_ca_bundle_path(self, httpbin_secure_untrusted): r = http(httpbin_secure_untrusted + '/get', '--verify', CA_BUNDLE) assert HTTP_OK in r
def test_verify_no_OK(self, httpbin_secure): r = http(httpbin_secure.url + '/get', '--verify=no') assert HTTP_OK in r
def test_self_signed_server_cert_by_default_raises_ssl_error( self, httpbin_secure_untrusted): with pytest.raises(SSLError): http(httpbin_secure_untrusted.url + '/get')
def test_json_input_preserve_order(httpbin_both): r = http('PATCH', httpbin_both + '/patch', 'order:={"map":{"1":"first","2":"second"}}') assert HTTP_OK in r assert r.json['data'] == \ '{"order": {"map": {"1": "first", "2": "second"}}}'
def test_verify_false_OK(self, httpbin_secure, verify_value): r = http(httpbin_secure.url + '/get', '--verify', verify_value) assert HTTP_OK in r
def test_GET(httpbin_both): r = http('GET', httpbin_both + '/get') assert HTTP_OK in r
def test_cert_ok_but_missing_key(self, httpbin_secure): with pytest.raises(SSLError): http(httpbin_secure + '/get', '--cert', CLIENT_CERT)
def test_headers_empty_value_with_value_gives_error(httpbin): with pytest.raises(ParseError): http('GET', httpbin + '/headers', 'Accept;SYNTAX_ERROR')
def test_cert_and_key(self, httpbin_secure): r = http(httpbin_secure + '/get', '--cert', CLIENT_CERT, '--cert-key', CLIENT_KEY) assert HTTP_OK in r
def test_POST_stdin(httpbin_both): with open(FILE_PATH) as f: env = MockEnvironment(stdin=f, stdin_isatty=False) r = http('--form', 'POST', httpbin_both + '/post', env=env) assert HTTP_OK in r assert FILE_CONTENT in r
def test_cert_pem(self, httpbin_secure): r = http(httpbin_secure + '/get', '--cert', CLIENT_PEM) assert HTTP_OK in r
def test_POST_form(httpbin_both): r = http('--form', 'POST', httpbin_both + '/post', 'foo=bar') assert HTTP_OK in r assert '"foo": "bar"' in r
def test_POST_file(httpbin_both): r = http('--form', 'POST', httpbin_both + '/post', 'file@' + FILE_PATH) assert HTTP_OK in r assert FILE_CONTENT in r
def test_version(): r = http('--version', tolerate_error_exit_status=True) assert r.exit_status == ExitStatus.SUCCESS # FIXME: py3 has version in stdout, py2 in stderr assert httpie.__version__ == r.strip()
def test_POST_form_multiple_values(httpbin_both): r = http('--form', 'POST', httpbin_both + '/post', 'foo=bar', 'foo=baz') assert HTTP_OK in r assert r.json['form'] == {'foo': ['bar', 'baz']}
def test_help(): r = http('--help', tolerate_error_exit_status=True) assert r.exit_status == ExitStatus.SUCCESS assert 'https://github.com/jakubroztocil/httpie/issues' in r
def test_POST_JSON_data(httpbin_both): r = http('POST', httpbin_both + '/post', 'foo=bar') assert HTTP_OK in r assert r.json['json']['foo'] == 'bar'
def test_pretty_enabled_by_default_unless_stdout_redirected(self, httpbin): r = http('GET', httpbin.url + '/get') assert COLOR not in r
def test_unset_host_header(httpbin_both): r = http('GET', httpbin_both + '/headers') assert 'Host' in r.json['headers'] # default Host present r = http('GET', httpbin_both + '/headers', 'Host:') assert 'Host' not in r.json['headers'] # default Host unset