| 201 | type SMTPServerConfigureFunc func(*smtp.Server) |
| 202 | |
| 203 | func SMTPServer(t *testing.T, addr string, fn ...SMTPServerConfigureFunc) (*SMTPBackend, *smtp.Server) { |
| 204 | t.Helper() |
| 205 | |
| 206 | l, err := net.Listen("tcp", addr) |
| 207 | if err != nil { |
| 208 | t.Fatal(err) |
| 209 | } |
| 210 | |
| 211 | be := new(SMTPBackend) |
| 212 | s := smtp.NewServer(be) |
| 213 | s.Domain = "localhost" |
| 214 | s.AllowInsecureAuth = true |
| 215 | for _, f := range fn { |
| 216 | f(s) |
| 217 | } |
| 218 | |
| 219 | go func() { |
| 220 | if err := s.Serve(l); err != nil { |
| 221 | t.Error(err) |
| 222 | } |
| 223 | }() |
| 224 | |
| 225 | // Dial it once it make sure Server completes its initialization before |
| 226 | // we try to use it. Notably, if test fails before connecting to the server, |
| 227 | // it will call Server.Close which will call Server.listener.Close with a |
| 228 | // nil Server.listener (Serve sets it to a non-nil value, so it is racy and |
| 229 | // happens only sometimes). |
| 230 | testConn, err := net.Dial("tcp", addr) |
| 231 | require.NoError(t, err) |
| 232 | require.NoError(t, testConn.Close()) |
| 233 | |
| 234 | return be, s |
| 235 | } |
| 236 | |
| 237 | // RSA 1024, valid for *.example.invalid, 127.0.0.1, 127.0.0.2,, 127.0.0.3 |
| 238 | // until Nov 18 17:13:45 2029 GMT. |