1
0
mirror of https://github.com/kevin1024/vcrpy.git synced 2025-12-09 01:03:24 +00:00

test that works behind proxy

This commit is contained in:
Hernan Ezequiel Di Giorgi
2020-04-27 21:34:04 -03:00
committed by Kevin McCarthy
parent 936feb7748
commit 04b7f4fc65
4 changed files with 106 additions and 1 deletions

View File

@@ -0,0 +1,33 @@
interactions:
- request:
body: ''
headers:
accept:
- '*/*'
accept-encoding:
- gzip, deflate, br
connection:
- keep-alive
host:
- httpbin.org
user-agent:
- python-httpx/0.12.1
method: GET
uri: https://httpbin.org/headers
response:
content: "{\n \"headers\": {\n \"Accept\": \"*/*\", \n \"Accept-Encoding\"\
: \"gzip, deflate, br\", \n \"Host\": \"httpbin.org\", \n \"User-Agent\"\
: \"python-httpx/0.12.1\", \n \"X-Amzn-Trace-Id\": \"Root=1-5ea778c9-ea76170da792abdbf7614067\"\
\n }\n}\n"
headers:
access-control-allow-credentials: 'true'
access-control-allow-origin: '*'
connection: keep-alive
content-length: '226'
content-type: application/json
date: Tue, 28 Apr 2020 00:28:57 GMT
server: gunicorn/19.9.0
via: my_own_proxy
http_version: HTTP/1.1
status_code: 200
version: 1

View File

@@ -0,0 +1,43 @@
interactions:
- request:
body: null
headers:
accept:
- '*/*'
accept-encoding:
- gzip, deflate, br
connection:
- keep-alive
host:
- httpbin.org
user-agent:
- python-httpx/0.12.1
method: GET
uri: https://localhost:8080/headers
response:
body:
string: "{\n \"headers\": {\n \"Accept\": \"*/*\", \n \"Accept-Encoding\"\
: \"gzip, deflate, br\", \n \"Host\": \"httpbin.org\", \n \"User-Agent\"\
: \"python-httpx/0.12.1\", \n \"X-Amzn-Trace-Id\": \"Root=1-5ea778ca-c402f3ae901e78b3435b0a0c\"\
\n }\n}\n"
headers:
Access-Control-Allow-Credentials:
- 'true'
Access-Control-Allow-Origin:
- '*'
Connection:
- keep-alive
Content-Length:
- '226'
Content-Type:
- application/json
Date:
- Tue, 28 Apr 2020 00:28:58 GMT
Server:
- gunicorn/19.9.0
Via:
- my_own_proxy
status:
code: 200
message: OK
version: 1

View File

@@ -1,6 +1,7 @@
from unittest.mock import MagicMock
import pytest
import contextlib
import os
import vcr # noqa: E402
from vcr.stubs.httpx_stubs import _get_next_url
@@ -186,3 +187,27 @@ class TestGetNextUrl:
response.url = "http://github.com/"
response.headers = {}
assert _get_next_url(response) is None
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"
)
url = "https://httpbin.org/headers"
proxy = "http://localhost:8080"
proxies = {"http": proxy, "https": proxy}
with vcr.use_cassette(yml):
response = do_request(proxies=proxies, verify=False)("GET", url)
with vcr.use_cassette(yml) as cassette:
cassette_response = do_request(proxies=proxies, verify=False)("GET", url)
cassette_response.request.url == url
assert cassette.play_count == 1
assert cassette_response.headers["Via"] == "my_own_proxy", str(cassette_response.headers)
assert cassette_response.request.url == response.request.url

View File

@@ -68,7 +68,11 @@ def _record_responses(cassette, vcr_request, real_response):
past_vcr_request = _make_vcr_request(past_real_response.request)
cassette.append(past_vcr_request, _to_serialized_response(past_real_response))
vcr_request = _make_vcr_request(real_response.request)
if real_response.history:
# If there was a redirection keep we want the request which will hold the
# final redirect value
vcr_request = _make_vcr_request(real_response.request)
cassette.append(vcr_request, _to_serialized_response(real_response))
return real_response