Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitba9f533

Browse files
[3.13]gh-71339: Use new assertion methods in the http tests (GH-129058) (GH-132500)
(cherry picked from commit7076d07)Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent6126b7c commitba9f533

File tree

4 files changed

+32
-31
lines changed

4 files changed

+32
-31
lines changed

‎Lib/test/test_http_cookiejar.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
importre
77
fromtest.supportimportos_helper
88
fromtest.supportimportwarnings_helper
9+
fromtest.support.testcaseimportExtraAssertions
910
importtime
1011
importunittest
1112
importurllib.request
@@ -1436,7 +1437,7 @@ def cookiejar_from_cookie_headers(headers):
14361437
self.assertIsNone(cookie.expires)
14371438

14381439

1439-
classLWPCookieTests(unittest.TestCase):
1440+
classLWPCookieTests(unittest.TestCase,ExtraAssertions):
14401441
# Tests taken from libwww-perl, with a few modifications and additions.
14411442

14421443
deftest_netscape_example_1(self):
@@ -1528,7 +1529,7 @@ def test_netscape_example_1(self):
15281529
h=req.get_header("Cookie")
15291530
self.assertIn("PART_NUMBER=ROCKET_LAUNCHER_0001",h)
15301531
self.assertIn("CUSTOMER=WILE_E_COYOTE",h)
1531-
self.assertTrue(h.startswith("SHIPPING=FEDEX;"))
1532+
self.assertStartsWith(h,"SHIPPING=FEDEX;")
15321533

15331534
deftest_netscape_example_2(self):
15341535
# Second Example transaction sequence:

‎Lib/test/test_http_cookies.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
fromhttpimportcookies
77
importpickle
88
fromtestimportsupport
9+
fromtest.support.testcaseimportExtraAssertions
910

1011

11-
classCookieTests(unittest.TestCase):
12+
classCookieTests(unittest.TestCase,ExtraAssertions):
1213

1314
deftest_basic(self):
1415
cases= [
@@ -180,7 +181,7 @@ def test_special_attrs(self):
180181
C=cookies.SimpleCookie('Customer="WILE_E_COYOTE"')
181182
C['Customer']['expires']=0
182183
# can't test exact output, it always depends on current date/time
183-
self.assertTrue(C.output().endswith('GMT'))
184+
self.assertEndsWith(C.output(),'GMT')
184185

185186
# loading 'expires'
186187
C=cookies.SimpleCookie()

‎Lib/test/test_httplib.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
fromtestimportsupport
1717
fromtest.supportimportos_helper
1818
fromtest.supportimportsocket_helper
19+
fromtest.support.testcaseimportExtraAssertions
1920

2021
support.requires_working_socket(module=True)
2122

@@ -134,7 +135,7 @@ def connect(self):
134135
defcreate_connection(self,*pos,**kw):
135136
returnFakeSocket(*self.fake_socket_args)
136137

137-
classHeaderTests(TestCase):
138+
classHeaderTests(TestCase,ExtraAssertions):
138139
deftest_auto_headers(self):
139140
# Some headers are added automatically, but should not be added by
140141
# .request() if they are explicitly set.
@@ -273,31 +274,31 @@ def test_ipv6host_header(self):
273274
sock=FakeSocket('')
274275
conn.sock=sock
275276
conn.request('GET','/foo')
276-
self.assertTrue(sock.data.startswith(expected))
277+
self.assertStartsWith(sock.data,expected)
277278

278279
expected=b'GET /foo HTTP/1.1\r\nHost: [2001:102A::]\r\n' \
279280
b'Accept-Encoding: identity\r\n\r\n'
280281
conn=client.HTTPConnection('[2001:102A::]')
281282
sock=FakeSocket('')
282283
conn.sock=sock
283284
conn.request('GET','/foo')
284-
self.assertTrue(sock.data.startswith(expected))
285+
self.assertStartsWith(sock.data,expected)
285286

286287
expected=b'GET /foo HTTP/1.1\r\nHost: [fe80::]\r\n' \
287288
b'Accept-Encoding: identity\r\n\r\n'
288289
conn=client.HTTPConnection('[fe80::%2]')
289290
sock=FakeSocket('')
290291
conn.sock=sock
291292
conn.request('GET','/foo')
292-
self.assertTrue(sock.data.startswith(expected))
293+
self.assertStartsWith(sock.data,expected)
293294

294295
expected=b'GET /foo HTTP/1.1\r\nHost: [fe80::]:81\r\n' \
295296
b'Accept-Encoding: identity\r\n\r\n'
296297
conn=client.HTTPConnection('[fe80::%2]:81')
297298
sock=FakeSocket('')
298299
conn.sock=sock
299300
conn.request('GET','/foo')
300-
self.assertTrue(sock.data.startswith(expected))
301+
self.assertStartsWith(sock.data,expected)
301302

302303
deftest_malformed_headers_coped_with(self):
303304
# Issue 19996
@@ -335,9 +336,9 @@ def test_parse_all_octets(self):
335336
self.assertIsNotNone(resp.getheader('obs-text'))
336337
self.assertIn('obs-text',resp.msg)
337338
forfoldedin (resp.getheader('obs-fold'),resp.msg['obs-fold']):
338-
self.assertTrue(folded.startswith('text'))
339+
self.assertStartsWith(folded,'text')
339340
self.assertIn(' folded with space',folded)
340-
self.assertTrue(folded.endswith('folded with tab'))
341+
self.assertEndsWith(folded,'folded with tab')
341342

342343
deftest_invalid_headers(self):
343344
conn=client.HTTPConnection('example.com')
@@ -537,7 +538,7 @@ def _parse_chunked(self, data):
537538
returnb''.join(body)
538539

539540

540-
classBasicTest(TestCase):
541+
classBasicTest(TestCase,ExtraAssertions):
541542
deftest_dir_with_added_behavior_on_status(self):
542543
# see issue40084
543544
self.assertTrue({'description','name','phrase','value'}<=set(dir(HTTPStatus(404))))
@@ -989,8 +990,7 @@ def test_send_file(self):
989990
sock=FakeSocket(body)
990991
conn.sock=sock
991992
conn.request('GET','/foo',body)
992-
self.assertTrue(sock.data.startswith(expected),'%r != %r'%
993-
(sock.data[:len(expected)],expected))
993+
self.assertStartsWith(sock.data,expected)
994994

995995
deftest_send(self):
996996
expected=b'this is a test this is only a test'
@@ -1494,7 +1494,7 @@ def _encode_request(self, str_url):
14941494
conn.putrequest('GET','/☃')
14951495

14961496

1497-
classExtendedReadTest(TestCase):
1497+
classExtendedReadTest(TestCase,ExtraAssertions):
14981498
"""
14991499
Test peek(), read1(), readline()
15001500
"""
@@ -1553,7 +1553,7 @@ def mypeek(n=-1):
15531553
# then unbounded peek
15541554
p2=resp.peek()
15551555
self.assertGreaterEqual(len(p2),len(p))
1556-
self.assertTrue(p2.startswith(p))
1556+
self.assertStartsWith(p2,p)
15571557
next=resp.read(len(p2))
15581558
self.assertEqual(next,p2)
15591559
else:
@@ -1578,7 +1578,7 @@ def _verify_readline(self, readline, expected, limit=5):
15781578
line=readline(limit)
15791579
iflineandline!=b"foo":
15801580
iflen(line)<5:
1581-
self.assertTrue(line.endswith(b"\n"))
1581+
self.assertEndsWith(line,b"\n")
15821582
all.append(line)
15831583
ifnotline:
15841584
break
@@ -1687,7 +1687,7 @@ def readline(self, limit):
16871687
raise
16881688

16891689

1690-
classOfflineTest(TestCase):
1690+
classOfflineTest(TestCase,ExtraAssertions):
16911691
deftest_all(self):
16921692
# Documented objects defined in the module should be in __all__
16931693
expected= {"responses"}# Allowlist documented dict() object
@@ -1773,7 +1773,7 @@ def test_client_constants(self):
17731773
]
17741774
forconstinexpected:
17751775
withself.subTest(constant=const):
1776-
self.assertTrue(hasattr(client,const))
1776+
self.assertHasAttr(client,const)
17771777

17781778

17791779
classSourceAddressTest(TestCase):
@@ -2241,7 +2241,7 @@ def test_getting_header_defaultint(self):
22412241
header=self.resp.getheader('No-Such-Header',default=42)
22422242
self.assertEqual(header,42)
22432243

2244-
classTunnelTests(TestCase):
2244+
classTunnelTests(TestCase,ExtraAssertions):
22452245
defsetUp(self):
22462246
response_text= (
22472247
'HTTP/1.1 200 OK\r\n\r\n'# Reply to CONNECT
@@ -2415,8 +2415,7 @@ def test_tunnel_connect_single_send_connection_setup(self):
24152415
msg=f'unexpected number of send calls:{mock_send.mock_calls}')
24162416
proxy_setup_data_sent=mock_send.mock_calls[0][1][0]
24172417
self.assertIn(b'CONNECT destination.com',proxy_setup_data_sent)
2418-
self.assertTrue(
2419-
proxy_setup_data_sent.endswith(b'\r\n\r\n'),
2418+
self.assertEndsWith(proxy_setup_data_sent,b'\r\n\r\n',
24202419
msg=f'unexpected proxy data sent{proxy_setup_data_sent!r}')
24212420

24222421
deftest_connect_put_request(self):

‎Lib/test/test_httpservers.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
fromtest.supportimport (
3434
is_apple,os_helper,requires_subprocess,threading_helper
3535
)
36+
fromtest.support.testcaseimportExtraAssertions
3637

3738
support.requires_working_socket(module=True)
3839

@@ -66,7 +67,7 @@ def stop(self):
6667
self.join()
6768

6869

69-
classBaseTestCase(unittest.TestCase):
70+
classBaseTestCase(unittest.TestCase,ExtraAssertions):
7071
defsetUp(self):
7172
self._threads=threading_helper.threading_setup()
7273
os.environ=os_helper.EnvironmentVarGuard()
@@ -335,8 +336,7 @@ def test_get(self):
335336
self.con.request('GET','/')
336337
self.con.getresponse()
337338

338-
self.assertTrue(
339-
err.getvalue().endswith('"GET / HTTP/1.1" 200 -\n'))
339+
self.assertEndsWith(err.getvalue(),'"GET / HTTP/1.1" 200 -\n')
340340

341341
deftest_err(self):
342342
self.con=http.client.HTTPConnection(self.HOST,self.PORT)
@@ -347,8 +347,8 @@ def test_err(self):
347347
self.con.getresponse()
348348

349349
lines=err.getvalue().split('\n')
350-
self.assertTrue(lines[0].endswith('code 404, message File not found'))
351-
self.assertTrue(lines[1].endswith('"ERROR / HTTP/1.1" 404 -'))
350+
self.assertEndsWith(lines[0],'code 404, message File not found')
351+
self.assertEndsWith(lines[1],'"ERROR / HTTP/1.1" 404 -')
352352

353353

354354
classSimpleHTTPServerTestCase(BaseTestCase):
@@ -550,7 +550,7 @@ def test_get_dir_redirect_location_domain_injection_bug(self):
550550
response=self.request(attack_url)
551551
self.check_status_and_reason(response,HTTPStatus.MOVED_PERMANENTLY)
552552
location=response.getheader('Location')
553-
self.assertFalse(location.startswith('//'),msg=location)
553+
self.assertNotStartsWith(location,'//')
554554
self.assertEqual(location,expected_location,
555555
msg='Expected Location header to start with a single / and '
556556
'end with a / as this is a directory redirect.')
@@ -573,7 +573,7 @@ def test_get_dir_redirect_location_domain_injection_bug(self):
573573
# We're just ensuring that the scheme and domain make it through, if
574574
# there are or aren't multiple slashes at the start of the path that
575575
# follows that isn't important in this Location: header.
576-
self.assertTrue(location.startswith('https://pypi.org/'),msg=location)
576+
self.assertStartsWith(location,'https://pypi.org/')
577577

578578
deftest_get(self):
579579
#constructs the path relative to the root directory of the HTTPServer
@@ -1074,7 +1074,7 @@ def numWrites(self):
10741074
returnlen(self.datas)
10751075

10761076

1077-
classBaseHTTPRequestHandlerTestCase(unittest.TestCase):
1077+
classBaseHTTPRequestHandlerTestCase(unittest.TestCase,ExtraAssertions):
10781078
"""Test the functionality of the BaseHTTPServer.
10791079
10801080
Test the support for the Expect 100-continue header.
@@ -1162,7 +1162,7 @@ def test_extra_space(self):
11621162
b'Host: dummy\r\n'
11631163
b'\r\n'
11641164
)
1165-
self.assertTrue(result[0].startswith(b'HTTP/1.1 400 '))
1165+
self.assertStartsWith(result[0],b'HTTP/1.1 400 ')
11661166
self.verify_expected_headers(result[1:result.index(b'\r\n')])
11671167
self.assertFalse(self.handler.get_called)
11681168

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp