5757
5858Hot diggity dogg'''
5959
60+ UPGRADE_RESPONSE1 = b'''HTTP/1.1 101 Switching Protocols
61+ UPGRADE: websocket
62+ SEC-WEBSOCKET-ACCEPT: rVg+XakFNFOxk3ZH0lzrZBmg0aU=
63+ TRANSFER-ENCODING: chunked
64+ CONNECTION: upgrade
65+ DATE: Sat, 07 May 2016 23:44:32 GMT
66+ SERVER: Python/3.4 aiohttp/1.0.3
67+
68+ data''' .replace (b'\n ' , b'\r \n ' )
69+
6070
6171class TestResponseParser (unittest .TestCase ):
6272
@@ -67,7 +77,7 @@ def test_parser_response_1(self):
6777 m .on_header .side_effect = headers .__setitem__
6878
6979 p = httptools .HttpResponseParser (m )
70- p .feed_data (RESPONSE1_HEAD )
80+ p .feed_data (memoryview ( RESPONSE1_HEAD ) )
7181
7282 self .assertEqual (p .get_http_version (), '1.1' )
7383 self .assertEqual (p .get_status_code (), 200 )
@@ -92,9 +102,8 @@ def test_parser_response_1(self):
92102 self .assertFalse (m .on_chunk_complete .called )
93103
94104 with self .assertRaisesRegex (
95- httptools .HttpParserError ,
96- 'data received after completed connection' ):
97-
105+ httptools .HttpParserError ,
106+ 'data received after completed connection' ):
98107 p .feed_data (b'12123123' )
99108
100109 def test_parser_response_2 (self ):
@@ -136,6 +145,78 @@ def test_parser_response_5(self):
136145
137146 m .on_message_complete .assert_called_once_with ()
138147
148+ def test_parser_response_cb_on_status_1 (self ):
149+ class Error (Exception ):
150+ pass
151+
152+ m = mock .Mock ()
153+ m .on_status .side_effect = Error ()
154+
155+ p = httptools .HttpResponseParser (m )
156+ try :
157+ p .feed_data (RESPONSE1_HEAD + RESPONSE1_BODY )
158+ except httptools .HttpParserCallbackError as ex :
159+ self .assertIsInstance (ex .__context__ , Error )
160+ else :
161+ self .fail ('HttpParserCallbackError was not raised' )
162+
163+ def test_parser_response_cb_on_body_1 (self ):
164+ class Error (Exception ):
165+ pass
166+
167+ m = mock .Mock ()
168+ m .on_body .side_effect = Error ()
169+
170+ p = httptools .HttpResponseParser (m )
171+ try :
172+ p .feed_data (RESPONSE1_HEAD + RESPONSE1_BODY )
173+ except httptools .HttpParserCallbackError as ex :
174+ self .assertIsInstance (ex .__context__ , Error )
175+ else :
176+ self .fail ('HttpParserCallbackError was not raised' )
177+
178+ def test_parser_response_cb_on_message_complete_1 (self ):
179+ class Error (Exception ):
180+ pass
181+
182+ m = mock .Mock ()
183+ m .on_message_complete .side_effect = Error ()
184+
185+ p = httptools .HttpResponseParser (m )
186+ try :
187+ p .feed_data (RESPONSE1_HEAD + RESPONSE1_BODY )
188+ except httptools .HttpParserCallbackError as ex :
189+ self .assertIsInstance (ex .__context__ , Error )
190+ else :
191+ self .fail ('HttpParserCallbackError was not raised' )
192+
193+ def test_parser_upgrade_response_1 (self ):
194+ m = mock .Mock ()
195+
196+ headers = {}
197+ m .on_header .side_effect = headers .__setitem__
198+
199+ p = httptools .HttpResponseParser (m )
200+ try :
201+ p .feed_data (UPGRADE_RESPONSE1 )
202+ except httptools .HttpParserUpgrade as ex :
203+ offset = ex .args [0 ]
204+ else :
205+ self .fail ('HttpParserUpgrade was not raised' )
206+
207+ self .assertEqual (UPGRADE_RESPONSE1 [offset :], b'data' )
208+
209+ self .assertEqual (p .get_http_version (), '1.1' )
210+ self .assertEqual (p .get_status_code (), 101 )
211+
212+ m .on_status .assert_called_once_with (b'Switching Protocols' )
213+
214+ m .on_headers_complete .assert_called_once_with ()
215+ self .assertEqual (m .on_header .call_count , 6 )
216+ self .assertEqual (len (headers ), 6 )
217+
218+ m .on_message_complete .assert_called_once_with ()
219+
139220
140221class TestRequestParser (unittest .TestCase ):
141222
@@ -194,6 +275,36 @@ def test_parser_request_chunked_2(self):
194275 b'Host' : b'bar' ,
195276 b'Vary' : b'*' })
196277
278+ def test_parser_request_chunked_cb_error_1 (self ):
279+ class Error (Exception ):
280+ pass
281+
282+ m = mock .Mock ()
283+ m .on_chunk_header .side_effect = Error ()
284+
285+ p = httptools .HttpRequestParser (m )
286+ try :
287+ p .feed_data (CHUNKED_REQUEST1_1 )
288+ except httptools .HttpParserCallbackError as ex :
289+ self .assertIsInstance (ex .__context__ , Error )
290+ else :
291+ self .fail ('HttpParserCallbackError was not raised' )
292+
293+ def test_parser_request_chunked_cb_error_2 (self ):
294+ class Error (Exception ):
295+ pass
296+
297+ m = mock .Mock ()
298+ m .on_chunk_complete .side_effect = Error ()
299+
300+ p = httptools .HttpRequestParser (m )
301+ try :
302+ p .feed_data (CHUNKED_REQUEST1_1 )
303+ except httptools .HttpParserCallbackError as ex :
304+ self .assertIsInstance (ex .__context__ , Error )
305+ else :
306+ self .fail ('HttpParserCallbackError was not raised' )
307+
197308 def test_parser_request_chunked_3 (self ):
198309 m = mock .Mock ()
199310 p = httptools .HttpRequestParser (m )
@@ -237,6 +348,62 @@ def test_parser_request_upgrade_1(self):
237348 b'Host' : b'example.com' ,
238349 b'Upgrade' : b'WebSocket' })
239350
351+ def test_parser_request_error_in_on_header (self ):
352+ class Error (Exception ):
353+ pass
354+ m = mock .Mock ()
355+ m .on_header .side_effect = Error ()
356+ p = httptools .HttpRequestParser (m )
357+
358+ try :
359+ p .feed_data (UPGRADE_REQUEST1 )
360+ except httptools .HttpParserCallbackError as ex :
361+ self .assertIsInstance (ex .__context__ , Error )
362+ else :
363+ self .fail ('HttpParserCallbackError was not raised' )
364+
365+ def test_parser_request_error_in_on_message_begin (self ):
366+ class Error (Exception ):
367+ pass
368+ m = mock .Mock ()
369+ m .on_message_begin .side_effect = Error ()
370+ p = httptools .HttpRequestParser (m )
371+
372+ try :
373+ p .feed_data (UPGRADE_REQUEST1 )
374+ except httptools .HttpParserCallbackError as ex :
375+ self .assertIsInstance (ex .__context__ , Error )
376+ else :
377+ self .fail ('HttpParserCallbackError was not raised' )
378+
379+ def test_parser_request_error_in_cb_on_url (self ):
380+ class Error (Exception ):
381+ pass
382+ m = mock .Mock ()
383+ m .on_url .side_effect = Error ()
384+ p = httptools .HttpRequestParser (m )
385+
386+ try :
387+ p .feed_data (UPGRADE_REQUEST1 )
388+ except httptools .HttpParserCallbackError as ex :
389+ self .assertIsInstance (ex .__context__ , Error )
390+ else :
391+ self .fail ('HttpParserCallbackError was not raised' )
392+
393+ def test_parser_request_error_in_cb_on_headers_complete (self ):
394+ class Error (Exception ):
395+ pass
396+ m = mock .Mock ()
397+ m .on_headers_complete .side_effect = Error ()
398+ p = httptools .HttpRequestParser (m )
399+
400+ try :
401+ p .feed_data (UPGRADE_REQUEST1 )
402+ except httptools .HttpParserCallbackError as ex :
403+ self .assertIsInstance (ex .__context__ , Error )
404+ else :
405+ self .fail ('HttpParserCallbackError was not raised' )
406+
240407 def test_parser_request_2 (self ):
241408 p = httptools .HttpRequestParser (None )
242409 with self .assertRaises (httptools .HttpParserInvalidMethodError ):
0 commit comments