Skip to content

Commit

Permalink
Add extension methods for checking "Content-Type" header value (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
Leo506 authored Jul 5, 2023
1 parent 3084bfe commit 2e09fcc
Show file tree
Hide file tree
Showing 5 changed files with 253 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Arbus.Network.UnitTests/Arbus.Network.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.11.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="NUnit" Version="3.13.3" />
Expand Down
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();
}
}
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();
}
58 changes: 58 additions & 0 deletions src/Arbus.Network/Extensions/HttpContentExtensions.cs
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;
}
2 changes: 1 addition & 1 deletion src/Arbus.Network/HttpContentType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ public static class Multipart
public const string FormData = "multipart/form-data";
public const string Mixed = "multipart/mixed";
public const string Alternative = "multipart/alternative";
public const string Related = "multipart/related ";
public const string Related = "multipart/related";
}
}

0 comments on commit 2e09fcc

Please sign in to comment.