Permalink
Browse files

Run tests against local httpbin instance via pytest-httpbin.

  • Loading branch information...
1 parent 79329ed commit 2a72ae23d52ec2ca583a7b2324d69bd3f2421386 @jakubroztocil committed Jun 28, 2014
View
@@ -105,8 +105,13 @@ def headers(self):
if 'Host' not in self._orig.headers:
headers['Host'] = url.netloc.split('@')[-1]
- headers = ['%s: %s' % (name, value)
- for name, value in headers.items()]
+ headers = [
+ '%s: %s' % (
+ name,
+ value if isinstance(value, str) else value.decode('utf8')
+ )
+ for name, value in headers.items()
+ ]
headers.insert(0, request_line)
headers = '\r\n'.join(headers).strip()
View
@@ -2,4 +2,5 @@ tox
pytest
pytest-xdist
pytest-cov
+pytest-httpbin
docutils
View
@@ -26,6 +26,7 @@ def run_tests(self):
tests_require = [
'pytest',
+ 'pytest-httpbin',
]
View
@@ -2,42 +2,45 @@
import requests
import pytest
-from utils import http, httpbin, HTTP_OK
+from utils import http, add_auth, HTTP_OK
import httpie.input
class TestAuth:
- def test_basic_auth(self):
+ def test_basic_auth(self, httpbin):
r = http('--auth=user:password',
- 'GET', httpbin('/basic-auth/user/password'))
+ 'GET', httpbin.url + '/basic-auth/user/password')
assert HTTP_OK in r
assert r.json == {'authenticated': True, 'user': 'user'}
@pytest.mark.skipif(
requests.__version__ == '0.13.6',
reason='Redirects with prefetch=False are broken in Requests 0.13.6')
- def test_digest_auth(self):
+ def test_digest_auth(self, httpbin):
r = http('--auth-type=digest', '--auth=user:password',
- 'GET', httpbin('/digest-auth/auth/user/password'))
+ 'GET', httpbin.url + '/digest-auth/auth/user/password')
assert HTTP_OK in r
assert r.json == {'authenticated': True, 'user': 'user'}
- def test_password_prompt(self):
+ def test_password_prompt(self, httpbin):
httpie.input.AuthCredentials._getpass = lambda self, prompt: 'password'
- r = http('--auth', 'user', 'GET', httpbin('/basic-auth/user/password'))
+ r = http('--auth', 'user',
+ 'GET', httpbin.url + '/basic-auth/user/password')
assert HTTP_OK in r
assert r.json == {'authenticated': True, 'user': 'user'}
- def test_credentials_in_url(self):
- url = httpbin('/basic-auth/user/password', auth='user:password')
+ def test_credentials_in_url(self, httpbin):
+ url = add_auth(httpbin.url + '/basic-auth/user/password',
+ auth='user:password')
r = http('GET', url)
assert HTTP_OK in r
assert r.json == {'authenticated': True, 'user': 'user'}
- def test_credentials_in_url_auth_flag_has_priority(self):
+ def test_credentials_in_url_auth_flag_has_priority(self, httpbin):
"""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')
+ url = add_auth(httpbin.url + '/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': 'user'}
View
@@ -1,30 +1,30 @@
"""Tests for dealing with binary request and response data."""
from httpie.compat import urlopen
from httpie.output.streams import BINARY_SUPPRESSED_NOTICE
-from utils import TestEnvironment, http, httpbin
+from utils import TestEnvironment, http
from fixtures import BIN_FILE_PATH, BIN_FILE_CONTENT, BIN_FILE_PATH_ARG
class TestBinaryRequestData:
- def test_binary_stdin(self):
+ 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('/post'), env=env)
+ r = http('--print=B', 'POST', httpbin.url + '/post', env=env)
assert r == BIN_FILE_CONTENT
- def test_binary_file_path(self):
+ def test_binary_file_path(self, httpbin):
env = TestEnvironment(stdin_isatty=True, stdout_isatty=False)
- r = http('--print=B', 'POST', httpbin('/post'),
+ r = http('--print=B', 'POST', httpbin.url + '/post',
'@' + BIN_FILE_PATH_ARG, env=env, )
assert r == BIN_FILE_CONTENT
- def test_binary_file_form(self):
+ def test_binary_file_form(self, httpbin):
env = TestEnvironment(stdin_isatty=True, stdout_isatty=False)
- r = http('--print=B', '--form', 'POST', httpbin('/post'),
+ r = http('--print=B', '--form', 'POST', httpbin.url + '/post',
'test@' + BIN_FILE_PATH_ARG, env=env)
assert bytes(BIN_FILE_CONTENT) in bytes(r)
View
@@ -9,7 +9,7 @@
from httpie.input import KeyValue, KeyValueArgType
from httpie import ExitStatus
from httpie.cli import parser
-from utils import TestEnvironment, http, httpbin, HTTP_OK
+from utils import TestEnvironment, http, HTTP_OK
from fixtures import (
FILE_PATH_ARG, JSON_FILE_PATH_ARG,
JSON_FILE_CONTENT, FILE_CONTENT, FILE_PATH
@@ -93,27 +93,28 @@ def test_valid_items(self):
class TestQuerystring:
- def test_query_string_params_in_url(self):
- r = http('--print=Hhb', 'GET', httpbin('/get?a=1&b=2'))
+ 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(path)
+ 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_items(self):
- r = http('--print=Hhb', 'GET', httpbin('/get'), 'a==1', 'b==2')
- path = '/get?a=1&b=2'
- url = httpbin(path)
+ 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_and_items_with_duplicates(self):
- r = http('--print=Hhb', 'GET', httpbin('/get?a=1&a=1'),
- 'a==1', 'a==1', 'b==2')
- path = '/get?a=1&a=1&a=1&a=1&b=2'
- url = httpbin(path)
+ 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
@@ -257,29 +258,32 @@ def test_guess_when_method_set_but_invalid_and_item_exists(self):
class TestNoOptions:
- def test_valid_no_options(self):
- r = http('--verbose', '--no-verbose', 'GET', httpbin('/get'))
+
+ def test_valid_no_options(self, httpbin):
+ r = http('--verbose', '--no-verbose', 'GET', httpbin.url + '/get')
assert 'GET /get HTTP/1.1' not in r
- def test_invalid_no_options(self):
- r = http('--no-war', 'GET', httpbin('/get'),
+ def test_invalid_no_options(self, httpbin):
+ r = http('--no-war', 'GET', httpbin.url + '/get',
error_exit_ok=True)
assert r.exit_status == 1
assert 'unrecognized arguments: --no-war' in r.stderr
assert 'GET /get HTTP/1.1' not in r
class TestIgnoreStdin:
- def test_ignore_stdin(self):
+
+ 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('/get'), env=env)
+ 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_ignore_stdin_cannot_prompt_password(self):
- r = http('--ignore-stdin', '--auth=no-password', httpbin('/get'),
+ def test_ignore_stdin_cannot_prompt_password(self, httpbin):
+ r = http('--ignore-stdin', '--auth=no-password', httpbin.url + '/get',
error_exit_ok=True)
assert r.exit_status == ExitStatus.ERROR
assert 'because --ignore-stdin' in r.stderr
View
@@ -2,34 +2,34 @@
Tests for the provided defaults regarding HTTP method, and --json vs. --form.
"""
-from utils import TestEnvironment, http, httpbin, HTTP_OK
+from utils import TestEnvironment, http, HTTP_OK, no_content_type
from fixtures import FILE_PATH
class TestImplicitHTTPMethod:
- def test_implicit_GET(self):
- r = http(httpbin('/get'))
+ def test_implicit_GET(self, httpbin):
+ r = http(httpbin.url + '/get')
assert HTTP_OK in r
- def test_implicit_GET_with_headers(self):
- r = http(httpbin('/headers'), 'Foo:bar')
+ def test_implicit_GET_with_headers(self, httpbin):
+ r = http(httpbin.url + '/headers', 'Foo:bar')
assert HTTP_OK in r
assert r.json['headers']['Foo'] == 'bar'
- def test_implicit_POST_json(self):
- r = http(httpbin('/post'), 'hello=world')
+ def test_implicit_POST_json(self, httpbin):
+ r = http(httpbin.url + '/post', 'hello=world')
assert HTTP_OK in r
assert r.json['json'] == {'hello': 'world'}
- def test_implicit_POST_form(self):
- r = http('--form', httpbin('/post'), 'foo=bar')
+ def test_implicit_POST_form(self, httpbin):
+ r = http('--form', httpbin.url + '/post', 'foo=bar')
assert HTTP_OK in r
assert r.json['form'] == {'foo': 'bar'}
- def test_implicit_POST_stdin(self):
+ def test_implicit_POST_stdin(self, httpbin):
with open(FILE_PATH) as f:
env = TestEnvironment(stdin_isatty=False, stdin=f)
- r = http('--form', httpbin('/post'), env=env)
+ r = http('--form', httpbin.url + '/post', env=env)
assert HTTP_OK in r
@@ -41,66 +41,66 @@ class TestAutoContentTypeAndAcceptHeaders:
"""
- def test_GET_no_data_no_auto_headers(self):
+ def test_GET_no_data_no_auto_headers(self, httpbin):
# https://github.com/jakubroztocil/httpie/issues/62
- r = http('GET', httpbin('/headers'))
+ r = http('GET', httpbin.url + '/headers')
assert HTTP_OK in r
assert r.json['headers']['Accept'] == '*/*'
- assert 'Content-Type' not in r.json['headers']
+ assert no_content_type(r.json['headers'])
- def test_POST_no_data_no_auto_headers(self):
+ def test_POST_no_data_no_auto_headers(self, httpbin):
# JSON headers shouldn't be automatically set for POST with no data.
- r = http('POST', httpbin('/post'))
+ r = http('POST', httpbin.url + '/post')
assert HTTP_OK in r
assert '"Accept": "*/*"' in r
assert '"Content-Type": "application/json' not in r
- def test_POST_with_data_auto_JSON_headers(self):
- r = http('POST', httpbin('/post'), 'a=b')
+ def test_POST_with_data_auto_JSON_headers(self, httpbin):
+ r = http('POST', httpbin.url + '/post', 'a=b')
assert HTTP_OK in r
assert '"Accept": "application/json"' in r
assert '"Content-Type": "application/json; charset=utf-8' in r
- def test_GET_with_data_auto_JSON_headers(self):
+ def test_GET_with_data_auto_JSON_headers(self, httpbin):
# JSON headers should automatically be set also for GET with data.
- r = http('POST', httpbin('/post'), 'a=b')
+ r = http('POST', httpbin.url + '/post', 'a=b')
assert HTTP_OK in r
assert '"Accept": "application/json"' in r, r
assert '"Content-Type": "application/json; charset=utf-8' in r
- def test_POST_explicit_JSON_auto_JSON_accept(self):
- r = http('--json', 'POST', httpbin('/post'))
+ 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'] == 'application/json'
# 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_GET_explicit_JSON_explicit_headers(self):
- r = http('--json', 'GET', httpbin('/headers'),
+ 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_POST_form_auto_Content_Type(self):
- r = http('--form', 'POST', httpbin('/post'))
+ def test_POST_form_auto_Content_Type(self, httpbin):
+ r = http('--form', 'POST', httpbin.url + '/post')
assert HTTP_OK in r
assert '"Content-Type": "application/x-www-form-urlencoded' in r
- def test_POST_form_Content_Type_override(self):
- r = http('--form', 'POST', httpbin('/post'),
+ def test_POST_form_Content_Type_override(self, httpbin):
+ r = http('--form', 'POST', httpbin.url + '/post',
'Content-Type:application/xml')
assert HTTP_OK in r
assert '"Content-Type": "application/xml"' in r
- def test_print_only_body_when_stdout_redirected_by_default(self):
+ def test_print_only_body_when_stdout_redirected_by_default(self, httpbin):
env = TestEnvironment(stdin_isatty=True, stdout_isatty=False)
- r = http('GET', httpbin('/get'), env=env)
+ r = http('GET', httpbin.url + '/get', env=env)
assert 'HTTP/' not in r
- def test_print_overridable_when_stdout_redirected(self):
+ def test_print_overridable_when_stdout_redirected(self, httpbin):
env = TestEnvironment(stdin_isatty=True, stdout_isatty=False)
- r = http('--print=h', 'GET', httpbin('/get'), env=env)
+ r = http('--print=h', 'GET', httpbin.url + '/get', env=env)
assert HTTP_OK in r
Oops, something went wrong.

0 comments on commit 2a72ae2

Please sign in to comment.