Skip to content

Commit

Permalink
new content-type support
Browse files Browse the repository at this point in the history
  • Loading branch information
mihaj authored Oct 10, 2021
1 parent cca7e9f commit f24b86f
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project>
<PropertyGroup>
<Version>0.3.9</Version>
<Version>0.3.10</Version>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using QAToolKit.Core.Exceptions;
using QAToolKit.Core.Models;
using Xunit;

namespace QAToolKit.Core.Test.Models
{
public class ContentTypeTextJsonTests
{
[Fact]
public void ContentTypeTextJsonTestsPresent_Success()
{
Assert.Equal("text/json", ContentType.TextJson.Value());
}

[Theory]
[InlineData("text/json")]
public void ContentTypeTextJsonFromString_Success(string value)
{
Assert.Equal(ContentType.TextJson, ContentType.From(value));
}

[Theory]
[InlineData("")]
[InlineData(null)]
[InlineData("somestring")]
public void ConvertFromString_Fails(string value)
{
Assert.Throws<QAToolKitCoreException>(() => ContentType.From(value));
}

[Theory]
[InlineData(ContentType.Enumeration.TextJson)]
public void ConverTextJsonFromEnum_Success(ContentType.Enumeration value)
{
Assert.Equal(ContentType.TextJson, ContentType.From(value));
}

[Fact]
public void ConvertTextJsonObjectToString_Success()
{
Assert.Equal("text/json", ContentType.TextJson.Value());
}

[Fact]
public void ConvertTextJsonObjectToEnum_Success()
{
Assert.Equal(ContentType.Enumeration.TextJson, ContentType.ToEnum(ContentType.TextJson));
}

[Fact]
public void ConvertTextJsonStringToEnum_Success()
{
Assert.Equal(ContentType.Enumeration.TextJson, ContentType.ToEnum("text/json"));
}

[Theory]
[InlineData("")]
[InlineData(null)]
[InlineData("somestring")]
public void ConvertStringToEnum_Fails(string value)
{
Assert.Throws<QAToolKitCoreException>(() => ContentType.ToEnum(value));
}

[Fact]
public void ConvertTextJsonStringToContentType_Success()
{
Assert.Equal(ContentType.TextJson, ContentType.From("text/json"));
}
}
}
14 changes: 13 additions & 1 deletion src/QAToolKit.Core/Models/ContentType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ public enum Enumeration
/// <summary>
/// text/plain content type
/// </summary>
TextPlain
TextPlain,
/// <summary>
/// text/json content type
/// </summary>
TextJson
}

private readonly string _value;
Expand Down Expand Up @@ -64,6 +68,10 @@ public enum Enumeration
/// text/plain content type
/// </summary>
public static readonly ContentType TextPlain = new ContentType("text/plain");
/// <summary>
/// text/json content type
/// </summary>
public static readonly ContentType TextJson = new ContentType("text/json");

/// <summary>
/// Content type constructor
Expand Down Expand Up @@ -103,6 +111,7 @@ public static ContentType From(string value)
"application/octet-stream" => OctetStream,
"multipart/form-data" => MultipartFormData,
"text/plain" => TextPlain,
"text/json" => TextJson,
_ => throw new QAToolKitCoreException($"{value} is invalid content type. Check the documentation which types are supported."),
};
}
Expand All @@ -122,6 +131,7 @@ public static ContentType From(Enumeration value)
Enumeration.OctetStream => OctetStream,
Enumeration.MultipartFormData => MultipartFormData,
Enumeration.TextPlain => TextPlain,
Enumeration.TextJson => TextJson,
_ => throw new QAToolKitCoreException($"{value} is invalid content type. Check the documentation which types are supported."),
};
}
Expand All @@ -146,6 +156,7 @@ public static Enumeration ToEnum(ContentType value)
"application/octet-stream" => Enumeration.OctetStream,
"multipart/form-data" => Enumeration.MultipartFormData,
"text/plain" => Enumeration.TextPlain,
"text/json" => Enumeration.TextJson,
_ => throw new QAToolKitCoreException($"{value} is invalid content type. Check the documentation which types are supported."),
};
}
Expand All @@ -170,6 +181,7 @@ public static Enumeration ToEnum(string value)
"application/octet-stream" => Enumeration.OctetStream,
"multipart/form-data" => Enumeration.MultipartFormData,
"text/plain" => Enumeration.TextPlain,
"text/json" => Enumeration.TextJson,
_ => throw new QAToolKitCoreException($"{value} is invalid content type. Check the documentation which types are supported."),
};
}
Expand Down

0 comments on commit f24b86f

Please sign in to comment.