Skip to content

Commit

Permalink
porting more unit tests to .netcore project
Browse files Browse the repository at this point in the history
  • Loading branch information
DV committed Oct 24, 2019
1 parent f47487f commit b6d58e9
Show file tree
Hide file tree
Showing 5 changed files with 412 additions and 1 deletion.
73 changes: 73 additions & 0 deletions UnitTests/CollectionsTest.cs
@@ -0,0 +1,73 @@
using System.Collections.Generic;
using Jose;
using Xunit;

namespace UnitTests
{
public class CollectionsTest
{
[Fact]
public void UnionArrays()
{
var src = new[] {"one", "two"};
var other = new[] {"two", "three"};

Assert.Equal(Collections.Union(src, other), new [] {"one", "two", "three"});
}

[Fact]
public void UnionArrayList()
{
var src = new[] { "one", "two" };
var other = new List<string>(new [] { "two", "three" });

Assert.Equal(Collections.Union(src, other), new[] { "one", "two", "three" });

}

[Fact]
public void UnionArraySet()
{
var src = new[] { "one", "two" };
var other = new HashSet<string>(new [] { "two", "three" });

Assert.Equal(Collections.Union(src, other), new[] { "one", "two", "three" });

}

[Fact]
public void UnionNonStrings()
{
var src = new[] { "one", "two" };
var other = new[] { 2, 3 };

Assert.Equal(Collections.Union(src, other), new[] { "one", "two", "2", "3" });
}

[Fact]
public void UnionNull()
{
var other = new[] { "two", "three" };

Assert.Equal(Collections.Union(null, other), new[] { "two", "three" });
}

[Fact]
public void UnionWithNonEnumerable()
{
var src = new[] { "one", "two" };
var other = 3;

Assert.Equal(Collections.Union(src, other), new[] { "one", "two" });
}

[Fact]
public void UnionWithNull()
{
var src = new[] { "one", "two" };

Assert.Equal(Collections.Union(src, null), new[] { "one", "two" });
}

}
}
30 changes: 30 additions & 0 deletions UnitTests/TestSuite.cs
Expand Up @@ -8,6 +8,7 @@
using Security.Cryptography;
using Xunit;
using Jose.jwe;
using Newtonsoft.Json.Linq;

namespace UnitTests
{
Expand Down Expand Up @@ -2405,6 +2406,35 @@ public void EncodeWithUnencodedAndDetachedContent()
Assert.Equal(Jose.JWT.Decode(token, Encoding.UTF8.GetBytes(key), payload: @"{""hello"": ""world""}"), json);
}

[Fact]
public void EncodeWithUnencodedDetachedExtraHeaders()
{
//given
string json = @"{""hello"": ""world""}";

var headers = new Dictionary<string, object>
{
{ "exp", 1363284000 },
{ "crit", new [] {"exp"} },
};

//when
string token = Jose.JWT.Encode(json, Encoding.UTF8.GetBytes(key), JwsAlgorithm.HS256,
extraHeaders: headers,
options: new JwtOptions { DetachPayload = true, EncodePayload = false});

//then
Assert.Equal(token, "eyJhbGciOiJIUzI1NiIsImI2NCI6ZmFsc2UsImNyaXQiOlsiYjY0IiwiZXhwIl0sImV4cCI6MTM2MzI4NDAwMH0..9nZCB1H_OMmoTRBe2p5qeq38cyzcjJ6FzUZ9SkeZ4TU");
Assert.Equal(Jose.JWT.Decode(token, Encoding.UTF8.GetBytes(key), payload: @"{""hello"": ""world""}"), json);
var tokenHeaders = Jose.JWT.Headers(token);

Assert.Equal(tokenHeaders.Count(), 4);
Assert.Equal(tokenHeaders["alg"], "HS256");
Assert.Equal(tokenHeaders["b64"], false);
Assert.Equal(tokenHeaders["exp"], 1363284000L);
Assert.Equal(tokenHeaders["crit"], new JArray(new [] {"b64", "exp"}));
}

[Fact]
public void DecodeUnencodedDetached()
{
Expand Down

0 comments on commit b6d58e9

Please sign in to comment.