Skip to content

Commit a23f22d

Browse files
move all passthru functions into one class (#646)
1 parent c25a1c8 commit a23f22d

File tree

1 file changed

+169
-165
lines changed

1 file changed

+169
-165
lines changed

responses/tests/test_responses.py

Lines changed: 169 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,196 +1624,213 @@ def run():
16241624
assert_reset()
16251625

16261626

1627-
def test_passthrough_flag(httpserver):
1628-
httpserver.expect_request("/").respond_with_data("OK", content_type="text/plain")
1629-
url = httpserver.url_for("/")
1630-
1631-
response = Response(responses.GET, url, body="MOCK")
1632-
1633-
@responses.activate
1634-
def run_passthrough():
1635-
responses.add(response)
1636-
resp = requests.get(url)
1637-
assert_response(resp, "OK")
1627+
class TestPassthru:
1628+
def test_passthrough_flag(self, httpserver):
1629+
httpserver.expect_request("/").respond_with_data(
1630+
"OK", content_type="text/plain"
1631+
)
1632+
url = httpserver.url_for("/")
16381633

1639-
@responses.activate
1640-
def run_mocked():
1641-
responses.add(response)
1642-
resp = requests.get(url)
1643-
assert_response(resp, "MOCK")
1634+
response = Response(responses.GET, url, body="MOCK")
16441635

1645-
run_mocked()
1646-
assert_reset()
1636+
@responses.activate
1637+
def run_passthrough():
1638+
responses.add(response)
1639+
resp = requests.get(url)
1640+
assert_response(resp, "OK")
16471641

1648-
response.passthrough = True
1649-
run_passthrough()
1650-
assert_reset()
1642+
@responses.activate
1643+
def run_mocked():
1644+
responses.add(response)
1645+
resp = requests.get(url)
1646+
assert_response(resp, "MOCK")
16511647

1648+
run_mocked()
1649+
assert_reset()
16521650

1653-
def test_passthrough_kwarg(httpserver):
1654-
httpserver.expect_request("/").respond_with_data("OK", content_type="text/plain")
1655-
url = httpserver.url_for("/")
1651+
response.passthrough = True
1652+
run_passthrough()
1653+
assert_reset()
16561654

1657-
def configure_response(passthrough):
1658-
responses.get(url, body="MOCK", passthrough=passthrough)
1655+
def test_passthrough_kwarg(self, httpserver):
1656+
httpserver.expect_request("/").respond_with_data(
1657+
"OK", content_type="text/plain"
1658+
)
1659+
url = httpserver.url_for("/")
16591660

1660-
@responses.activate
1661-
def run_passthrough():
1662-
configure_response(passthrough=True)
1663-
resp = requests.get(url)
1664-
assert_response(resp, "OK")
1661+
def configure_response(passthrough):
1662+
responses.get(url, body="MOCK", passthrough=passthrough)
16651663

1666-
@responses.activate
1667-
def run_mocked():
1668-
configure_response(passthrough=False)
1669-
resp = requests.get(url)
1670-
assert_response(resp, "MOCK")
1664+
@responses.activate
1665+
def run_passthrough():
1666+
configure_response(passthrough=True)
1667+
resp = requests.get(url)
1668+
assert_response(resp, "OK")
16711669

1672-
run_mocked()
1673-
assert_reset()
1670+
@responses.activate
1671+
def run_mocked():
1672+
configure_response(passthrough=False)
1673+
resp = requests.get(url)
1674+
assert_response(resp, "MOCK")
16741675

1675-
run_passthrough()
1676-
assert_reset()
1676+
run_mocked()
1677+
assert_reset()
16771678

1679+
run_passthrough()
1680+
assert_reset()
16781681

1679-
def test_passthrough_response(httpserver):
1680-
httpserver.expect_request("/").respond_with_data("OK", content_type="text/plain")
1681-
url = httpserver.url_for("/")
1682+
def test_passthrough_response(self, httpserver):
1683+
httpserver.expect_request("/").respond_with_data(
1684+
"OK", content_type="text/plain"
1685+
)
1686+
url = httpserver.url_for("/")
16821687

1683-
@responses.activate
1684-
def run():
1685-
responses.add(PassthroughResponse(responses.GET, url))
1686-
responses.add(responses.GET, "{}/one".format(url), body="one")
1687-
responses.add(responses.GET, "http://example.com/two", body="two")
1688+
@responses.activate
1689+
def run():
1690+
responses.add(PassthroughResponse(responses.GET, url))
1691+
responses.add(responses.GET, "{}/one".format(url), body="one")
1692+
responses.add(responses.GET, "http://example.com/two", body="two")
1693+
1694+
resp = requests.get("http://example.com/two")
1695+
assert_response(resp, "two")
1696+
resp = requests.get("{}/one".format(url))
1697+
assert_response(resp, "one")
1698+
resp = requests.get(url)
1699+
assert_response(resp, "OK")
16881700

1689-
resp = requests.get("http://example.com/two")
1690-
assert_response(resp, "two")
1691-
resp = requests.get("{}/one".format(url))
1692-
assert_response(resp, "one")
1693-
resp = requests.get(url)
1694-
assert_response(resp, "OK")
1701+
assert len(responses.calls) == 3
1702+
responses.assert_call_count(url, 1)
16951703

1696-
assert len(responses.calls) == 3
1697-
responses.assert_call_count(url, 1)
1704+
run()
1705+
assert_reset()
16981706

1699-
run()
1700-
assert_reset()
1707+
def test_passthrough_response_stream(self, httpserver):
1708+
httpserver.expect_request("/").respond_with_data(
1709+
"OK", content_type="text/plain"
1710+
)
17011711

1712+
@responses.activate
1713+
def run():
1714+
url = httpserver.url_for("/")
1715+
responses.add(PassthroughResponse(responses.GET, url))
1716+
content_1 = requests.get(url).content
1717+
with requests.get(url, stream=True) as resp:
1718+
content_2 = resp.raw.read()
1719+
assert content_1 == content_2
17021720

1703-
def test_passthrough_response_stream(httpserver):
1704-
httpserver.expect_request("/").respond_with_data("OK", content_type="text/plain")
1721+
run()
1722+
assert_reset()
17051723

1706-
@responses.activate
1707-
def run():
1724+
def test_passthru_prefixes(self, httpserver):
1725+
httpserver.expect_request("/").respond_with_data(
1726+
"OK", content_type="text/plain"
1727+
)
17081728
url = httpserver.url_for("/")
1709-
responses.add(PassthroughResponse(responses.GET, url))
1710-
content_1 = requests.get(url).content
1711-
with requests.get(url, stream=True) as resp:
1712-
content_2 = resp.raw.read()
1713-
assert content_1 == content_2
17141729

1715-
run()
1716-
assert_reset()
1730+
@responses.activate
1731+
def run_constructor_argument():
1732+
with responses.RequestsMock(passthru_prefixes=(url,)):
1733+
resp = requests.get(url)
1734+
assert_response(resp, "OK")
17171735

1736+
@responses.activate
1737+
def run_property_setter():
1738+
with responses.RequestsMock() as m:
1739+
m.passthru_prefixes = tuple([url])
1740+
resp = requests.get(url)
1741+
assert_response(resp, "OK")
17181742

1719-
def test_passthru_prefixes(httpserver):
1720-
httpserver.expect_request("/").respond_with_data("OK", content_type="text/plain")
1721-
url = httpserver.url_for("/")
1743+
run_constructor_argument()
1744+
assert_reset()
1745+
run_property_setter()
1746+
assert_reset()
17221747

1723-
@responses.activate
1724-
def run_constructor_argument():
1725-
with responses.RequestsMock(passthru_prefixes=(url,)):
1726-
resp = requests.get(url)
1727-
assert_response(resp, "OK")
1748+
def test_passthru(self, httpserver):
1749+
httpserver.expect_request("/").respond_with_data(
1750+
"OK", content_type="text/plain"
1751+
)
1752+
url = httpserver.url_for("/")
17281753

1729-
@responses.activate
1730-
def run_property_setter():
1731-
with responses.RequestsMock() as m:
1732-
m.passthru_prefixes = tuple([url])
1754+
@responses.activate
1755+
def run():
1756+
responses.add_passthru(url)
1757+
responses.add(responses.GET, "{}/one".format(url), body="one")
1758+
responses.add(responses.GET, "http://example.com/two", body="two")
1759+
1760+
resp = requests.get("http://example.com/two")
1761+
assert_response(resp, "two")
1762+
resp = requests.get("{}/one".format(url))
1763+
assert_response(resp, "one")
17331764
resp = requests.get(url)
17341765
assert_response(resp, "OK")
17351766

1736-
run_constructor_argument()
1737-
assert_reset()
1738-
run_property_setter()
1739-
assert_reset()
1740-
1741-
1742-
def test_passthru(httpserver):
1743-
httpserver.expect_request("/").respond_with_data("OK", content_type="text/plain")
1744-
url = httpserver.url_for("/")
1745-
1746-
@responses.activate
1747-
def run():
1748-
responses.add_passthru(url)
1749-
responses.add(responses.GET, "{}/one".format(url), body="one")
1750-
responses.add(responses.GET, "http://example.com/two", body="two")
1751-
1752-
resp = requests.get("http://example.com/two")
1753-
assert_response(resp, "two")
1754-
resp = requests.get("{}/one".format(url))
1755-
assert_response(resp, "one")
1756-
resp = requests.get(url)
1757-
assert_response(resp, "OK")
1758-
1759-
run()
1760-
assert_reset()
1761-
1762-
1763-
def test_passthru_regex(httpserver):
1764-
httpserver.expect_request(re.compile("^/\\w+")).respond_with_data(
1765-
"OK", content_type="text/plain"
1766-
)
1767-
url = httpserver.url_for("/")
1767+
run()
1768+
assert_reset()
17681769

1769-
@responses.activate
1770-
def run():
1771-
responses.add_passthru(re.compile(f"{url}/\\w+"))
1772-
responses.add(responses.GET, "{}/one".format(url), body="one")
1773-
responses.add(responses.GET, "http://example.com/two", body="two")
1770+
def test_passthru_regex(self, httpserver):
1771+
httpserver.expect_request(re.compile("^/\\w+")).respond_with_data(
1772+
"OK", content_type="text/plain"
1773+
)
1774+
url = httpserver.url_for("/")
17741775

1775-
resp = requests.get("http://example.com/two")
1776-
assert_response(resp, "two")
1777-
resp = requests.get(f"{url}/one")
1778-
assert_response(resp, "one")
1779-
resp = requests.get(f"{url}/two")
1780-
assert_response(resp, "OK")
1781-
resp = requests.get(f"{url}/three")
1782-
assert_response(resp, "OK")
1776+
@responses.activate
1777+
def run():
1778+
responses.add_passthru(re.compile(f"{url}/\\w+"))
1779+
responses.add(responses.GET, "{}/one".format(url), body="one")
1780+
responses.add(responses.GET, "http://example.com/two", body="two")
1781+
1782+
resp = requests.get("http://example.com/two")
1783+
assert_response(resp, "two")
1784+
resp = requests.get(f"{url}/one")
1785+
assert_response(resp, "one")
1786+
resp = requests.get(f"{url}/two")
1787+
assert_response(resp, "OK")
1788+
resp = requests.get(f"{url}/three")
1789+
assert_response(resp, "OK")
17831790

1784-
run()
1785-
assert_reset()
1791+
run()
1792+
assert_reset()
17861793

1794+
def test_passthru_does_not_persist_across_tests(self, httpserver):
1795+
"""
1796+
passthru should be erased on exit from context manager
1797+
see:
1798+
https://github.com/getsentry/responses/issues/322
1799+
"""
1800+
httpserver.expect_request("/").respond_with_data(
1801+
"mocked server", status=969, content_type="text/plain"
1802+
)
17871803

1788-
def test_passthru_does_not_persist_across_tests(httpserver):
1789-
"""
1790-
passthru should be erased on exit from context manager
1791-
see:
1792-
https://github.com/getsentry/responses/issues/322
1793-
"""
1794-
httpserver.expect_request("/").respond_with_data("OK", content_type="text/plain")
1804+
@responses.activate
1805+
def with_a_passthru():
1806+
assert not responses.mock.passthru_prefixes
1807+
responses.add_passthru(re.compile(".*"))
17951808

1796-
@responses.activate
1797-
def with_a_passthru():
1798-
assert not responses.mock.passthru_prefixes
1799-
responses.add_passthru(re.compile(".*"))
1809+
url = httpserver.url_for("/")
1810+
response = requests.get(url)
1811+
assert response.status_code == 969
1812+
assert response.text == "mocked server"
18001813

1801-
# wrap request that is passed through with another mock. That helps
1802-
# to avoid issues if real URL is unavailable, allow to run tests offline
1803-
with responses.RequestsMock(target="responses._real_send") as rsp:
1804-
rsp.add(responses.GET, "https://example66.ru", status=969)
1805-
response = requests.get("https://example66.ru")
1814+
@responses.activate
1815+
def without_a_passthru():
1816+
assert not responses.mock.passthru_prefixes
1817+
with pytest.raises(requests.exceptions.ConnectionError):
1818+
requests.get("https://example.com")
18061819

1807-
assert response.status_code == 969
1820+
with_a_passthru()
1821+
without_a_passthru()
18081822

1809-
@responses.activate
1810-
def without_a_passthru():
1811-
assert not responses.mock.passthru_prefixes
1812-
with pytest.raises(requests.exceptions.ConnectionError):
1813-
requests.get("https://example.com")
1823+
def test_passthru_unicode(self):
1824+
@responses.activate
1825+
def run():
1826+
with responses.RequestsMock() as m:
1827+
url = "http://موقع.وزارة-الاتصالات.مصر/"
1828+
clean_url = "http://xn--4gbrim.xn----ymcbaaajlc6dj7bxne2c.xn--wgbh1c/"
1829+
m.add_passthru(url)
1830+
assert m.passthru_prefixes[0] == clean_url
18141831

1815-
with_a_passthru()
1816-
without_a_passthru()
1832+
run()
1833+
assert_reset()
18171834

18181835

18191836
def test_method_named_param():
@@ -1827,19 +1844,6 @@ def run():
18271844
assert_reset()
18281845

18291846

1830-
def test_passthru_unicode():
1831-
@responses.activate
1832-
def run():
1833-
with responses.RequestsMock() as m:
1834-
url = "http://موقع.وزارة-الاتصالات.مصر/"
1835-
clean_url = "http://xn--4gbrim.xn----ymcbaaajlc6dj7bxne2c.xn--wgbh1c/"
1836-
m.add_passthru(url)
1837-
assert m.passthru_prefixes[0] == clean_url
1838-
1839-
run()
1840-
assert_reset()
1841-
1842-
18431847
def test_custom_target(monkeypatch):
18441848
requests_mock = responses.RequestsMock(target="something.else")
18451849
std_mock_mock = responses.std_mock.MagicMock()

0 commit comments

Comments
 (0)