-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add extension methods for checking "Content-Type" header value (#43)
- Loading branch information
Showing
5 changed files
with
253 additions
and
1 deletion.
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
177 changes: 177 additions & 0 deletions
177
src/Arbus.Network.UnitTests/Tests/CheckingContentType/CheckingContentTypeTests.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,177 @@ | ||
using Arbus.Network.Extensions; | ||
using FluentAssertions; | ||
|
||
namespace Arbus.Network.UnitTests.Tests.CheckingContentType; | ||
|
||
public class CheckingContentTypeTests | ||
{ | ||
[Test] | ||
public void IsApplicationJson_ContentTypeIsApplicationJson_ReturnsTrue() | ||
{ | ||
var content = new TestableHttpContent(HttpContentType.Application.Json); | ||
|
||
var result = content.IsApplicationJson(); | ||
|
||
result.Should().BeTrue(); | ||
} | ||
|
||
[Test] | ||
public void IsApplicationText_ContentTypeIsApplicationText_ReturnsTrue() | ||
{ | ||
var content = new TestableHttpContent(HttpContentType.Application.Text); | ||
|
||
bool result = content.IsApplicationText(); | ||
|
||
result.Should().BeTrue(); | ||
} | ||
|
||
[Test] | ||
public void IsApplicationProtobuf_ContentTypeIsApplicationProtobuf_ReturnsTrue() | ||
{ | ||
var content = new TestableHttpContent(HttpContentType.Application.Protobuf); | ||
|
||
bool result = content.IsApplicationProtobuf(); | ||
|
||
result.Should().BeTrue(); | ||
} | ||
|
||
[Test] | ||
public void IsApplicationProblemJson_ContentTypeIsApplicationProblemJson_ReturnsTrue() | ||
{ | ||
var content = new TestableHttpContent(HttpContentType.Application.ProblemJson); | ||
|
||
bool result = content.IsApplicationProblemJson(); | ||
|
||
result.Should().BeTrue(); | ||
} | ||
|
||
[Test] | ||
public void IsTextPlain_ContentTypeIsTextPlain_ReturnsTrue() | ||
{ | ||
var content = new TestableHttpContent(HttpContentType.Text.Plain); | ||
|
||
bool result = content.IsTextPlain(); | ||
|
||
result.Should().BeTrue(); | ||
} | ||
|
||
[Test] | ||
public void IsMultipartFormData_ContentTypeIsMultipartFormData_ReturnsTrue() | ||
{ | ||
var content = new TestableHttpContent(HttpContentType.Multipart.FormData); | ||
|
||
bool result = content.IsMultipartFormData(); | ||
|
||
result.Should().BeTrue(); | ||
} | ||
|
||
[Test] | ||
public void IsMultipartMixed_ContentTypeIsMultipartMixed_ReturnsTrue() | ||
{ | ||
var content = new TestableHttpContent(HttpContentType.Multipart.Mixed); | ||
|
||
bool result = content.IsMultipartMixed(); | ||
|
||
result.Should().BeTrue(); | ||
} | ||
|
||
[Test] | ||
public void IsMultipartAlternative_ContentTypeIsMultipartAlternative_ReturnsTrue() | ||
{ | ||
var content = new TestableHttpContent(HttpContentType.Multipart.Alternative); | ||
|
||
bool result = content.IsMultipartAlternative(); | ||
|
||
result.Should().BeTrue(); | ||
} | ||
|
||
[Test] | ||
public void IsMultipartRelated_ContentTypeIsMultipartRelated_ReturnsTrue() | ||
{ | ||
var content = new TestableHttpContent(HttpContentType.Multipart.Related); | ||
|
||
bool result = content.IsMultipartRelated(); | ||
|
||
result.Should().BeTrue(); | ||
} | ||
|
||
[Test] | ||
public void IsImageSvgXml_ContentTypeIsImageSvgXml_ReturnsTrue() | ||
{ | ||
var content = new TestableHttpContent(HttpContentType.Image.SvgXml); | ||
|
||
bool result = content.IsImageSvgXml(); | ||
|
||
result.Should().BeTrue(); | ||
} | ||
|
||
[Test] | ||
public void IsImageGif_ContentTypeIsImageGif_ReturnsTrue() | ||
{ | ||
var content = new TestableHttpContent(HttpContentType.Image.Gif); | ||
|
||
bool result = content.IsImageGif(); | ||
|
||
result.Should().BeTrue(); | ||
} | ||
|
||
[Test] | ||
public void IsImageJpeg_ContentTypeIsImageJpeg_ReturnsTrue() | ||
{ | ||
var content = new TestableHttpContent(HttpContentType.Image.Jpeg); | ||
|
||
bool result = content.IsImageJpeg(); | ||
|
||
result.Should().BeTrue(); | ||
} | ||
|
||
[Test] | ||
public void IsImagePngIsImagePngReturnsTrue() | ||
{ | ||
var content = new TestableHttpContent(HttpContentType.Image.Png); | ||
|
||
bool result = content.IsImagePng(); | ||
|
||
result.Should().BeTrue(); | ||
} | ||
|
||
[Test] | ||
public void IsImageTiff_ContentTypeIsImageTiff_ReturnsTrue() | ||
{ | ||
var content = new TestableHttpContent(HttpContentType.Image.Tiff); | ||
|
||
bool result = content.IsImageTiff(); | ||
|
||
result.Should().BeTrue(); | ||
} | ||
|
||
[Test] | ||
public void IsImageVndMicrosoftIcon_ContentTypeIsImageVndMicrosoftIcon_ReturnsTrue() | ||
{ | ||
var content = new TestableHttpContent(HttpContentType.Image.VndMicrosoftIcon); | ||
|
||
bool result = content.IsImageVndMicrosoftIcon(); | ||
|
||
result.Should().BeTrue(); | ||
} | ||
|
||
[Test] | ||
public void IsImageXIcon_ContentTypeIsImageXIcon_ReturnsTrue() | ||
{ | ||
var content = new TestableHttpContent(HttpContentType.Image.XIcon); | ||
|
||
bool result = content.IsImageXIcon(); | ||
|
||
result.Should().BeTrue(); | ||
} | ||
|
||
[Test] | ||
public void IsImageVndDjvu_ContentTypeIsImageVndDjvu_ReturnsTrue() | ||
{ | ||
var content = new TestableHttpContent(HttpContentType.Image.VndDjvu); | ||
|
||
bool result = content.IsImageVndDjvu(); | ||
|
||
result.Should().BeTrue(); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Arbus.Network.UnitTests/Tests/CheckingContentType/TestableHttpContent.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,16 @@ | ||
using System.Net; | ||
using System.Net.Http.Headers; | ||
|
||
namespace Arbus.Network.UnitTests.Tests.CheckingContentType; | ||
|
||
public class TestableHttpContent : HttpContent | ||
{ | ||
public TestableHttpContent(string contentType) | ||
{ | ||
Headers.ContentType = new MediaTypeHeaderValue(contentType); | ||
} | ||
|
||
protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context) => throw new NotImplementedException(); | ||
|
||
protected override bool TryComputeLength(out long length) => throw new NotImplementedException(); | ||
} |
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,58 @@ | ||
namespace Arbus.Network.Extensions; | ||
|
||
public static class HttpContentExtensions | ||
{ | ||
public static bool IsApplicationJson(this HttpContent content) => | ||
content.IsEqualsTo(HttpContentType.Application.Json); | ||
|
||
public static bool IsApplicationText(this HttpContent content) => | ||
content.IsEqualsTo(HttpContentType.Application.Text); | ||
|
||
public static bool IsApplicationProtobuf(this HttpContent content) => | ||
content.IsEqualsTo(HttpContentType.Application.Protobuf); | ||
|
||
public static bool IsApplicationProblemJson(this HttpContent content) => | ||
content.IsEqualsTo(HttpContentType.Application.ProblemJson); | ||
|
||
public static bool IsTextPlain(this HttpContent content) => | ||
content.IsEqualsTo(HttpContentType.Text.Plain); | ||
|
||
public static bool IsMultipartFormData(this HttpContent content) => | ||
content.IsEqualsTo(HttpContentType.Multipart.FormData); | ||
|
||
public static bool IsMultipartMixed(this HttpContent content) => | ||
content.IsEqualsTo(HttpContentType.Multipart.Mixed); | ||
|
||
public static bool IsMultipartAlternative(this HttpContent content) => | ||
content.IsEqualsTo(HttpContentType.Multipart.Alternative); | ||
|
||
public static bool IsMultipartRelated(this HttpContent content) => | ||
content.IsEqualsTo(HttpContentType.Multipart.Related); | ||
|
||
public static bool IsImageSvgXml(this HttpContent content) => | ||
content.IsEqualsTo(HttpContentType.Image.SvgXml); | ||
|
||
public static bool IsImageGif(this HttpContent content) => | ||
content.IsEqualsTo(HttpContentType.Image.Gif); | ||
|
||
public static bool IsImageJpeg(this HttpContent content) => | ||
content.IsEqualsTo(HttpContentType.Image.Jpeg); | ||
|
||
public static bool IsImagePng(this HttpContent content) => | ||
content.IsEqualsTo(HttpContentType.Image.Png); | ||
|
||
public static bool IsImageTiff(this HttpContent content) => | ||
content.IsEqualsTo(HttpContentType.Image.Tiff); | ||
|
||
public static bool IsImageVndMicrosoftIcon(this HttpContent content) => | ||
content.IsEqualsTo(HttpContentType.Image.VndMicrosoftIcon); | ||
|
||
public static bool IsImageXIcon(this HttpContent content) => | ||
content.IsEqualsTo(HttpContentType.Image.XIcon); | ||
|
||
public static bool IsImageVndDjvu(this HttpContent content) => | ||
content.IsEqualsTo(HttpContentType.Image.VndDjvu); | ||
|
||
private static bool IsEqualsTo(this HttpContent content, string contentType) => | ||
content.Headers.ContentType.MediaType == contentType; | ||
} |
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