Skip to content

Commit

Permalink
Add basic test bed
Browse files Browse the repository at this point in the history
  • Loading branch information
henkmollema committed Feb 10, 2018
1 parent e8c7686 commit 04fac72
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Jwt.NET.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JsonWebTokens", "src\JsonWebTokens\JsonWebTokens.csproj", "{8AE68C4D-5A71-4B0F-9275-0541E5D388FD}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{5BE147ED-42A2-4506-9F54-6F3819A13AEA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JsonWebTokens.Tests", "test\JsonWebTokens.Tests\JsonWebTokens.Tests.csproj", "{FA5653F8-C2DA-4B4A-BB9A-DD689A759520}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -33,11 +37,24 @@ Global
{8AE68C4D-5A71-4B0F-9275-0541E5D388FD}.Release|x64.Build.0 = Release|x64
{8AE68C4D-5A71-4B0F-9275-0541E5D388FD}.Release|x86.ActiveCfg = Release|x86
{8AE68C4D-5A71-4B0F-9275-0541E5D388FD}.Release|x86.Build.0 = Release|x86
{FA5653F8-C2DA-4B4A-BB9A-DD689A759520}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FA5653F8-C2DA-4B4A-BB9A-DD689A759520}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FA5653F8-C2DA-4B4A-BB9A-DD689A759520}.Debug|x64.ActiveCfg = Debug|x64
{FA5653F8-C2DA-4B4A-BB9A-DD689A759520}.Debug|x64.Build.0 = Debug|x64
{FA5653F8-C2DA-4B4A-BB9A-DD689A759520}.Debug|x86.ActiveCfg = Debug|x86
{FA5653F8-C2DA-4B4A-BB9A-DD689A759520}.Debug|x86.Build.0 = Debug|x86
{FA5653F8-C2DA-4B4A-BB9A-DD689A759520}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FA5653F8-C2DA-4B4A-BB9A-DD689A759520}.Release|Any CPU.Build.0 = Release|Any CPU
{FA5653F8-C2DA-4B4A-BB9A-DD689A759520}.Release|x64.ActiveCfg = Release|x64
{FA5653F8-C2DA-4B4A-BB9A-DD689A759520}.Release|x64.Build.0 = Release|x64
{FA5653F8-C2DA-4B4A-BB9A-DD689A759520}.Release|x86.ActiveCfg = Release|x86
{FA5653F8-C2DA-4B4A-BB9A-DD689A759520}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{8AE68C4D-5A71-4B0F-9275-0541E5D388FD} = {27CE9B86-3C2B-4DC5-A4A4-9B7CE5B19FD1}
{FA5653F8-C2DA-4B4A-BB9A-DD689A759520} = {5BE147ED-42A2-4506-9F54-6F3819A13AEA}
EndGlobalSection
EndGlobal
106 changes: 106 additions & 0 deletions test/JsonWebTokens.Tests/JsonWebTokenTests.cs
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"]);
}
}
}
15 changes: 15 additions & 0 deletions test/JsonWebTokens.Tests/JsonWebTokens.Tests.csproj
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>

0 comments on commit 04fac72

Please sign in to comment.