-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from VahidFarahmandian/60-send-multipartform-d…
…ata-using-httpclientfactory 60 send multipartform data using httpclientfactory
- Loading branch information
Showing
11 changed files
with
1,076 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net.Mime; | ||
|
||
namespace Jinget.Core.Utilities.Http | ||
{ | ||
public class HeaderUtility | ||
{ | ||
/// <summary> | ||
/// check if content type header presents in the given collection or not. | ||
/// This method does serch for the key using <seealso cref="StringComparison.OrdinalIgnoreCase"/> | ||
/// </summary> | ||
public static bool HasContentType(Dictionary<string, string> headers) | ||
=> | ||
headers != null && | ||
headers.Keys.Any(x => string.Equals(x, "Content-Type", StringComparison.OrdinalIgnoreCase)); | ||
|
||
/// <summary> | ||
/// searches for content-type header and if presents, return its value. | ||
/// This method searches for the content-type header using <seealso cref="HasContentType(Dictionary{string, string})"/> | ||
/// </summary> | ||
public static string GetContentTypeValue(Dictionary<string, string> headers) | ||
{ | ||
if (headers != null && HasContentType(headers)) | ||
{ | ||
return headers.FirstOrDefault(x => string.Equals(x.Key, "Content-Type", StringComparison.OrdinalIgnoreCase)).Value; | ||
} | ||
return null; | ||
} | ||
|
||
/// <summary> | ||
/// searches for content-type header and if presents, return its key. | ||
/// This method searches for the content-type header using <seealso cref="HasContentType(Dictionary{string, string})"/> | ||
/// </summary> | ||
public static string GetContentTypeHeaderName(Dictionary<string, string> headers) | ||
{ | ||
if (headers != null && HasContentType(headers)) | ||
return headers.FirstOrDefault(x => string.Equals(x.Key, "Content-Type", StringComparison.OrdinalIgnoreCase)).Key; | ||
return null; | ||
} | ||
|
||
/// <summary> | ||
/// check if <seealso cref="MediaTypeNames.Application.Xml"/> | ||
/// or <seealso cref="MediaTypeNames.Text.Xml"/> exists in the given header collection | ||
/// </summary> | ||
public static bool IsXmlContentType(Dictionary<string, string> headers) | ||
=> | ||
HasContentType(headers) && | ||
(string.Equals(GetContentTypeValue(headers), MediaTypeNames.Text.Xml, StringComparison.OrdinalIgnoreCase) || | ||
GetContentTypeValue(headers).StartsWith(MediaTypeNames.Application.Xml, StringComparison.OrdinalIgnoreCase)); | ||
|
||
/// <summary> | ||
/// check if <seealso cref="MediaTypeNames.Application.Json"/> exists in the given header collection | ||
/// </summary> | ||
public static bool IsJsonContentType(Dictionary<string, string> headers) | ||
=> | ||
HasContentType(headers) && | ||
GetContentTypeValue(headers).StartsWith(MediaTypeNames.Application.Json, StringComparison.OrdinalIgnoreCase); | ||
|
||
/// <summary> | ||
/// check if <seealso cref="MediaTypeNames.Application.mu"/> exists in the given header collection | ||
/// </summary> | ||
public static bool IsMultiPartFormDataContentType(Dictionary<string, string> headers) | ||
=> | ||
HasContentType(headers) && | ||
GetContentTypeValue(headers).StartsWith(MediaTypeNames.Application.Json, StringComparison.OrdinalIgnoreCase); | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
Tests/Jinget.Core.Tests/Utilities/Http/HeaderUtilityTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System.Collections.Generic; | ||
|
||
namespace Jinget.Core.Utilities.Http.Tests | ||
{ | ||
[TestClass()] | ||
public class HeaderUtilityTests | ||
{ | ||
[TestMethod()] | ||
public void should_return_true_when_contenttype_exists() | ||
{ | ||
Dictionary<string, string> headers = new() | ||
{ | ||
{ "content-type","some thing..." } | ||
}; | ||
var result = HeaderUtility.HasContentType(headers); | ||
Assert.IsTrue(result); | ||
} | ||
|
||
[TestMethod()] | ||
public void should_return_true_for_text_xml_contenttype_header() | ||
{ | ||
Dictionary<string, string> headers = new() | ||
{ | ||
{ "content-type","text/xml" } | ||
}; | ||
var result = HeaderUtility.IsXmlContentType(headers); | ||
Assert.IsTrue(result); | ||
} | ||
|
||
[TestMethod()] | ||
public void should_return_true_for_application_xml_contenttype_header() | ||
{ | ||
Dictionary<string, string> headers = new() | ||
{ | ||
{ "content-type","application/xml" } | ||
}; | ||
var result = HeaderUtility.IsXmlContentType(headers); | ||
Assert.IsTrue(result); | ||
} | ||
|
||
[TestMethod()] | ||
public void should_return_true_for_application_json_contenttype_header() | ||
{ | ||
Dictionary<string, string> headers = new() | ||
{ | ||
{ "content-type","application/json" } | ||
}; | ||
var result = HeaderUtility.IsJsonContentType(headers); | ||
Assert.IsTrue(result); | ||
} | ||
|
||
[TestMethod()] | ||
public void should_return_false_for_non_xml_json_contenttype_header() | ||
{ | ||
Dictionary<string, string> headers = new() | ||
{ | ||
{ "content-type","some thing..." } | ||
}; | ||
var result = HeaderUtility.IsXmlContentType(headers); | ||
Assert.IsFalse(result); | ||
} | ||
|
||
[TestMethod()] | ||
public void should_return_contenttype_header_name() | ||
{ | ||
string expectedResult = "content-type"; | ||
Dictionary<string, string> headers = new() | ||
{ | ||
{ "content-type","some thing..." } | ||
}; | ||
var result = HeaderUtility.GetContentTypeHeaderName(headers); | ||
Assert.AreEqual(result, expectedResult); | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
Tests/Jinget.Core.Tests/Utilities/Http/MimeTypeMapTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace Jinget.Core.Utilities.Http.Tests | ||
{ | ||
[TestClass()] | ||
public class MimeTypeMapTests | ||
{ | ||
[TestMethod()] | ||
public void should_try_return_valid_mimetype_for_givent_filename() | ||
{ | ||
bool result = MimeTypeMap.TryGetMimeType("sample.txt", out string mimeType); | ||
Assert.IsTrue(result); | ||
Assert.AreEqual("text/plain", mimeType); | ||
} | ||
|
||
[TestMethod()] | ||
public void should_return_valid_mimetype_for_givent_filename() | ||
{ | ||
string result = MimeTypeMap.GetMimeType("sample.txt"); | ||
Assert.AreEqual("text/plain", result); | ||
} | ||
|
||
[TestMethod()] | ||
public void should_return_file_extension_for_given_mimetype() | ||
{ | ||
string result = MimeTypeMap.GetExtension("text/plain"); | ||
Assert.AreEqual(".txt", result); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
Tests/Jinget.Handlers.ExternalServiceHandlers.Tests/Sample Upload File1.txt
Large diffs are not rendered by default.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
Tests/Jinget.Handlers.ExternalServiceHandlers.Tests/Sample Upload File2.txt
Large diffs are not rendered by default.
Oops, something went wrong.