diff --git a/Mailosaur.Test/DevicesTests.cs b/Mailosaur.Test/DevicesTests.cs index 3843047..a29c354 100644 --- a/Mailosaur.Test/DevicesTests.cs +++ b/Mailosaur.Test/DevicesTests.cs @@ -2,12 +2,10 @@ using System.Linq; using Mailosaur.Models; using Xunit; -using System.Threading.Tasks; -using System.Collections.Generic; namespace Mailosaur.Test { - public class DevicesTests + public class DevicesTests : IDisposable { private MailosaurClient m_Client; private static string s_ApiKey = Environment.GetEnvironmentVariable("MAILOSAUR_API_KEY"); @@ -55,5 +53,10 @@ public void OtpViaSharedSecretTest() OtpResult otpResult = m_Client.Devices.Otp(sharedSecret); Assert.Equal(6, otpResult.Code.Length); } + + public void Dispose() + { + m_Client.Dispose(); + } } } diff --git a/Mailosaur.Test/EmailsTests.cs b/Mailosaur.Test/EmailsTests.cs index 6934d15..03c2bf5 100644 --- a/Mailosaur.Test/EmailsTests.cs +++ b/Mailosaur.Test/EmailsTests.cs @@ -2,7 +2,6 @@ using System.Linq; using Mailosaur.Models; using Xunit; -using System.Threading.Tasks; using System.Collections.Generic; using System.IO; @@ -39,7 +38,10 @@ public EmailsFixture() emails = client.Messages.List(server).Items; } - public void Dispose() { } + public void Dispose() + { + client.Dispose(); + } } public class EmailsTests : IClassFixture diff --git a/Mailosaur.Test/ErrorsTests.cs b/Mailosaur.Test/ErrorsTests.cs index 9e251ae..a35404a 100644 --- a/Mailosaur.Test/ErrorsTests.cs +++ b/Mailosaur.Test/ErrorsTests.cs @@ -9,38 +9,44 @@ public class ErrorsTests [Fact] public void UnauthorizedTest() { - var client = new MailosaurClient("invalid_key"); - var ex = Assert.Throws(delegate + using (var client = new MailosaurClient("invalid_key")) { - client.Servers.List(); - }); + var ex = Assert.Throws(delegate + { + client.Servers.List(); + }); - Assert.Equal("Authentication failed, check your API key.", ex.Message); + Assert.Equal("Authentication failed, check your API key.", ex.Message); + } } [Fact] public void NotFoundTest() { - var client = new MailosaurClient(Environment.GetEnvironmentVariable("MAILOSAUR_API_KEY")); - var ex = Assert.Throws(delegate + using (var client = new MailosaurClient(Environment.GetEnvironmentVariable("MAILOSAUR_API_KEY"))) { - client.Servers.Get("not_found"); - }); + var ex = Assert.Throws(delegate + { + client.Servers.Get("not_found"); + }); - Assert.Equal("Not found, check input parameters.", ex.Message); + Assert.Equal("Not found, check input parameters.", ex.Message); + } } [Fact] public void BadRequestTest() { - var client = new MailosaurClient(Environment.GetEnvironmentVariable("MAILOSAUR_API_KEY")); - var options = new ServerCreateOptions(""); - var ex = Assert.Throws(delegate + using (var client = new MailosaurClient(Environment.GetEnvironmentVariable("MAILOSAUR_API_KEY"))) { - client.Servers.Create(options); - }); + var options = new ServerCreateOptions(""); + var ex = Assert.Throws(delegate + { + client.Servers.Create(options); + }); - Assert.Equal("(name) Servers need a name\r\n", ex.Message); + Assert.Equal("(name) Servers need a name\r\n", ex.Message); + } } } } diff --git a/Mailosaur.Test/FilesTests.cs b/Mailosaur.Test/FilesTests.cs index bfcdc6a..0ef7484 100644 --- a/Mailosaur.Test/FilesTests.cs +++ b/Mailosaur.Test/FilesTests.cs @@ -1,9 +1,6 @@ using System; -using System.Linq; using Mailosaur.Models; using Xunit; -using System.Threading.Tasks; -using System.Collections.Generic; using System.IO; using System.Text; @@ -41,7 +38,10 @@ public FilesFixture() }); } - public void Dispose() { } + public void Dispose() + { + client.Dispose(); + } } public class FilesTests : IClassFixture @@ -78,4 +78,4 @@ public void GetAttachmentTest() Assert.Equal(attachment.Length, bytes.Length); } } -} \ No newline at end of file +} diff --git a/Mailosaur.Test/ServersTests.cs b/Mailosaur.Test/ServersTests.cs index 942e99c..556afa5 100644 --- a/Mailosaur.Test/ServersTests.cs +++ b/Mailosaur.Test/ServersTests.cs @@ -2,12 +2,11 @@ using System.Linq; using Mailosaur.Models; using Xunit; -using System.Threading.Tasks; using System.Collections.Generic; namespace Mailosaur.Test { - public class ServersTests + public class ServersTests : IDisposable { private MailosaurClient m_Client; private static string s_ApiKey = Environment.GetEnvironmentVariable("MAILOSAUR_API_KEY"); @@ -92,5 +91,10 @@ public void FailedCreateTest() Assert.Equal(400, ex.HttpStatusCode); Assert.Contains("{\"type\":", ex.HttpResponseBody); } + + public void Dispose() + { + m_Client.Dispose(); + } } } diff --git a/Mailosaur.Test/UsageTests.cs b/Mailosaur.Test/UsageTests.cs index b16d516..bb09d0b 100644 --- a/Mailosaur.Test/UsageTests.cs +++ b/Mailosaur.Test/UsageTests.cs @@ -2,20 +2,19 @@ using System.Linq; using Mailosaur.Models; using Xunit; -using System.Threading.Tasks; -using System.Collections.Generic; namespace Mailosaur.Test { - public class UsageTests + public class UsageTests : IDisposable { private MailosaurClient m_Client; private static string s_ApiKey = Environment.GetEnvironmentVariable("MAILOSAUR_API_KEY"); private string s_BaseUrl = Environment.GetEnvironmentVariable("MAILOSAUR_BASE_URL") ?? "https://mailosaur.com/"; - + public UsageTests() { - if (string.IsNullOrWhiteSpace(s_ApiKey)) { + if (string.IsNullOrWhiteSpace(s_ApiKey)) + { throw new Exception("Missing necessary environment variables - refer to README.md"); } @@ -46,5 +45,10 @@ public void TransactionsTest() Assert.IsType(result.Items[0].Email); Assert.IsType(result.Items[0].Sms); } + + public void Dispose() + { + m_Client.Dispose(); + } } } diff --git a/Mailosaur/MailosaurClient.cs b/Mailosaur/MailosaurClient.cs index bf4a793..68bd1b9 100644 --- a/Mailosaur/MailosaurClient.cs +++ b/Mailosaur/MailosaurClient.cs @@ -1,14 +1,12 @@ namespace Mailosaur { using System; - using System.Collections.Generic; - using System.IO; using System.Net.Http; using System.Net.Http.Headers; using System.Text; using Mailosaur.Operations; - public class MailosaurClient + public class MailosaurClient : IDisposable { public Servers Servers { get; private set; } public Messages Messages { get; private set; } @@ -54,5 +52,13 @@ public MailosaurClient(string apiKey, string baseUrl) Usage = new Usage(_client); Devices = new Devices(_client); } + + /// + /// Disposes relevant resources + /// + public void Dispose() + { + _client?.Dispose(); + } } }