Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(10)

Side by Side Diff: Lib/test/test_imaplib.py

Issue 4972: context managerment support in imaplib, smtplib, ftplib
Patch Set: Created 5 years, 7 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/imaplib.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 from test import support 1 from test import support
2 # If we end up with a significant number of tests that don't require 2 # If we end up with a significant number of tests that don't require
3 # threading, this test module should be split. Right now we skip 3 # threading, this test module should be split. Right now we skip
4 # them all if we don't have threading. 4 # them all if we don't have threading.
5 threading = support.import_module('threading') 5 threading = support.import_module('threading')
6 6
7 from contextlib import contextmanager 7 from contextlib import contextmanager
8 import imaplib 8 import imaplib
9 import os.path 9 import os.path
10 import socketserver 10 import socketserver
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 pass 93 pass
94 94
95 IMAP4_SSL = None 95 IMAP4_SSL = None
96 96
97 97
98 class SimpleIMAPHandler(socketserver.StreamRequestHandler): 98 class SimpleIMAPHandler(socketserver.StreamRequestHandler):
99 99
100 timeout = 1 100 timeout = 1
101 continuation = None 101 continuation = None
102 capabilities = '' 102 capabilities = ''
103
104 def setup(self):
105 super().setup()
106 self.server.logged = None
103 107
104 def _send(self, message): 108 def _send(self, message):
105 if verbose: print("SENT: %r" % message.strip()) 109 if verbose: print("SENT: %r" % message.strip())
106 self.wfile.write(message) 110 self.wfile.write(message)
107 111
108 def _send_line(self, message): 112 def _send_line(self, message):
109 self._send(message + b'\r\n') 113 self._send(message + b'\r\n')
110 114
111 def _send_textline(self, message): 115 def _send_textline(self, message):
112 self._send_line(message.encode('ASCII')) 116 self._send_line(message.encode('ASCII'))
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 next(continuation) 158 next(continuation)
155 else: 159 else:
156 self._send_tagged(tag, 'BAD', cmd + ' unknown') 160 self._send_tagged(tag, 'BAD', cmd + ' unknown')
157 161
158 def cmd_CAPABILITY(self, tag, args): 162 def cmd_CAPABILITY(self, tag, args):
159 caps = 'IMAP4rev1 ' + self.capabilities if self.capabilities else 'IMAP4 rev1' 163 caps = 'IMAP4rev1 ' + self.capabilities if self.capabilities else 'IMAP4 rev1'
160 self._send_textline('* CAPABILITY ' + caps) 164 self._send_textline('* CAPABILITY ' + caps)
161 self._send_tagged(tag, 'OK', 'CAPABILITY completed') 165 self._send_tagged(tag, 'OK', 'CAPABILITY completed')
162 166
163 def cmd_LOGOUT(self, tag, args): 167 def cmd_LOGOUT(self, tag, args):
168 self.server.logged = None
164 self._send_textline('* BYE IMAP4ref1 Server logging out') 169 self._send_textline('* BYE IMAP4ref1 Server logging out')
165 self._send_tagged(tag, 'OK', 'LOGOUT completed') 170 self._send_tagged(tag, 'OK', 'LOGOUT completed')
171
172 def cmd_LOGIN(self, tag, args):
173 self.server.logged = args[0]
174 self._send_tagged(tag, 'OK', 'LOGIN completed')
166 175
167 176
168 class BaseThreadedNetworkedTests(unittest.TestCase): 177 class BaseThreadedNetworkedTests(unittest.TestCase):
169 178
170 def make_server(self, addr, hdlr): 179 def make_server(self, addr, hdlr):
171 180
172 class MyServer(self.server_class): 181 class MyServer(self.server_class):
173 def handle_error(self, request, client_address): 182 def handle_error(self, request, client_address):
174 self.close_request(request) 183 self.close_request(request)
175 self.server_close() 184 self.server_close()
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
330 339
331 def test_linetoolong(self): 340 def test_linetoolong(self):
332 class TooLongHandler(SimpleIMAPHandler): 341 class TooLongHandler(SimpleIMAPHandler):
333 def handle(self): 342 def handle(self):
334 # Send a very long response line 343 # Send a very long response line
335 self.wfile.write(b'* OK ' + imaplib._MAXLINE*b'x' + b'\r\n') 344 self.wfile.write(b'* OK ' + imaplib._MAXLINE*b'x' + b'\r\n')
336 345
337 with self.reaped_server(TooLongHandler) as server: 346 with self.reaped_server(TooLongHandler) as server:
338 self.assertRaises(imaplib.IMAP4.error, 347 self.assertRaises(imaplib.IMAP4.error,
339 self.imap_class, *server.server_address) 348 self.imap_class, *server.server_address)
349
350 @reap_threads
351 def test_simple_with_statement(self):
352 # simplest call
353 with self.reaped_server(SimpleIMAPHandler) as server:
354 with self.imap_class(*server.server_address):
355 pass
356
357 @reap_threads
358 def test_with_statement(self):
359 with self.reaped_server(SimpleIMAPHandler) as server:
360 with self.imap_class(*server.server_address) as imap:
361 imap.login('user', 'pass')
362 self.assertEqual(server.logged, 'user')
363 self.assertIsNone(server.logged)
364
365 @reap_threads
366 def test_with_statement_logout(self):
367 # what happens if already logout in the block?
368 with self.reaped_server(SimpleIMAPHandler) as server:
369 with self.imap_class(*server.server_address) as imap:
370 imap.login('user', 'pass')
371 self.assertEqual(server.logged, 'user')
372 imap.logout()
373 self.assertIsNone(server.logged)
374 self.assertIsNone(server.logged)
340 375
341 376
342 class ThreadedNetworkedTests(BaseThreadedNetworkedTests): 377 class ThreadedNetworkedTests(BaseThreadedNetworkedTests):
343 378
344 server_class = socketserver.TCPServer 379 server_class = socketserver.TCPServer
345 imap_class = imaplib.IMAP4 380 imap_class = imaplib.IMAP4
346 381
347 382
348 @unittest.skipUnless(ssl, "SSL not available") 383 @unittest.skipUnless(ssl, "SSL not available")
349 class ThreadedNetworkedTestsSSL(BaseThreadedNetworkedTests): 384 class ThreadedNetworkedTestsSSL(BaseThreadedNetworkedTests):
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
495 tests.extend([ 530 tests.extend([
496 ThreadedNetworkedTests, ThreadedNetworkedTestsSSL, 531 ThreadedNetworkedTests, ThreadedNetworkedTestsSSL,
497 RemoteIMAPTest, RemoteIMAP_SSLTest, RemoteIMAP_STARTTLSTest, 532 RemoteIMAPTest, RemoteIMAP_SSLTest, RemoteIMAP_STARTTLSTest,
498 ]) 533 ])
499 534
500 return unittest.TestSuite([unittest.makeSuite(test) for test in tests]) 535 return unittest.TestSuite([unittest.makeSuite(test) for test in tests])
501 536
502 537
503 if __name__ == "__main__": 538 if __name__ == "__main__":
504 unittest.main() 539 unittest.main()
OLDNEW
« no previous file with comments | « Lib/imaplib.py ('k') | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+