Skip to content

Commit db16bbe

Browse files
committed
Drop dependency on the abandoned python-lazy-fixture II.
1 parent 3524ccf commit db16bbe

File tree

5 files changed

+80
-172
lines changed

5 files changed

+80
-172
lines changed

tests/conftest.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import pytest
44
from pytest_httpbin import certs
5+
from pytest_httpbin.serve import Server as PyTestHttpBinServer
56

67
from .utils import ( # noqa
78
HTTPBIN_WITH_CHUNKED_SUPPORT_DOMAIN,
@@ -19,8 +20,10 @@
1920
interface,
2021
)
2122
from .utils.http_server import http_server, localhost_http_server # noqa
22-
# noinspection PyUnresolvedReferences
23-
from .fixtures import pytest_lazy_fixture
23+
24+
25+
# Patch to support `url = str(server)` in addition to `url = server + '/foo'`.
26+
PyTestHttpBinServer.__str__ = lambda self: self.url
2427

2528

2629
@pytest.fixture(scope='function', autouse=True)
@@ -72,8 +75,15 @@ def _remote_httpbin_available():
7275

7376
@pytest.fixture
7477
def remote_httpbin(_remote_httpbin_available):
78+
7579
if _remote_httpbin_available:
76-
return 'http://' + REMOTE_HTTPBIN_DOMAIN
80+
class Server(str):
81+
"""Look like `pytest_httpbin.serve.Server` but only provide URL info."""
82+
@property
83+
def url(self):
84+
return self
85+
86+
return Server('http://' + REMOTE_HTTPBIN_DOMAIN)
7787
pytest.skip(f'{REMOTE_HTTPBIN_DOMAIN} not resolvable')
7888

7989

tests/fixtures/pytest_lazy_fixture.py

Lines changed: 0 additions & 99 deletions
This file was deleted.

tests/test_cookie_on_redirects.py

Lines changed: 53 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,47 @@
22
from .utils import http
33

44

5-
def _stringify(fixture):
6-
return fixture + ''
7-
8-
9-
@pytest.mark.parametrize('instance', [
10-
pytest.lazy_fixture('httpbin'),
11-
pytest.lazy_fixture('remote_httpbin'),
5+
@pytest.mark.parametrize('target_httpbin', [
6+
'httpbin',
7+
'remote_httpbin',
128
])
13-
def test_explicit_user_set_cookie(httpbin, instance):
14-
# User set cookies ARE NOT persisted within redirects
15-
# when there is no session, even on the same domain.
16-
9+
def test_explicit_user_set_cookie(httpbin, target_httpbin, request):
10+
"""User set cookies ARE NOT persisted within redirects when there is no session, even on the same domain."""
11+
target_httpbin = request.getfixturevalue(target_httpbin)
1712
r = http(
1813
'--follow',
1914
httpbin + '/redirect-to',
20-
f'url=={_stringify(instance)}/cookies',
15+
f'url=={target_httpbin.url}/cookies',
2116
'Cookie:a=b'
2217
)
2318
assert r.json == {'cookies': {}}
2419

2520

26-
@pytest.mark.parametrize('instance', [
27-
pytest.lazy_fixture('httpbin'),
28-
pytest.lazy_fixture('remote_httpbin'),
21+
@pytest.mark.parametrize('target_httpbin', [
22+
'httpbin',
23+
'remote_httpbin',
2924
])
30-
def test_explicit_user_set_cookie_in_session(tmp_path, httpbin, instance):
31-
# User set cookies ARE persisted within redirects
32-
# when there is A session, even on the same domain.
33-
25+
def test_explicit_user_set_cookie_in_session(tmp_path, httpbin, target_httpbin, request):
26+
"""User set cookies ARE persisted within redirects when there is A session, even on the same domain."""
27+
target_httpbin = request.getfixturevalue(target_httpbin)
3428
r = http(
3529
'--follow',
3630
'--session',
3731
str(tmp_path / 'session.json'),
3832
httpbin + '/redirect-to',
39-
f'url=={_stringify(instance)}/cookies',
33+
f'url=={target_httpbin}/cookies',
4034
'Cookie:a=b'
4135
)
4236
assert r.json == {'cookies': {'a': 'b'}}
4337

4438

45-
@pytest.mark.parametrize('instance', [
46-
pytest.lazy_fixture('httpbin'),
47-
pytest.lazy_fixture('remote_httpbin'),
39+
@pytest.mark.parametrize('target_httpbin', [
40+
'httpbin',
41+
'remote_httpbin',
4842
])
49-
def test_saved_user_set_cookie_in_session(tmp_path, httpbin, instance):
50-
# User set cookies ARE persisted within redirects
51-
# when there is A session, even on the same domain.
52-
43+
def test_saved_user_set_cookie_in_session(tmp_path, httpbin, target_httpbin, request):
44+
"""User set cookies ARE persisted within redirects when there is A session, even on the same domain."""
45+
target_httpbin = request.getfixturevalue(target_httpbin)
5346
http(
5447
'--follow',
5548
'--session',
@@ -62,49 +55,47 @@ def test_saved_user_set_cookie_in_session(tmp_path, httpbin, instance):
6255
'--session',
6356
str(tmp_path / 'session.json'),
6457
httpbin + '/redirect-to',
65-
f'url=={_stringify(instance)}/cookies',
58+
f'url=={target_httpbin}/cookies',
6659
)
6760
assert r.json == {'cookies': {'a': 'b'}}
6861

6962

70-
@pytest.mark.parametrize('instance', [
71-
pytest.lazy_fixture('httpbin'),
72-
pytest.lazy_fixture('remote_httpbin'),
63+
@pytest.mark.parametrize('target_httpbin', [
64+
'httpbin',
65+
'remote_httpbin',
7366
])
7467
@pytest.mark.parametrize('session', [True, False])
75-
def test_explicit_user_set_headers(httpbin, tmp_path, instance, session):
76-
# User set headers ARE persisted within redirects
77-
# even on different domains domain with or without
78-
# an active session.
68+
def test_explicit_user_set_headers(httpbin, tmp_path, target_httpbin, session, request):
69+
"""
70+
User set headers ARE persisted within redirects even on different domains domain with or without an active session.
71+
72+
"""
73+
target_httpbin = request.getfixturevalue(target_httpbin)
7974
session_args = []
8075
if session:
8176
session_args.extend([
8277
'--session',
8378
str(tmp_path / 'session.json')
8479
])
85-
8680
r = http(
8781
'--follow',
8882
*session_args,
8983
httpbin + '/redirect-to',
90-
f'url=={_stringify(instance)}/get',
84+
f'url=={target_httpbin}/get',
9185
'X-Custom-Header:value'
9286
)
9387
assert 'X-Custom-Header' in r.json['headers']
9488

9589

9690
@pytest.mark.parametrize('session', [True, False])
9791
def test_server_set_cookie_on_redirect_same_domain(tmp_path, httpbin, session):
98-
# Server set cookies ARE persisted on the same domain
99-
# when they are forwarded.
100-
92+
"""Server set cookies ARE persisted on the same domain when they are forwarded."""
10193
session_args = []
10294
if session:
10395
session_args.extend([
10496
'--session',
10597
str(tmp_path / 'session.json')
10698
])
107-
10899
r = http(
109100
'--follow',
110101
*session_args,
@@ -136,8 +127,7 @@ def test_server_set_cookie_on_redirect_different_domain(tmp_path, http_server, h
136127

137128

138129
def test_saved_session_cookies_on_same_domain(tmp_path, httpbin):
139-
# Saved session cookies ARE persisted when making a new
140-
# request to the same domain.
130+
"""Saved session cookies ARE persisted when making a new request to the same domain."""
141131
http(
142132
'--session',
143133
str(tmp_path / 'session.json'),
@@ -152,8 +142,7 @@ def test_saved_session_cookies_on_same_domain(tmp_path, httpbin):
152142

153143

154144
def test_saved_session_cookies_on_different_domain(tmp_path, httpbin, remote_httpbin):
155-
# Saved session cookies ARE persisted when making a new
156-
# request to a different domain.
145+
"""Saved session cookies ARE persisted when making a new request to a different domain."""
157146
http(
158147
'--session',
159148
str(tmp_path / 'session.json'),
@@ -167,45 +156,49 @@ def test_saved_session_cookies_on_different_domain(tmp_path, httpbin, remote_htt
167156
assert r.json == {'cookies': {}}
168157

169158

170-
@pytest.mark.parametrize('initial_domain, first_request_domain, second_request_domain, expect_cookies', [
159+
@pytest.mark.parametrize(['initial_domain', 'first_request_domain', 'second_request_domain', 'expect_cookies'], [
171160
(
172161
# Cookies are set by Domain A
173162
# Initial domain is Domain A
174163
# Redirected domain is Domain A
175-
pytest.lazy_fixture('httpbin'),
176-
pytest.lazy_fixture('httpbin'),
177-
pytest.lazy_fixture('httpbin'),
164+
'httpbin',
165+
'httpbin',
166+
'httpbin',
178167
True,
179168
),
180169
(
181170
# Cookies are set by Domain A
182171
# Initial domain is Domain B
183172
# Redirected domain is Domain B
184-
pytest.lazy_fixture('httpbin'),
185-
pytest.lazy_fixture('remote_httpbin'),
186-
pytest.lazy_fixture('remote_httpbin'),
173+
'httpbin',
174+
'remote_httpbin',
175+
'remote_httpbin',
187176
False,
188177
),
189178
(
190179
# Cookies are set by Domain A
191180
# Initial domain is Domain A
192181
# Redirected domain is Domain B
193-
pytest.lazy_fixture('httpbin'),
194-
pytest.lazy_fixture('httpbin'),
195-
pytest.lazy_fixture('remote_httpbin'),
182+
'httpbin',
183+
'httpbin',
184+
'remote_httpbin',
196185
False,
197186
),
198187
(
199188
# Cookies are set by Domain A
200189
# Initial domain is Domain B
201190
# Redirected domain is Domain A
202-
pytest.lazy_fixture('httpbin'),
203-
pytest.lazy_fixture('remote_httpbin'),
204-
pytest.lazy_fixture('httpbin'),
191+
'httpbin',
192+
'remote_httpbin',
193+
'httpbin',
205194
True,
206195
),
207196
])
208-
def test_saved_session_cookies_on_redirect(tmp_path, initial_domain, first_request_domain, second_request_domain, expect_cookies):
197+
def test_saved_session_cookies_on_redirect(
198+
tmp_path, initial_domain, first_request_domain, second_request_domain, expect_cookies, request):
199+
initial_domain = request.getfixturevalue(initial_domain)
200+
first_request_domain = request.getfixturevalue(first_request_domain)
201+
second_request_domain = request.getfixturevalue(second_request_domain)
209202
http(
210203
'--session',
211204
str(tmp_path / 'session.json'),
@@ -216,7 +209,7 @@ def test_saved_session_cookies_on_redirect(tmp_path, initial_domain, first_reque
216209
str(tmp_path / 'session.json'),
217210
'--follow',
218211
first_request_domain + '/redirect-to',
219-
f'url=={_stringify(second_request_domain)}/cookies'
212+
f'url=={second_request_domain}/cookies'
220213
)
221214
if expect_cookies:
222215
expected_data = {'cookies': {'a': 'b'}}

tests/test_sessions.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -821,16 +821,17 @@ def test_session_multiple_headers_with_same_name(basic_session, httpbin):
821821
'server, expected_cookies',
822822
[
823823
(
824-
pytest.lazy_fixture('localhost_http_server'),
824+
'localhost_http_server',
825825
{'secure_cookie': 'foo', 'insecure_cookie': 'bar'}
826826
),
827827
(
828-
pytest.lazy_fixture('remote_httpbin'),
828+
'remote_httpbin',
829829
{'insecure_cookie': 'bar'}
830830
)
831831
]
832832
)
833-
def test_secure_cookies_on_localhost(mock_env, tmp_path, server, expected_cookies):
833+
def test_secure_cookies_on_localhost(mock_env, tmp_path, server, expected_cookies, request):
834+
server = request.getfixturevalue(server)
834835
session_path = tmp_path / 'session.json'
835836
http(
836837
'--session', str(session_path),

0 commit comments

Comments
 (0)