remove MimeKitLite and use MediaTypeHeaderValue (#338)

This commit is contained in:
Stef Heyenrath
2019-08-31 19:01:44 +00:00
committed by GitHub
parent 7941894543
commit c0a43ed204
11 changed files with 205 additions and 185 deletions
+1 -1
View File
@@ -4,7 +4,7 @@
</PropertyGroup> </PropertyGroup>
<PropertyGroup> <PropertyGroup>
<VersionPrefix>1.0.29</VersionPrefix> <VersionPrefix>1.0.30</VersionPrefix>
</PropertyGroup> </PropertyGroup>
<Choose> <Choose>
+1 -1
View File
@@ -1,3 +1,3 @@
https://github.com/StefH/GitHubReleaseNotes https://github.com/StefH/GitHubReleaseNotes
GitHubReleaseNotes.exe --output CHANGELOG.md --skip-empty-releases --exclude-labels question, invalid --version 1.0.29.0 GitHubReleaseNotes.exe --output CHANGELOG.md --skip-empty-releases --exclude-labels question, invalid --version 1.0.30.0
+1 -1
View File
@@ -72,7 +72,7 @@ namespace WireMock.Http
var httpResponseMessage = await client.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseContentRead); var httpResponseMessage = await client.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseContentRead);
// Create ResponseMessage // Create ResponseMessage
return await HttpResponseMessageHelper.Create(httpResponseMessage, requiredUri, originalUri); return await HttpResponseMessageHelper.CreateAsync(httpResponseMessage, requiredUri, originalUri);
} }
} }
} }
@@ -2,8 +2,8 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Net.Http.Headers;
using JetBrains.Annotations; using JetBrains.Annotations;
using MimeKit;
using Newtonsoft.Json; using Newtonsoft.Json;
using WireMock.Util; using WireMock.Util;
using WireMock.Validation; using WireMock.Validation;
@@ -19,11 +19,11 @@ namespace WireMock.Http
var httpRequestMessage = new HttpRequestMessage(new HttpMethod(requestMessage.Method), url); var httpRequestMessage = new HttpRequestMessage(new HttpMethod(requestMessage.Method), url);
ContentType contentType = null; MediaTypeHeaderValue contentType = null;
if (requestMessage.Headers != null && requestMessage.Headers.ContainsKey(HttpKnownHeaderNames.ContentType)) if (requestMessage.Headers != null && requestMessage.Headers.ContainsKey(HttpKnownHeaderNames.ContentType))
{ {
var value = requestMessage.Headers[HttpKnownHeaderNames.ContentType].FirstOrDefault(); var value = requestMessage.Headers[HttpKnownHeaderNames.ContentType].FirstOrDefault();
ContentType.TryParse(value, out contentType); MediaTypeHeaderValue.TryParse(value, out contentType);
} }
switch (requestMessage.BodyData?.DetectedBodyType) switch (requestMessage.BodyData?.DetectedBodyType)
@@ -9,7 +9,7 @@ namespace WireMock.Http
{ {
internal static class HttpResponseMessageHelper internal static class HttpResponseMessageHelper
{ {
public static async Task<ResponseMessage> Create(HttpResponseMessage httpResponseMessage, Uri requiredUri, Uri originalUri) public static async Task<ResponseMessage> CreateAsync(HttpResponseMessage httpResponseMessage, Uri requiredUri, Uri originalUri)
{ {
var responseMessage = new ResponseMessage { StatusCode = (int)httpResponseMessage.StatusCode }; var responseMessage = new ResponseMessage { StatusCode = (int)httpResponseMessage.StatusCode };
+5 -18
View File
@@ -1,8 +1,6 @@
using System.Net.Http; using System.Net.Http;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Text;
using JetBrains.Annotations; using JetBrains.Annotations;
using MimeKit;
using WireMock.Validation; using WireMock.Validation;
namespace WireMock.Http namespace WireMock.Http
@@ -10,29 +8,18 @@ namespace WireMock.Http
internal static class StringContentHelper internal static class StringContentHelper
{ {
/// <summary> /// <summary>
/// Creates a StringContent object. Note that the Encoding is only set when it's also set on the original header. /// Creates a StringContent object.
/// </summary> /// </summary>
/// <param name="content">The string content (cannot be null)</param> /// <param name="content">The string content (cannot be null)</param>
/// <param name="contentType">The ContentType (can be null)</param> /// <param name="contentType">The ContentType (can be null)</param>
/// <returns>StringContent</returns> /// <returns>StringContent</returns>
internal static StringContent Create([NotNull] string content, [CanBeNull] ContentType contentType) internal static StringContent Create([NotNull] string content, [CanBeNull] MediaTypeHeaderValue contentType)
{ {
Check.NotNull(content, nameof(content)); Check.NotNull(content, nameof(content));
if (contentType == null) var stringContent = new StringContent(content);
{ stringContent.Headers.ContentType = contentType;
return new StringContent(content); return stringContent;
}
if (contentType.Charset == null)
{
var stringContent = new StringContent(content);
stringContent.Headers.ContentType = new MediaTypeHeaderValue(contentType.MimeType);
return stringContent;
}
var encoding = Encoding.GetEncoding(contentType.Charset);
return new StringContent(content, encoding, contentType.MimeType);
} }
} }
} }
+12 -16
View File
@@ -1,12 +1,11 @@
using JetBrains.Annotations; using System.Collections.Generic;
using MimeKit;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net.Http.Headers;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using JetBrains.Annotations;
using Newtonsoft.Json;
using WireMock.Matchers; using WireMock.Matchers;
using WireMock.Validation; using WireMock.Validation;
@@ -58,21 +57,18 @@ namespace WireMock.Util
new WildcardMatcher("application/x-www-form-urlencoded", true) new WildcardMatcher("application/x-www-form-urlencoded", true)
}; };
public static bool ParseBodyAsIsValid([CanBeNull] string parseBodyAs)
{
return Enum.TryParse(parseBodyAs, out BodyType _);
}
public static bool ShouldParseBody([CanBeNull] string method) public static bool ShouldParseBody([CanBeNull] string method)
{ {
if (String.IsNullOrEmpty(method)) if (string.IsNullOrEmpty(method))
{ {
return false; return false;
} }
if (BodyAllowedForMethods.TryGetValue(method.ToUpper(), out var allowed))
if (BodyAllowedForMethods.TryGetValue(method.ToUpper(), out bool allowed))
{ {
return allowed; return allowed;
} }
// If we don't have any knowledge of this method, we should assume that a body *may* // If we don't have any knowledge of this method, we should assume that a body *may*
// be present, so we should parse it if it is. Therefore, if a new method is added to // be present, so we should parse it if it is. Therefore, if a new method is added to
// the HTTP Method Registry, we only really need to add it to BodyAllowedForMethods if // the HTTP Method Registry, we only really need to add it to BodyAllowedForMethods if
@@ -82,22 +78,22 @@ namespace WireMock.Util
public static BodyType DetectBodyTypeFromContentType([CanBeNull] string contentTypeValue) public static BodyType DetectBodyTypeFromContentType([CanBeNull] string contentTypeValue)
{ {
if (string.IsNullOrEmpty(contentTypeValue) || !ContentType.TryParse(contentTypeValue, out ContentType contentType)) if (string.IsNullOrEmpty(contentTypeValue) || !MediaTypeHeaderValue.TryParse(contentTypeValue, out MediaTypeHeaderValue contentType))
{ {
return BodyType.Bytes; return BodyType.Bytes;
} }
if (TextContentTypeMatchers.Any(matcher => MatchScores.IsPerfect(matcher.IsMatch(contentType.MimeType)))) if (TextContentTypeMatchers.Any(matcher => MatchScores.IsPerfect(matcher.IsMatch(contentType.MediaType))))
{ {
return BodyType.String; return BodyType.String;
} }
if (JsonContentTypesMatchers.Any(matcher => MatchScores.IsPerfect(matcher.IsMatch(contentType.MimeType)))) if (JsonContentTypesMatchers.Any(matcher => MatchScores.IsPerfect(matcher.IsMatch(contentType.MediaType))))
{ {
return BodyType.Json; return BodyType.Json;
} }
if (MultipartContentTypesMatchers.Any(matcher => MatchScores.IsPerfect(matcher.IsMatch(contentType.MimeType)))) if (MultipartContentTypesMatchers.Any(matcher => MatchScores.IsPerfect(matcher.IsMatch(contentType.MediaType))))
{ {
return BodyType.MultiPart; return BodyType.MultiPart;
} }
-1
View File
@@ -57,7 +57,6 @@
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" /> <PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="SimMetrics.Net" Version="1.0.5" /> <PackageReference Include="SimMetrics.Net" Version="1.0.5" />
<PackageReference Include="RestEase" Version="1.4.7" /> <PackageReference Include="RestEase" Version="1.4.7" />
<PackageReference Include="MimeKitLite" Version="2.0.7" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="7.8.0.7320"> <PackageReference Include="SonarAnalyzer.CSharp" Version="7.8.0.7320">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
@@ -137,7 +137,7 @@ namespace WireMock.Net.Tests.Http
var message = HttpRequestMessageHelper.Create(request, "http://url"); var message = HttpRequestMessageHelper.Create(request, "http://url");
// Assert // Assert
Check.That(message.Content.Headers.GetValues("Content-Type")).ContainsExactly("application/xml; charset=utf-8"); Check.That(message.Content.Headers.GetValues("Content-Type")).ContainsExactly("application/xml; charset=UTF-8");
} }
[Fact] [Fact]
@@ -156,7 +156,7 @@ namespace WireMock.Net.Tests.Http
var message = HttpRequestMessageHelper.Create(request, "http://url"); var message = HttpRequestMessageHelper.Create(request, "http://url");
// Assert // Assert
Check.That(message.Content.Headers.GetValues("Content-Type")).ContainsExactly("application/xml; charset=us-ascii"); Check.That(message.Content.Headers.GetValues("Content-Type")).ContainsExactly("application/xml; charset=Ascii");
} }
} }
} }
@@ -0,0 +1,39 @@
using System.Net.Http.Headers;
using FluentAssertions;
using WireMock.Http;
using Xunit;
namespace WireMock.Net.Tests.Http
{
public class StringContentHelperTests
{
[Fact]
public void StringContentHelper_Create_WithNullContentType()
{
// Act
var result = StringContentHelper.Create("test", null);
// Assert
result.Headers.ContentType.Should().BeNull();
result.ReadAsStringAsync().Result.Should().Be("test");
}
[Theory]
[InlineData("application/json", "application/json")]
[InlineData("application/soap+xml", "application/soap+xml")]
[InlineData("application/soap+xml;charset=UTF-8", "application/soap+xml; charset=UTF-8")]
[InlineData("application/soap+xml;charset=UTF-8;action=\"http://myCompany.Customer.Contract/ICustomerService/GetSomeConfiguration\"", "application/soap+xml; charset=UTF-8; action=\"http://myCompany.Customer.Contract/ICustomerService/GetSomeConfiguration\"")]
public void StringContentHelper_Create(string test, string expected)
{
// Arrange
var contentType = MediaTypeHeaderValue.Parse(test);
// Act
var result = StringContentHelper.Create("test", contentType);
// Assert
result.Headers.ContentType.ToString().Should().Be(expected);
result.ReadAsStringAsync().Result.Should().Be("test");
}
}
}
@@ -39,7 +39,6 @@
<PackageReference Include="RestEase" Version="1.4.7" /> <PackageReference Include="RestEase" Version="1.4.7" />
<PackageReference Include="RandomDataGenerator.Net" Version="1.0.8" /> <PackageReference Include="RandomDataGenerator.Net" Version="1.0.8" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="MimeKitLite" Version="2.0.7" />
<PackageReference Include="Moq" Version="4.10.1" /> <PackageReference Include="Moq" Version="4.10.1" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" /> <PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="NFluent" Version="2.5.0" /> <PackageReference Include="NFluent" Version="2.5.0" />