Inital commit

This commit is contained in:
2024-09-07 15:31:11 -04:00
commit 5a3402ecfe
11 changed files with 197 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
bin/
obj/
/packages/
riderModule.iml
/_ReSharper.Caches/
+22
View File
@@ -0,0 +1,22 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MimeSharp", "MimeSharp\MimeSharp.csproj", "{E70944CA-99E3-4C2D-B5C6-10384AB92A06}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MimeSharpUnitTests", "MimeSharpUnitTests\MimeSharpUnitTests.csproj", "{1B83EAFB-8748-4D70-80FF-863928DE59F3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E70944CA-99E3-4C2D-B5C6-10384AB92A06}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E70944CA-99E3-4C2D-B5C6-10384AB92A06}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E70944CA-99E3-4C2D-B5C6-10384AB92A06}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E70944CA-99E3-4C2D-B5C6-10384AB92A06}.Release|Any CPU.Build.0 = Release|Any CPU
{1B83EAFB-8748-4D70-80FF-863928DE59F3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1B83EAFB-8748-4D70-80FF-863928DE59F3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1B83EAFB-8748-4D70-80FF-863928DE59F3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1B83EAFB-8748-4D70-80FF-863928DE59F3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
+7
View File
@@ -0,0 +1,7 @@
namespace MimeSharp;
public class AuthTypes
{
public string Domain { get; } = "Basic-AD";
public string Cloud { get; } = "Basic-Cloud";
}
+14
View File
@@ -0,0 +1,14 @@
namespace MimeSharp;
public class DateTimeHelper
{
public static string GetRfc1123()
{
return DateTime.Now.ToString("R");
}
public static string GetRfc3339()
{
return DateTime.Now.ToString("yyyy-MM-dd'T'HH:mm:ssZ");
}
}
+11
View File
@@ -0,0 +1,11 @@
namespace MimeSharp;
public class Endpoints
{
public static string API = "/api";
//******* Login Namespace ********//
public static string LoginNamespace = $"{API}/login";
public static string Login = $"{LoginNamespace}/login";
public static string Logout = $"{LoginNamespace}/logout";
}
+24
View File
@@ -0,0 +1,24 @@
namespace MimeSharp;
public static class Extensions
{
private static readonly string BaseHostname = "-api.mimecast.com";
private static readonly List<MimecastRegion> PreProd = [MimecastRegion.QAN, MimecastRegion.DEV];
public static String GetHostname(this MimecastRegion region)
{
return region.IsPreProd() ?
$"{region.GetRegionCode()}--api.mimecast--{region.GetRegionCode()}.com" :
$"{region.GetRegionCode()}--api.mimecast.com";
}
public static String? GetRegionCode(this MimecastRegion region)
{
return Enum.GetName(typeof(MimecastRegion), region)?.ToLower();
}
public static Boolean IsPreProd(this MimecastRegion region)
{
return PreProd.Contains(region);
}
}
+9
View File
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
+14
View File
@@ -0,0 +1,14 @@
namespace MimeSharp;
public enum MimecastRegion
{
EU,
US,
ZA,
AU,
DE,
JER,
QAN,
DEV,
STG
}
+21
View File
@@ -0,0 +1,21 @@
namespace MimeSharp;
public class Urls(MimecastRegion region)
{
private const string Protocol = "https://";
public String GetLogin()
{
return GetUrl(Endpoints.Login);
}
public String GetLogout()
{
return GetUrl(Endpoints.Logout);
}
private String GetUrl(String endpoint)
{
return $"{Protocol}{region.GetHostname()}{endpoint}";
}
}
+34
View File
@@ -0,0 +1,34 @@
using System.Text;
namespace MimeSharp;
public class WebHelper
{
private readonly HttpClient _client;
public WebHelper()
{
_client = new HttpClient();
}
public WebHelper(HttpClient client)
{
_client = client;
}
public async Task<string> Post(string url, Dictionary<string, string>? headers, string body)
{
headers?.ToList().ForEach(h => _client.DefaultRequestHeaders.TryAddWithoutValidation(h.Key, h.Value));
var content = new StringContent(body, Encoding.UTF8, "application/json");
var response = await _client.PostAsync(url, content);
return await response.Content.ReadAsStringAsync();
}
public async Task<string> Get(string url, Dictionary<string, string>? headers)
{
headers?.ToList().ForEach(h => _client.DefaultRequestHeaders.TryAddWithoutValidation(h.Key, h.Value));
var response = await _client.GetAsync(url);
return await response.Content.ReadAsStringAsync();
}
}
+36
View File
@@ -0,0 +1,36 @@
using System.Net;
using System.Text;
using System.Text.Json;
using MimeSharp;
using Moq;
using Moq.Contrib.HttpClient;
namespace MimeSharpUnitTests;
public class WebHelperUnitTests
{
private readonly HttpClient _client;
[Test]
public async Task CanMakeCallUsingGet()
{
//arrange
var mockHandler = new Mock<HttpMessageHandler>(MockBehavior.Strict);
var mockResponseBody = "\"[{'id':1,'value':'1'}]\"";
var mockUrl = "https://example.com";
mockHandler.SetupRequest(HttpMethod.Get, mockUrl)
.ReturnsResponse(HttpStatusCode.OK, mockResponseBody);
// Inject the handler or client into your application code
var httpClient = mockHandler.CreateClient();
var webHelper = new WebHelper(httpClient);
//act
var actual = await webHelper.Get(mockUrl, null);
//assert
Assert.NotNull(actual);
Assert.That(actual, Is.EqualTo(mockResponseBody));
mockHandler.VerifyRequest(HttpMethod.Get, mockUrl, Times.Once());
}
}