This repository was archived by the owner on Jan 13, 2021. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change @@ -165,3 +165,52 @@ def socket_handler(listener):
165
165
assert r .headers [b'content-length' ] == [b'15' ]
166
166
167
167
assert r .read () == b'hellotherehello'
168
+
169
+ def test_connection_context_manager (self ):
170
+ self .set_up ()
171
+
172
+ send_event = threading .Event ()
173
+
174
+ def socket_handler (listener ):
175
+ sock = listener .accept ()[0 ]
176
+
177
+ # We should get the initial request.
178
+ data = b''
179
+ while not data .endswith (b'\r \n \r \n ' ):
180
+ data += sock .recv (65535 )
181
+
182
+ send_event .wait ()
183
+
184
+ # We need to send back a response.
185
+ resp = (
186
+ b'HTTP/1.1 200 OK\r \n '
187
+ b'Server: socket-level-server\r \n '
188
+ b'Content-Length: 15\r \n '
189
+ b'\r \n '
190
+ )
191
+ sock .send (resp )
192
+
193
+ chunks = [
194
+ b'hello' ,
195
+ b'there' ,
196
+ b'hello' ,
197
+ ]
198
+
199
+ for chunk in chunks :
200
+ sock .send (chunk )
201
+
202
+ sock .close ()
203
+
204
+ self ._start_server (socket_handler )
205
+ with self .get_connection () as c :
206
+ c .request ('GET' , '/' )
207
+ send_event .set ()
208
+ r = c .get_response ()
209
+ data = r .read ()
210
+
211
+ assert r .status == 200
212
+ assert r .reason == b'OK'
213
+ assert len (r .headers ) == 2
214
+ assert data == b'hellotherehello'
215
+
216
+ assert c ._sock is None
You can’t perform that action at this time.
0 commit comments