@@ -24,41 +24,41 @@ def scheme(request):
24
24
def test_status_code (scheme , tmpdir ):
25
25
'''Ensure that we can read the status code'''
26
26
url = scheme + '://httpbin.org/'
27
- with vcr .use_cassette (str (tmpdir .join ('atts.yaml' ))) as cass :
27
+ with vcr .use_cassette (str (tmpdir .join ('atts.yaml' ))):
28
28
status_code = requests .get (url ).status_code
29
29
30
- with vcr .use_cassette (str (tmpdir .join ('atts.yaml' ))) as cass :
30
+ with vcr .use_cassette (str (tmpdir .join ('atts.yaml' ))):
31
31
assert status_code == requests .get (url ).status_code
32
32
33
33
34
34
def test_headers (scheme , tmpdir ):
35
35
'''Ensure that we can read the headers back'''
36
36
url = scheme + '://httpbin.org/'
37
- with vcr .use_cassette (str (tmpdir .join ('headers.yaml' ))) as cass :
37
+ with vcr .use_cassette (str (tmpdir .join ('headers.yaml' ))):
38
38
headers = requests .get (url ).headers
39
39
40
- with vcr .use_cassette (str (tmpdir .join ('headers.yaml' ))) as cass :
40
+ with vcr .use_cassette (str (tmpdir .join ('headers.yaml' ))):
41
41
assert headers == requests .get (url ).headers
42
42
43
43
44
44
def test_body (tmpdir , scheme ):
45
45
'''Ensure the responses are all identical enough'''
46
46
url = scheme + '://httpbin.org/bytes/1024'
47
- with vcr .use_cassette (str (tmpdir .join ('body.yaml' ))) as cass :
47
+ with vcr .use_cassette (str (tmpdir .join ('body.yaml' ))):
48
48
content = requests .get (url ).content
49
49
50
- with vcr .use_cassette (str (tmpdir .join ('body.yaml' ))) as cass :
50
+ with vcr .use_cassette (str (tmpdir .join ('body.yaml' ))):
51
51
assert content == requests .get (url ).content
52
52
53
53
54
54
def test_auth (tmpdir , scheme ):
55
55
'''Ensure that we can handle basic auth'''
56
56
auth = ('user' , 'passwd' )
57
57
url = scheme + '://httpbin.org/basic-auth/user/passwd'
58
- with vcr .use_cassette (str (tmpdir .join ('auth.yaml' ))) as cass :
58
+ with vcr .use_cassette (str (tmpdir .join ('auth.yaml' ))):
59
59
one = requests .get (url , auth = auth )
60
60
61
- with vcr .use_cassette (str (tmpdir .join ('auth.yaml' ))) as cass :
61
+ with vcr .use_cassette (str (tmpdir .join ('auth.yaml' ))):
62
62
two = requests .get (url , auth = auth )
63
63
assert one .content == two .content
64
64
assert one .status_code == two .status_code
@@ -81,10 +81,10 @@ def test_post(tmpdir, scheme):
81
81
'''Ensure that we can post and cache the results'''
82
82
data = {'key1' : 'value1' , 'key2' : 'value2' }
83
83
url = scheme + '://httpbin.org/post'
84
- with vcr .use_cassette (str (tmpdir .join ('requests.yaml' ))) as cass :
84
+ with vcr .use_cassette (str (tmpdir .join ('requests.yaml' ))):
85
85
req1 = requests .post (url , data ).content
86
86
87
- with vcr .use_cassette (str (tmpdir .join ('requests.yaml' ))) as cass :
87
+ with vcr .use_cassette (str (tmpdir .join ('requests.yaml' ))):
88
88
req2 = requests .post (url , data ).content
89
89
90
90
assert req1 == req2
@@ -93,7 +93,7 @@ def test_post(tmpdir, scheme):
93
93
def test_redirects (tmpdir , scheme ):
94
94
'''Ensure that we can handle redirects'''
95
95
url = scheme + '://httpbin.org/redirect-to?url=bytes/1024'
96
- with vcr .use_cassette (str (tmpdir .join ('requests.yaml' ))) as cass :
96
+ with vcr .use_cassette (str (tmpdir .join ('requests.yaml' ))):
97
97
content = requests .get (url ).content
98
98
99
99
with vcr .use_cassette (str (tmpdir .join ('requests.yaml' ))) as cass :
@@ -124,11 +124,11 @@ def test_gzip(tmpdir, scheme):
124
124
url = scheme + '://httpbin.org/gzip'
125
125
response = requests .get (url )
126
126
127
- with vcr .use_cassette (str (tmpdir .join ('gzip.yaml' ))) as cass :
127
+ with vcr .use_cassette (str (tmpdir .join ('gzip.yaml' ))):
128
128
response = requests .get (url )
129
129
assert_is_json (response .content )
130
130
131
- with vcr .use_cassette (str (tmpdir .join ('gzip.yaml' ))) as cass :
131
+ with vcr .use_cassette (str (tmpdir .join ('gzip.yaml' ))):
132
132
assert_is_json (response .content )
133
133
134
134
@@ -143,9 +143,65 @@ def test_session_and_connection_close(tmpdir, scheme):
143
143
with vcr .use_cassette (str (tmpdir .join ('session_connection_closed.yaml' ))):
144
144
session = requests .session ()
145
145
146
- resp = session .get ('http://httpbin.org/get' , headers = {'Connection' : 'close' })
147
- resp = session .get ('http://httpbin.org/get' , headers = {'Connection' : 'close' })
146
+ session .get ('http://httpbin.org/get' , headers = {'Connection' : 'close' })
147
+ session .get ('http://httpbin.org/get' , headers = {'Connection' : 'close' })
148
+
148
149
149
150
def test_https_with_cert_validation_disabled (tmpdir ):
150
151
with vcr .use_cassette (str (tmpdir .join ('cert_validation_disabled.yaml' ))):
151
152
requests .get ('https://httpbin.org' , verify = False )
153
+
154
+
155
+ def test_session_can_make_requests_after_requests_unpatched (tmpdir ):
156
+ with vcr .use_cassette (str (tmpdir .join ('test_session_after_unpatched.yaml' ))):
157
+ session = requests .session ()
158
+ session .get ('http://httpbin.org/get' )
159
+
160
+ with vcr .use_cassette (str (tmpdir .join ('test_session_after_unpatched.yaml' ))):
161
+ session = requests .session ()
162
+ session .get ('http://httpbin.org/get' )
163
+
164
+ session .get ('http://httpbin.org/status/200' )
165
+
166
+
167
+ def test_session_created_before_use_cassette_is_patched (tmpdir , scheme ):
168
+ url = scheme + '://httpbin.org/bytes/1024'
169
+ # Record arbitrary, random data to the cassette
170
+ with vcr .use_cassette (str (tmpdir .join ('session_created_outside.yaml' ))):
171
+ session = requests .session ()
172
+ body = session .get (url ).content
173
+
174
+ # Create a session outside of any cassette context manager
175
+ session = requests .session ()
176
+ # Make a request to make sure that a connectionpool is instantiated
177
+ session .get (scheme + '://httpbin.org/get' )
178
+
179
+ with vcr .use_cassette (str (tmpdir .join ('session_created_outside.yaml' ))):
180
+ # These should only be the same if the patching succeeded.
181
+ assert session .get (url ).content == body
182
+
183
+
184
+ def test_nested_cassettes_with_session_created_before_nesting (scheme , tmpdir ):
185
+ '''
186
+ This tests ensures that a session that was created while one cassette was
187
+ active is patched to the use the responses of a second cassette when it
188
+ is enabled.
189
+ '''
190
+ url = scheme + '://httpbin.org/bytes/1024'
191
+ with vcr .use_cassette (str (tmpdir .join ('first_nested.yaml' ))):
192
+ session = requests .session ()
193
+ first_body = session .get (url ).content
194
+ with vcr .use_cassette (str (tmpdir .join ('second_nested.yaml' ))):
195
+ second_body = session .get (url ).content
196
+ third_body = requests .get (url ).content
197
+
198
+ with vcr .use_cassette (str (tmpdir .join ('second_nested.yaml' ))):
199
+ session = requests .session ()
200
+ assert session .get (url ).content == second_body
201
+ with vcr .use_cassette (str (tmpdir .join ('first_nested.yaml' ))):
202
+ assert session .get (url ).content == first_body
203
+ assert session .get (url ).content == third_body
204
+
205
+ # Make sure that the session can now get content normally.
206
+ session .get ('http://www.reddit.com' )
207
+
0 commit comments