-
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.
- Loading branch information
1 parent
e8c7686
commit 04fac72
Showing
3 changed files
with
138 additions
and
0 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
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,106 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Xunit; | ||
|
||
namespace Jwt.Tests | ||
{ | ||
public class JsonWebTokenTests | ||
{ | ||
[Fact] | ||
public void EncodeHs256Token_Returns_ExpectedToken() | ||
{ | ||
// Arrange | ||
var payload = new Dictionary<string, object>() | ||
{ | ||
{ "key1", 1 }, | ||
{ "key2", "the-value" } | ||
}; | ||
var secretKey = "SOME_SECRET_KEY"; | ||
|
||
// Act | ||
var token = JsonWebToken.Encode(payload, secretKey, JwtHashAlgorithm.HS256); | ||
var decoded = JsonWebToken.Decode(token, secretKey); | ||
|
||
// Assert | ||
Assert.Equal("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJrZXkxIjoxLCJrZXkyIjoidGhlLXZhbHVlIn0.z4nWl_itwSsz1SbxEZkxCmm9MMkIKanFvgGz_gsWIJo", token); | ||
} | ||
|
||
[Fact] | ||
public void DecodeHS256_Verifies_Correctly() | ||
{ | ||
// Arrange | ||
var token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJrZXkxIjoxLCJrZXkyIjoidGhlLXZhbHVlIn0.z4nWl_itwSsz1SbxEZkxCmm9MMkIKanFvgGz_gsWIJo"; | ||
|
||
// Act | ||
var jwt = JsonWebToken.DecodeToObject(token, "SOME_SECRET_KEY"); | ||
|
||
// Assert | ||
Assert.Equal(1L, jwt["key1"]); | ||
Assert.Equal("the-value", jwt["key2"]); | ||
} | ||
|
||
[Fact] | ||
public void EncodeHs384Token_Returns_ExpectedToken() | ||
{ | ||
// Arrange | ||
var payload = new Dictionary<string, object>() | ||
{ | ||
{ "key1", 1 }, | ||
{ "key2", "the-value" } | ||
}; | ||
var secretKey = "SOME_SECRET_KEY"; | ||
|
||
// Act | ||
var token = JsonWebToken.Encode(payload, secretKey, JwtHashAlgorithm.HS384); | ||
|
||
// Assert | ||
Assert.Equal("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzM4NCJ9.eyJrZXkxIjoxLCJrZXkyIjoidGhlLXZhbHVlIn0.UKMB2eLfTwe_AupgNxAgX8hvGUYxivKjvonUCOhhY_EMpyMG8VVimu9E1GepOnvY", token); | ||
} | ||
|
||
[Fact] | ||
public void DecodeHS384_Verifies_Correctly() | ||
{ | ||
// Arrange | ||
var token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzM4NCJ9.eyJrZXkxIjoxLCJrZXkyIjoidGhlLXZhbHVlIn0.UKMB2eLfTwe_AupgNxAgX8hvGUYxivKjvonUCOhhY_EMpyMG8VVimu9E1GepOnvY"; | ||
|
||
// Act | ||
var jwt = JsonWebToken.DecodeToObject(token, "SOME_SECRET_KEY"); | ||
|
||
// Assert | ||
Assert.Equal(1L, jwt["key1"]); | ||
Assert.Equal("the-value", jwt["key2"]); | ||
} | ||
|
||
[Fact] | ||
public void EncodeHs512Token_Returns_ExpectedToken() | ||
{ | ||
// Arrange | ||
var payload = new Dictionary<string, object>() | ||
{ | ||
{ "key1", 1 }, | ||
{ "key2", "the-value" } | ||
}; | ||
var secretKey = "SOME_SECRET_KEY"; | ||
|
||
// Act | ||
var token = JsonWebToken.Encode(payload, secretKey, JwtHashAlgorithm.HS512); | ||
|
||
// Assert | ||
Assert.Equal("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJrZXkxIjoxLCJrZXkyIjoidGhlLXZhbHVlIn0.m6zcghjkZT6qZPFh5V6_oe-OKVmJtZ2orLYgFxhs1RxBMekftqVE0bE89LvU-q_eBBDfr7B3oA9SU_ZapQfPvQ", token); | ||
} | ||
|
||
[Fact] | ||
public void DecodeHS512_Verifies_Correctly() | ||
{ | ||
// Arrange | ||
var token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJrZXkxIjoxLCJrZXkyIjoidGhlLXZhbHVlIn0.m6zcghjkZT6qZPFh5V6_oe-OKVmJtZ2orLYgFxhs1RxBMekftqVE0bE89LvU-q_eBBDfr7B3oA9SU_ZapQfPvQ"; | ||
|
||
// Act | ||
var jwt = JsonWebToken.DecodeToObject(token, "SOME_SECRET_KEY"); | ||
|
||
// Assert | ||
Assert.Equal(1L, jwt["key1"]); | ||
Assert.Equal("the-value", jwt["key2"]); | ||
} | ||
} | ||
} |
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,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>netcoreapp2.0</TargetFramework> | ||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" /> | ||
<PackageReference Include="xunit" Version="2.3.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" /> | ||
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\JsonWebTokens\JsonWebTokens.csproj" /> | ||
</ItemGroup> | ||
</Project> |