mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-08 16:53:23 +00:00
* Drop support for legacy Python 2.7 * Upgrade Python syntax with pyupgrade --py3-plus * Trim testing matrix to remove python2 * re-enable python3.8 in travis as tests that are not allowed to fail * Remove some six * The future is now * Remove Python 2 imports * Add back example, but change py27 to py36 * Remove redundant compat.py * Blacken * Credit hugovk in changelog WIP Updating Sphinx Docs and AutoDoc * Fix AutoDoc and update Sphinx theme to python_doc_theme * Fix #420, autodoc even undocumented (docstring-less) method signatures * Doc theme 'nature'. Add global TOC to doc sidebar * Comment last reference to package six * Changelog is now a consistent format * Yet another documentation fix for links and title hierarchy * Start work on new SVG logo test SVG in README trying to test new SVG logo in README Apply centering Apply readme logo centering Trying to align image Trying random shit trying align right add emoji Large logo has higher priority Change title hierarchy Actually use a H1 Try again try and organise badges revert link back to point at master * updated new take on VCR logo as SVG code * Testing modern logo in docs * Add sanitize for rendering SVG * Switch to alabaster theme * Update vcrpy logo (#503) * Add credit for V4 logo changes. * Add rewind and play animation * Add svg into ReadTheDocs static assets so that it can be hosted so the animations work. * Need to embedd the SVG for ReadTheDocs somewhere so I can get the link to later embed in the README Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com> Co-authored-by: Sean Bailey <sean@seanbailey.io>
68 lines
2.6 KiB
Python
68 lines
2.6 KiB
Python
from urllib.request import urlopen
|
|
import socket
|
|
from contextlib import contextmanager
|
|
import vcr
|
|
|
|
|
|
@contextmanager
|
|
def overridden_dns(overrides):
|
|
"""
|
|
Monkeypatch socket.getaddrinfo() to override DNS lookups (name will resolve
|
|
to address)
|
|
"""
|
|
real_getaddrinfo = socket.getaddrinfo
|
|
|
|
def fake_getaddrinfo(*args, **kwargs):
|
|
if args[0] in overrides:
|
|
address = overrides[args[0]]
|
|
return [(2, 1, 6, "", (address, args[1]))]
|
|
return real_getaddrinfo(*args, **kwargs)
|
|
|
|
socket.getaddrinfo = fake_getaddrinfo
|
|
yield
|
|
socket.getaddrinfo = real_getaddrinfo
|
|
|
|
|
|
def test_ignore_localhost(tmpdir, httpbin):
|
|
with overridden_dns({"httpbin.org": "127.0.0.1"}):
|
|
cass_file = str(tmpdir.join("filter_qs.yaml"))
|
|
with vcr.use_cassette(cass_file, ignore_localhost=True) as cass:
|
|
urlopen("http://localhost:{}/".format(httpbin.port))
|
|
assert len(cass) == 0
|
|
urlopen("http://httpbin.org:{}/".format(httpbin.port))
|
|
assert len(cass) == 1
|
|
|
|
|
|
def test_ignore_httpbin(tmpdir, httpbin):
|
|
with overridden_dns({"httpbin.org": "127.0.0.1"}):
|
|
cass_file = str(tmpdir.join("filter_qs.yaml"))
|
|
with vcr.use_cassette(cass_file, ignore_hosts=["httpbin.org"]) as cass:
|
|
urlopen("http://httpbin.org:{}/".format(httpbin.port))
|
|
assert len(cass) == 0
|
|
urlopen("http://localhost:{}/".format(httpbin.port))
|
|
assert len(cass) == 1
|
|
|
|
|
|
def test_ignore_localhost_and_httpbin(tmpdir, httpbin):
|
|
with overridden_dns({"httpbin.org": "127.0.0.1"}):
|
|
cass_file = str(tmpdir.join("filter_qs.yaml"))
|
|
with vcr.use_cassette(cass_file, ignore_hosts=["httpbin.org"], ignore_localhost=True) as cass:
|
|
urlopen("http://httpbin.org:{}".format(httpbin.port))
|
|
urlopen("http://localhost:{}".format(httpbin.port))
|
|
assert len(cass) == 0
|
|
|
|
|
|
def test_ignore_localhost_twice(tmpdir, httpbin):
|
|
with overridden_dns({"httpbin.org": "127.0.0.1"}):
|
|
cass_file = str(tmpdir.join("filter_qs.yaml"))
|
|
with vcr.use_cassette(cass_file, ignore_localhost=True) as cass:
|
|
urlopen("http://localhost:{}".format(httpbin.port))
|
|
assert len(cass) == 0
|
|
urlopen("http://httpbin.org:{}".format(httpbin.port))
|
|
assert len(cass) == 1
|
|
with vcr.use_cassette(cass_file, ignore_localhost=True) as cass:
|
|
assert len(cass) == 1
|
|
urlopen("http://localhost:{}".format(httpbin.port))
|
|
urlopen("http://httpbin.org:{}".format(httpbin.port))
|
|
assert len(cass) == 1
|