@@ -56,7 +56,7 @@ def server(evt, buf, serv):
56
56
serv .close ()
57
57
evt .set ()
58
58
59
- class GeneralTests ( unittest . TestCase ) :
59
+ class GeneralTests :
60
60
61
61
def setUp (self ):
62
62
smtplib .socket = mock_socket
@@ -75,86 +75,101 @@ def testQuoteData(self):
75
75
def testBasic1 (self ):
76
76
mock_socket .reply_with (b"220 Hola mundo" )
77
77
# connects
78
- smtp = smtplib . SMTP (HOST , self .port )
79
- smtp .close ()
78
+ client = self . client (HOST , self .port )
79
+ client .close ()
80
80
81
81
def testSourceAddress (self ):
82
82
mock_socket .reply_with (b"220 Hola mundo" )
83
83
# connects
84
- smtp = smtplib . SMTP (HOST , self .port ,
85
- source_address = ('127.0.0.1' ,19876 ))
86
- self .assertEqual (smtp .source_address , ('127.0.0.1' , 19876 ))
87
- smtp .close ()
84
+ client = self . client (HOST , self .port ,
85
+ source_address = ('127.0.0.1' ,19876 ))
86
+ self .assertEqual (client .source_address , ('127.0.0.1' , 19876 ))
87
+ client .close ()
88
88
89
89
def testBasic2 (self ):
90
90
mock_socket .reply_with (b"220 Hola mundo" )
91
91
# connects, include port in host name
92
- smtp = smtplib . SMTP ("%s:%s" % (HOST , self .port ))
93
- smtp .close ()
92
+ client = self . client ("%s:%s" % (HOST , self .port ))
93
+ client .close ()
94
94
95
95
def testLocalHostName (self ):
96
96
mock_socket .reply_with (b"220 Hola mundo" )
97
97
# check that supplied local_hostname is used
98
- smtp = smtplib . SMTP (HOST , self .port , local_hostname = "testhost" )
99
- self .assertEqual (smtp .local_hostname , "testhost" )
100
- smtp .close ()
98
+ client = self . client (HOST , self .port , local_hostname = "testhost" )
99
+ self .assertEqual (client .local_hostname , "testhost" )
100
+ client .close ()
101
101
102
102
def testTimeoutDefault (self ):
103
103
mock_socket .reply_with (b"220 Hola mundo" )
104
104
self .assertIsNone (mock_socket .getdefaulttimeout ())
105
105
mock_socket .setdefaulttimeout (30 )
106
106
self .assertEqual (mock_socket .getdefaulttimeout (), 30 )
107
107
try :
108
- smtp = smtplib . SMTP (HOST , self .port )
108
+ client = self . client (HOST , self .port )
109
109
finally :
110
110
mock_socket .setdefaulttimeout (None )
111
- self .assertEqual (smtp .sock .gettimeout (), 30 )
112
- smtp .close ()
111
+ self .assertEqual (client .sock .gettimeout (), 30 )
112
+ client .close ()
113
113
114
114
def testTimeoutNone (self ):
115
115
mock_socket .reply_with (b"220 Hola mundo" )
116
116
self .assertIsNone (socket .getdefaulttimeout ())
117
117
socket .setdefaulttimeout (30 )
118
118
try :
119
- smtp = smtplib . SMTP (HOST , self .port , timeout = None )
119
+ client = self . client (HOST , self .port , timeout = None )
120
120
finally :
121
121
socket .setdefaulttimeout (None )
122
- self .assertIsNone (smtp .sock .gettimeout ())
123
- smtp .close ()
122
+ self .assertIsNone (client .sock .gettimeout ())
123
+ client .close ()
124
124
125
125
def testTimeoutZero (self ):
126
126
mock_socket .reply_with (b"220 Hola mundo" )
127
127
with self .assertRaises (ValueError ):
128
- smtplib . SMTP (HOST , self .port , timeout = 0 )
128
+ self . client (HOST , self .port , timeout = 0 )
129
129
130
130
def testTimeoutValue (self ):
131
131
mock_socket .reply_with (b"220 Hola mundo" )
132
- smtp = smtplib . SMTP (HOST , self .port , timeout = 30 )
133
- self .assertEqual (smtp .sock .gettimeout (), 30 )
134
- smtp .close ()
132
+ client = self . client (HOST , self .port , timeout = 30 )
133
+ self .assertEqual (client .sock .gettimeout (), 30 )
134
+ client .close ()
135
135
136
136
def test_debuglevel (self ):
137
137
mock_socket .reply_with (b"220 Hello world" )
138
- smtp = smtplib . SMTP ()
139
- smtp .set_debuglevel (1 )
138
+ client = self . client ()
139
+ client .set_debuglevel (1 )
140
140
with support .captured_stderr () as stderr :
141
- smtp .connect (HOST , self .port )
142
- smtp .close ()
141
+ client .connect (HOST , self .port )
142
+ client .close ()
143
143
expected = re .compile (r"^connect:" , re .MULTILINE )
144
144
self .assertRegex (stderr .getvalue (), expected )
145
145
146
146
def test_debuglevel_2 (self ):
147
147
mock_socket .reply_with (b"220 Hello world" )
148
- smtp = smtplib . SMTP ()
149
- smtp .set_debuglevel (2 )
148
+ client = self . client ()
149
+ client .set_debuglevel (2 )
150
150
with support .captured_stderr () as stderr :
151
- smtp .connect (HOST , self .port )
152
- smtp .close ()
151
+ client .connect (HOST , self .port )
152
+ client .close ()
153
153
expected = re .compile (r"^\d{2}:\d{2}:\d{2}\.\d{6} connect: " ,
154
154
re .MULTILINE )
155
155
self .assertRegex (stderr .getvalue (), expected )
156
156
157
157
158
+ class SMTPGeneralTests (GeneralTests , unittest .TestCase ):
159
+
160
+ client = smtplib .SMTP
161
+
162
+
163
+ class LMTPGeneralTests (GeneralTests , unittest .TestCase ):
164
+
165
+ client = smtplib .LMTP
166
+
167
+ def testTimeoutZero (self ):
168
+ super ().testTimeoutZero ()
169
+ local_host = '/some/local/lmtp/delivery/program'
170
+ with self .assertRaises (ValueError ):
171
+ self .client (local_host , timeout = 0 )
172
+
158
173
# Test server thread using the specified SMTP server class
159
174
def debugging_server (serv , serv_evt , client_evt ):
160
175
serv_evt .set ()
0 commit comments