Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
- Use mockbin for redirections.
- Mock synchronous requests too.
  • Loading branch information
herdigiorgi authored and kevin1024 committed Jul 24, 2020
1 parent 5afa8f7 commit 8529c46
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 63 deletions.

This file was deleted.

23 changes: 10 additions & 13 deletions tests/integration/test_httpx.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def yml(tmpdir, request):


def test_status(tmpdir, scheme, do_request):
url = scheme + "://httpbin.org"
url = scheme + "://mockbin.org/request"
with vcr.use_cassette(str(tmpdir.join("status.yaml"))):
response = do_request()("GET", url)

Expand All @@ -64,7 +64,7 @@ def test_status(tmpdir, scheme, do_request):


def test_case_insensitive_headers(tmpdir, scheme, do_request):
url = scheme + "://httpbin.org"
url = scheme + "://mockbin.org/request"
with vcr.use_cassette(str(tmpdir.join("whatever.yaml"))):
do_request()("GET", url)

Expand Down Expand Up @@ -120,7 +120,7 @@ def test_params_same_url_distinct_params(tmpdir, scheme, do_request):


def test_redirect(tmpdir, do_request, yml):
url = "https://httpbin.org/redirect/2"
url = "https://mockbin.org/redirect/303/2"

response = do_request()("GET", url)
with vcr.use_cassette(yml):
Expand Down Expand Up @@ -169,10 +169,7 @@ def test_simple_fetching(tmpdir, do_request, yml, url):
def test_behind_proxy(do_request):
# This is recorded because otherwise we should have a live proxy somewhere.
yml = (
os.path.dirname(os.path.realpath(__file__))
+ "/cassettes/"
+ do_request.__name__
+ "test_httpx_test_test_behind_proxy.yml"
os.path.dirname(os.path.realpath(__file__)) + "/cassettes/" + "test_httpx_test_test_behind_proxy.yml"
)
url = "https://httpbin.org/headers"
proxy = "http://localhost:8080"
Expand Down Expand Up @@ -225,16 +222,16 @@ def response_cookies(response):


def test_relative_redirects(tmpdir, scheme, do_request):
url = scheme + "://httpbin.org"
url = scheme + "://mockbin.com/redirect/301?to=/redirect/301?to=/request"
testfile = str(tmpdir.join("relative_redirects.yml"))
with vcr.use_cassette(testfile):
response = do_request()("GET", url + "/redirect-to?url=/redirect-to?url=/get")
assert len(response.history) == 2
assert response.json()["url"].endswith("get")
response = do_request()("GET", url)
assert len(response.history) == 2, response
assert response.json()["url"].endswith("request")

with vcr.use_cassette(testfile) as cassette:
response = do_request()("GET", url + "/redirect-to?url=/redirect-to?url=/get")
response = do_request()("GET", url)
assert len(response.history) == 2
assert response.json()["url"].endswith("get")
assert response.json()["url"].endswith("request")

assert cassette.play_count == 3
11 changes: 6 additions & 5 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ skip_missing_interpreters=true
envlist =
cov-clean,
lint,
{py35,py36,py37,py38}-{requests,httplib2,urllib3,tornado4,boto3,aiohttp,httpx},
{pypy3}-{requests,httplib2,urllib3,tornado4,boto3,httpx},
{py35,py36,py37,py38}-{requests,httplib2,urllib3,tornado4,boto3,aiohttp},
{py36,py37,py38}-{httpx}
{pypy3}-{requests,httplib2,urllib3,tornado4,boto3},
cov-report


Expand Down Expand Up @@ -79,10 +80,10 @@ deps =
aiohttp: aiohttp
aiohttp: pytest-asyncio
aiohttp: pytest-aiohttp
httpx: httpx
httpx: pytest-asyncio
{py36,py37,py38}-{httpx}: httpx
{py36,py37,py38}-{httpx}: pytest-asyncio
depends =
lint,{py35,py36,py37,py38,pypy3}-{requests,httplib2,urllib3,tornado4,boto3,httpx},{py35,py36,py37,py38}-{aiohttp}: cov-clean
lint,{py35,py36,py37,py38,pypy3}-{requests,httplib2,urllib3,tornado4,boto3},{py35,py36,py37,py38}-{aiohttp},{py36,py37,py38}-{httpx}: cov-clean
cov-report: lint,{py35,py36,py37,py38,pypy3}-{requests,httplib2,urllib3,tornado4,boto3},{py35,py36,py37,py38}-{aiohttp}
passenv =
AWS_ACCESS_KEY_ID
Expand Down
7 changes: 5 additions & 2 deletions vcr/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
except ImportError: # pragma: no cover
pass
else:
_HttpxClient_send = httpx.Client.send
_HttpxSyncClient_send = httpx.Client.send
_HttpxAsyncClient_send = httpx.AsyncClient.send


Expand Down Expand Up @@ -330,11 +330,14 @@ def _httpx(self):
except ImportError: # pragma: no cover
return
else:
from .stubs.httpx_stubs import async_vcr_send
from .stubs.httpx_stubs import async_vcr_send, sync_vcr_send

new_async_client_send = async_vcr_send(self._cassette, _HttpxAsyncClient_send)
yield httpx.AsyncClient, "send", new_async_client_send

new_sync_client_send = sync_vcr_send(self._cassette, _HttpxSyncClient_send)
yield httpx.Client, "send", new_sync_client_send

def _urllib3_patchers(self, cpool, stubs):
http_connection_remover = ConnectionRemover(
self._get_cassette_subclass(stubs.VCRRequestsHTTPConnection)
Expand Down
19 changes: 19 additions & 0 deletions vcr/stubs/httpx_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,22 @@ def _inner_send(*args, **kwargs):
return _async_vcr_send(cassette, real_send, *args, **kwargs)

return _inner_send


def _sync_vcr_send(cassette, real_send, *args, **kwargs):
vcr_request, response = _shared_vcr_send(cassette, real_send, *args, **kwargs)
if response:
# add cookies from response to session cookie store
args[0].cookies.extract_cookies(response)
return response

real_response = real_send(*args, **kwargs)
return _record_responses(cassette, vcr_request, real_response)


def sync_vcr_send(cassette, real_send):
@functools.wraps(real_send)
def _inner_send(*args, **kwargs):
return _sync_vcr_send(cassette, real_send, *args, **kwargs)

return _inner_send

0 comments on commit 8529c46

Please sign in to comment.