4
4
import stat
5
5
import sys
6
6
import re
7
+ from test import support
7
8
from test .support import os_helper
8
9
from test .support import warnings_helper
9
10
import time
@@ -105,8 +106,7 @@ def test_http2time_formats(self):
105
106
self .assertEqual (http2time (s .lower ()), test_t , s .lower ())
106
107
self .assertEqual (http2time (s .upper ()), test_t , s .upper ())
107
108
108
- def test_http2time_garbage (self ):
109
- for test in [
109
+ @support .subTests ('test' , [
110
110
'' ,
111
111
'Garbage' ,
112
112
'Mandag 16. September 1996' ,
@@ -121,10 +121,9 @@ def test_http2time_garbage(self):
121
121
'08-01-3697739' ,
122
122
'09 Feb 19942632 22:23:32 GMT' ,
123
123
'Wed, 09 Feb 1994834 22:23:32 GMT' ,
124
- ]:
125
- self .assertIsNone (http2time (test ),
126
- "http2time(%s) is not None\n "
127
- "http2time(test) %s" % (test , http2time (test )))
124
+ ])
125
+ def test_http2time_garbage (self , test ):
126
+ self .assertIsNone (http2time (test ))
128
127
129
128
def test_http2time_redos_regression_actually_completes (self ):
130
129
# LOOSE_HTTP_DATE_RE was vulnerable to malicious input which caused catastrophic backtracking (REDoS).
@@ -149,9 +148,7 @@ def parse_date(text):
149
148
self .assertEqual (parse_date ("1994-02-03 19:45:29 +0530" ),
150
149
(1994 , 2 , 3 , 14 , 15 , 29 ))
151
150
152
- def test_iso2time_formats (self ):
153
- # test iso2time for supported dates.
154
- tests = [
151
+ @support .subTests ('s' , [
155
152
'1994-02-03 00:00:00 -0000' , # ISO 8601 format
156
153
'1994-02-03 00:00:00 +0000' , # ISO 8601 format
157
154
'1994-02-03 00:00:00' , # zone is optional
@@ -164,16 +161,15 @@ def test_iso2time_formats(self):
164
161
# A few tests with extra space at various places
165
162
' 1994-02-03 ' ,
166
163
' 1994-02-03T00:00:00 ' ,
167
- ]
168
-
164
+ ])
165
+ def test_iso2time_formats (self , s ):
166
+ # test iso2time for supported dates.
169
167
test_t = 760233600 # assume broken POSIX counting of seconds
170
- for s in tests :
171
- self .assertEqual (iso2time (s ), test_t , s )
172
- self .assertEqual (iso2time (s .lower ()), test_t , s .lower ())
173
- self .assertEqual (iso2time (s .upper ()), test_t , s .upper ())
168
+ self .assertEqual (iso2time (s ), test_t , s )
169
+ self .assertEqual (iso2time (s .lower ()), test_t , s .lower ())
170
+ self .assertEqual (iso2time (s .upper ()), test_t , s .upper ())
174
171
175
- def test_iso2time_garbage (self ):
176
- for test in [
172
+ @support .subTests ('test' , [
177
173
'' ,
178
174
'Garbage' ,
179
175
'Thursday, 03-Feb-94 00:00:00 GMT' ,
@@ -186,9 +182,9 @@ def test_iso2time_garbage(self):
186
182
'01-01-1980 00:00:62' ,
187
183
'01-01-1980T00:00:62' ,
188
184
'19800101T250000Z' ,
189
- ]:
190
- self . assertIsNone ( iso2time ( test ),
191
- " iso2time(%r)" % test )
185
+ ])
186
+ def test_iso2time_garbage ( self , test ):
187
+ self . assertIsNone ( iso2time (test ) )
192
188
193
189
def test_iso2time_performance_regression (self ):
194
190
# If ISO_DATE_RE regresses to quadratic complexity, this test will take a very long time to succeed.
@@ -199,24 +195,23 @@ def test_iso2time_performance_regression(self):
199
195
200
196
class HeaderTests (unittest .TestCase ):
201
197
202
- def test_parse_ns_headers (self ):
203
- # quotes should be stripped
204
- expected = [[('foo' , 'bar' ), ('expires' , 2209069412 ), ('version' , '0' )]]
205
- for hdr in [
198
+ @support .subTests ('hdr' , [
206
199
'foo=bar; expires=01 Jan 2040 22:23:32 GMT' ,
207
200
'foo=bar; expires="01 Jan 2040 22:23:32 GMT"' ,
208
- ]:
209
- self .assertEqual (parse_ns_headers ([hdr ]), expected )
210
-
211
- def test_parse_ns_headers_version (self ):
212
-
201
+ ])
202
+ def test_parse_ns_headers (self , hdr ):
213
203
# quotes should be stripped
214
- expected = [[('foo' , 'bar' ), ('version' , '1' )]]
215
- for hdr in [
204
+ expected = [[('foo' , 'bar' ), ('expires' , 2209069412 ), ('version' , '0' )]]
205
+ self .assertEqual (parse_ns_headers ([hdr ]), expected )
206
+
207
+ @support .subTests ('hdr' , [
216
208
'foo=bar; version="1"' ,
217
209
'foo=bar; Version="1"' ,
218
- ]:
219
- self .assertEqual (parse_ns_headers ([hdr ]), expected )
210
+ ])
211
+ def test_parse_ns_headers_version (self , hdr ):
212
+ # quotes should be stripped
213
+ expected = [[('foo' , 'bar' ), ('version' , '1' )]]
214
+ self .assertEqual (parse_ns_headers ([hdr ]), expected )
220
215
221
216
def test_parse_ns_headers_special_names (self ):
222
217
# names such as 'expires' are not special in first name=value pair
@@ -226,8 +221,7 @@ def test_parse_ns_headers_special_names(self):
226
221
expected = [[("expires" , "01 Jan 2040 22:23:32 GMT" ), ("version" , "0" )]]
227
222
self .assertEqual (parse_ns_headers ([hdr ]), expected )
228
223
229
- def test_join_header_words (self ):
230
- for src , expected in [
224
+ @support .subTests ('src,expected' , [
231
225
([[("foo" , None ), ("bar" , "baz" )]], "foo; bar=baz" ),
232
226
(([]), "" ),
233
227
(([[]]), "" ),
@@ -237,12 +231,11 @@ def test_join_header_words(self):
237
231
'n; foo="foo;_", bar=foo_bar' ),
238
232
([[("n" , "m" ), ("foo" , None )], [("bar" , "foo_bar" )]],
239
233
'n=m; foo, bar=foo_bar' ),
240
- ]:
241
- with self . subTest ( src = src ):
242
- self .assertEqual (join_header_words (src ), expected )
234
+ ])
235
+ def test_join_header_words ( self , src , expected ):
236
+ self .assertEqual (join_header_words (src ), expected )
243
237
244
- def test_split_header_words (self ):
245
- tests = [
238
+ @support .subTests ('arg,expect' , [
246
239
("foo" , [[("foo" , None )]]),
247
240
("foo=bar" , [[("foo" , "bar" )]]),
248
241
(" foo " , [[("foo" , None )]]),
@@ -259,24 +252,22 @@ def test_split_header_words(self):
259
252
(r'foo; bar=baz, spam=, foo="\,\;\"", bar= ' ,
260
253
[[("foo" , None ), ("bar" , "baz" )],
261
254
[("spam" , "" )], [("foo" , ',;"' )], [("bar" , "" )]]),
262
- ]
263
-
264
- for arg , expect in tests :
265
- try :
266
- result = split_header_words ([arg ])
267
- except :
268
- import traceback , io
269
- f = io .StringIO ()
270
- traceback .print_exc (None , f )
271
- result = "(error -- traceback follows)\n \n %s" % f .getvalue ()
272
- self .assertEqual (result , expect , """
255
+ ])
256
+ def test_split_header_words (self , arg , expect ):
257
+ try :
258
+ result = split_header_words ([arg ])
259
+ except :
260
+ import traceback , io
261
+ f = io .StringIO ()
262
+ traceback .print_exc (None , f )
263
+ result = "(error -- traceback follows)\n \n %s" % f .getvalue ()
264
+ self .assertEqual (result , expect , """
273
265
When parsing: '%s'
274
266
Expected: '%s'
275
267
Got: '%s'
276
268
""" % (arg , expect , result ))
277
269
278
- def test_roundtrip (self ):
279
- tests = [
270
+ @support .subTests ('arg,expect' , [
280
271
("foo" , "foo" ),
281
272
("foo=bar" , "foo=bar" ),
282
273
(" foo " , "foo" ),
@@ -309,12 +300,11 @@ def test_roundtrip(self):
309
300
310
301
('n; foo="foo;_", bar="foo,_"' ,
311
302
'n; foo="foo;_", bar="foo,_"' ),
312
- ]
313
-
314
- for arg , expect in tests :
315
- input = split_header_words ([arg ])
316
- res = join_header_words (input )
317
- self .assertEqual (res , expect , """
303
+ ])
304
+ def test_roundtrip (self , arg , expect ):
305
+ input = split_header_words ([arg ])
306
+ res = join_header_words (input )
307
+ self .assertEqual (res , expect , """
318
308
When parsing: '%s'
319
309
Expected: '%s'
320
310
Got: '%s'
@@ -516,14 +506,7 @@ class CookieTests(unittest.TestCase):
516
506
## just the 7 special TLD's listed in their spec. And folks rely on
517
507
## that...
518
508
519
- def test_domain_return_ok (self ):
520
- # test optimization: .domain_return_ok() should filter out most
521
- # domains in the CookieJar before we try to access them (because that
522
- # may require disk access -- in particular, with MSIECookieJar)
523
- # This is only a rough check for performance reasons, so it's not too
524
- # critical as long as it's sufficiently liberal.
525
- pol = DefaultCookiePolicy ()
526
- for url , domain , ok in [
509
+ @support .subTests ('url,domain,ok' , [
527
510
("http://foo.bar.com/" , "blah.com" , False ),
528
511
("http://foo.bar.com/" , "rhubarb.blah.com" , False ),
529
512
("http://foo.bar.com/" , "rhubarb.foo.bar.com" , False ),
@@ -543,11 +526,18 @@ def test_domain_return_ok(self):
543
526
("http://foo/" , ".local" , True ),
544
527
("http://barfoo.com" , ".foo.com" , False ),
545
528
("http://barfoo.com" , "foo.com" , False ),
546
- ]:
547
- request = urllib .request .Request (url )
548
- r = pol .domain_return_ok (domain , request )
549
- if ok : self .assertTrue (r )
550
- else : self .assertFalse (r )
529
+ ])
530
+ def test_domain_return_ok (self , url , domain , ok ):
531
+ # test optimization: .domain_return_ok() should filter out most
532
+ # domains in the CookieJar before we try to access them (because that
533
+ # may require disk access -- in particular, with MSIECookieJar)
534
+ # This is only a rough check for performance reasons, so it's not too
535
+ # critical as long as it's sufficiently liberal.
536
+ pol = DefaultCookiePolicy ()
537
+ request = urllib .request .Request (url )
538
+ r = pol .domain_return_ok (domain , request )
539
+ if ok : self .assertTrue (r )
540
+ else : self .assertFalse (r )
551
541
552
542
def test_missing_value (self ):
553
543
# missing = sign in Cookie: header is regarded by Mozilla as a missing
@@ -581,10 +571,7 @@ def test_missing_value(self):
581
571
self .assertEqual (interact_netscape (c , "http://www.acme.com/foo/" ),
582
572
'"spam"; eggs' )
583
573
584
- def test_rfc2109_handling (self ):
585
- # RFC 2109 cookies are handled as RFC 2965 or Netscape cookies,
586
- # dependent on policy settings
587
- for rfc2109_as_netscape , rfc2965 , version in [
574
+ @support .subTests ('rfc2109_as_netscape,rfc2965,version' , [
588
575
# default according to rfc2965 if not explicitly specified
589
576
(None , False , 0 ),
590
577
(None , True , 1 ),
@@ -593,24 +580,27 @@ def test_rfc2109_handling(self):
593
580
(False , True , 1 ),
594
581
(True , False , 0 ),
595
582
(True , True , 0 ),
596
- ]:
597
- policy = DefaultCookiePolicy (
598
- rfc2109_as_netscape = rfc2109_as_netscape ,
599
- rfc2965 = rfc2965 )
600
- c = CookieJar (policy )
601
- interact_netscape (c , "http://www.example.com/" , "ni=ni; Version=1" )
602
- try :
603
- cookie = c ._cookies ["www.example.com" ]["/" ]["ni" ]
604
- except KeyError :
605
- self .assertIsNone (version ) # didn't expect a stored cookie
606
- else :
607
- self .assertEqual (cookie .version , version )
608
- # 2965 cookies are unaffected
609
- interact_2965 (c , "http://www.example.com/" ,
610
- "foo=bar; Version=1" )
611
- if rfc2965 :
612
- cookie2965 = c ._cookies ["www.example.com" ]["/" ]["foo" ]
613
- self .assertEqual (cookie2965 .version , 1 )
583
+ ])
584
+ def test_rfc2109_handling (self , rfc2109_as_netscape , rfc2965 , version ):
585
+ # RFC 2109 cookies are handled as RFC 2965 or Netscape cookies,
586
+ # dependent on policy settings
587
+ policy = DefaultCookiePolicy (
588
+ rfc2109_as_netscape = rfc2109_as_netscape ,
589
+ rfc2965 = rfc2965 )
590
+ c = CookieJar (policy )
591
+ interact_netscape (c , "http://www.example.com/" , "ni=ni; Version=1" )
592
+ try :
593
+ cookie = c ._cookies ["www.example.com" ]["/" ]["ni" ]
594
+ except KeyError :
595
+ self .assertIsNone (version ) # didn't expect a stored cookie
596
+ else :
597
+ self .assertEqual (cookie .version , version )
598
+ # 2965 cookies are unaffected
599
+ interact_2965 (c , "http://www.example.com/" ,
600
+ "foo=bar; Version=1" )
601
+ if rfc2965 :
602
+ cookie2965 = c ._cookies ["www.example.com" ]["/" ]["foo" ]
603
+ self .assertEqual (cookie2965 .version , 1 )
614
604
615
605
def test_ns_parser (self ):
616
606
c = CookieJar ()
@@ -778,8 +768,7 @@ def test_default_path_with_query(self):
778
768
# Cookie is sent back to the same URI.
779
769
self .assertEqual (interact_netscape (cj , uri ), value )
780
770
781
- def test_escape_path (self ):
782
- cases = [
771
+ @support .subTests ('arg,result' , [
783
772
# quoted safe
784
773
("/foo%2f/bar" , "/foo%2F/bar" ),
785
774
("/foo%2F/bar" , "/foo%2F/bar" ),
@@ -799,9 +788,9 @@ def test_escape_path(self):
799
788
("/foo/bar\u00fc " , "/foo/bar%C3%BC" ), # UTF-8 encoded
800
789
# unicode
801
790
("/foo/bar\uabcd " , "/foo/bar%EA%AF%8D" ), # UTF-8 encoded
802
- ]
803
- for arg , result in cases :
804
- self .assertEqual (escape_path (arg ), result )
791
+ ])
792
+ def test_escape_path ( self , arg , result ) :
793
+ self .assertEqual (escape_path (arg ), result )
805
794
806
795
def test_request_path (self ):
807
796
# with parameters
0 commit comments