Skip to content

Commit 15621d4

Browse files
Move tests to SmtpServer 11 (#230)
1 parent ed69a64 commit 15621d4

4 files changed

Lines changed: 61 additions & 22 deletions

File tree

test/NLog.MailKit.Tests/IntegrationTests/MailTargetIntegrationTests.cs

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5+
using System.Net.Sockets;
56
using System.Threading;
67
using System.Threading.Tasks;
78
using NLog.Config;
@@ -80,10 +81,9 @@ public void SendMailWithHeaderFooter()
8081
mailTarget.Footer = " *** End *** ";
8182
}, 1);
8283

83-
var mailMessage = transactions.LastOrDefault()?.Message as SmtpServer.Mail.ITextMessage;
84-
Assert.NotNull(mailMessage);
85-
mailMessage.Content.Position = 0;
86-
var mailBody = new StreamReader(mailMessage.Content).ReadToEnd();
84+
var receivedMessage = transactions.LastOrDefault();
85+
Assert.NotNull(receivedMessage);
86+
var mailBody = receivedMessage.GetBodyAsString();
8787
Assert.NotNull(mailBody);
8888
Assert.Contains("*** Begin ***", mailBody);
8989
Assert.Contains("*** End ***", mailBody);
@@ -131,16 +131,15 @@ public void SendMailBatch()
131131
logger.Info("hello starting mail!");
132132
}, 1);
133133

134-
var mailMessage = transactions.LastOrDefault()?.Message as SmtpServer.Mail.ITextMessage;
135-
Assert.NotNull(mailMessage);
136-
mailMessage.Content.Position = 0;
137-
var mailBody = new StreamReader(mailMessage.Content).ReadToEnd();
134+
var receivedMessage = transactions.LastOrDefault();
135+
Assert.NotNull(receivedMessage);
136+
var mailBody = receivedMessage.GetBodyAsString();
138137
Assert.NotNull(mailBody);
139138
Assert.Contains("hello starting mail!", mailBody);
140139
Assert.Contains("hello first mail!", mailBody);
141140
}
142141

143-
private static IList<IMessageTransaction> SendTest(Action createConfig, int toCount)
142+
private static IList<ReceivedMessage> SendTest(Action createConfig, int toCount)
144143
{
145144
var countdownEvent = new CountdownEvent(1);
146145

@@ -151,6 +150,8 @@ private static IList<IMessageTransaction> SendTest(Action createConfig, int toCo
151150

152151
Task.Run(async () => await smtpServer.StartAsync(cancellationToken.Token), cancellationToken.Token);
153152

153+
WaitForSmtpServer();
154+
154155
createConfig();
155156

156157
var logger = LogManager.GetLogger("logger1");
@@ -178,14 +179,26 @@ private static void AssertMailBox(string expected, IMailbox mailbox)
178179
Assert.Equal(expected, mailbox.User + "@" + mailbox.Host);
179180
}
180181

181-
private static string GetReceivedBody(IMessageTransaction receivedMessage)
182+
private static string GetReceivedBody(ReceivedMessage receivedMessage)
182183
{
183-
if (receivedMessage.Message is ITextMessage textMessage)
184+
return receivedMessage.GetBodyAsString();
185+
}
186+
187+
private static void WaitForSmtpServer(int port = 587, int maxAttempts = 50)
188+
{
189+
for (var i = 0; i < maxAttempts; i++)
184190
{
185-
var streamReader = new StreamReader(textMessage.Content);
186-
return streamReader.ReadToEnd();
191+
try
192+
{
193+
using var client = new TcpClient();
194+
client.Connect("127.0.0.1", port);
195+
return;
196+
}
197+
catch (SocketException)
198+
{
199+
Thread.Sleep(100);
200+
}
187201
}
188-
throw new NotSupportedException("only ITextMessage supported");
189202
}
190203

191204
private static SmtpServer.SmtpServer CreateSmtpServer(MessageStore store)
@@ -197,12 +210,13 @@ private static SmtpServer.SmtpServer CreateSmtpServer(MessageStore store)
197210
var options = new SmtpServerOptionsBuilder()
198211
.ServerName("localhost")
199212
.Port(25, 587)
200-
.MessageStore(store)
201-
.UserAuthenticator(userAuthenticatorFactory)
202213
.Build();
203214

204-
var smtpServer = new SmtpServer.SmtpServer(options);
205-
return smtpServer;
215+
var serviceProvider = new SmtpServer.ComponentModel.ServiceProvider();
216+
serviceProvider.Add((SmtpServer.Storage.IMessageStoreFactory)store);
217+
serviceProvider.Add(userAuthenticatorFactory);
218+
219+
return new SmtpServer.SmtpServer(options, serviceProvider);
206220
}
207221

208222
private static MailTarget CreateNLogConfig()

test/NLog.MailKit.Tests/IntegrationTests/Util/MessageStore.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Buffers;
23
using System.Collections.Generic;
34
using System.Threading;
45
using System.Threading.Tasks;
@@ -10,7 +11,7 @@ namespace NLog.MailKit.Tests.IntegrationTests.Util
1011
{
1112
public class MessageStore : IMessageStore, IMessageStoreFactory
1213
{
13-
public IList<IMessageTransaction> ReceivedTransactions { get; } = new List<IMessageTransaction>();
14+
public IList<ReceivedMessage> ReceivedTransactions { get; } = new List<ReceivedMessage>();
1415

1516
private readonly CountdownEvent _countdownEvent;
1617

@@ -27,9 +28,9 @@ public IMessageStore CreateInstance(ISessionContext context)
2728
#region Implementation of IMessageStore
2829

2930
/// <inheritdoc />
30-
public Task<SmtpResponse> SaveAsync(ISessionContext context, IMessageTransaction transaction, CancellationToken cancellationToken)
31+
public Task<SmtpResponse> SaveAsync(ISessionContext context, IMessageTransaction transaction, ReadOnlySequence<byte> buffer, CancellationToken cancellationToken)
3132
{
32-
ReceivedTransactions.Add(transaction);
33+
ReceivedTransactions.Add(new ReceivedMessage(transaction, buffer.ToArray()));
3334
_countdownEvent.Signal();
3435
return Task.FromResult(SmtpResponse.Ok);
3536
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Collections.Generic;
2+
using System.Text;
3+
using SmtpServer;
4+
using SmtpServer.Mail;
5+
6+
namespace NLog.MailKit.Tests.IntegrationTests.Util
7+
{
8+
public class ReceivedMessage
9+
{
10+
private readonly IMessageTransaction _transaction;
11+
12+
public ReceivedMessage(IMessageTransaction transaction, byte[] body)
13+
{
14+
_transaction = transaction;
15+
Body = body;
16+
}
17+
18+
public IMailbox From => _transaction.From;
19+
public IList<IMailbox> To => _transaction.To;
20+
public byte[] Body { get; }
21+
22+
public string GetBodyAsString() => Encoding.UTF8.GetString(Body);
23+
}
24+
}

test/NLog.MailKit.Tests/NLog.MailKit.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<ItemGroup>
1212
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
13-
<PackageReference Include="SmtpServer" Version="7.2.0" />
13+
<PackageReference Include="SmtpServer" Version="11.1.0" />
1414
<PackageReference Include="xunit" Version="2.9.2" />
1515
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
1616
<PrivateAssets>all</PrivateAssets>

0 commit comments

Comments
 (0)