Skip to content

Commit

Permalink
Removed tests related to camel casing and fixed call to factory
Browse files Browse the repository at this point in the history
  • Loading branch information
hartmark committed Jun 10, 2022
1 parent b0d789e commit 46eae50
Show file tree
Hide file tree
Showing 6 changed files with 2 additions and 123 deletions.
14 changes: 1 addition & 13 deletions src/JWT/Builder/JwtBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System;
using System.Linq;
using JWT.Algorithms;
using JWT.Serializers;

using static JWT.Internal.EncodingHelper;
using static JWT.Serializers.JsonSerializerFactory;

namespace JWT.Builder
{
Expand Down Expand Up @@ -293,17 +292,6 @@ public T Decode<T>(string token)
return _decoder.DecodeToObject<T>(token, _secrets, _valParams.ValidateSignature);
}

private static IJsonSerializer CreateSerializer()
{
#if SYSTEM_TEXT_JSON
return new SystemTextSerializer();
#elif NEWTONSOFT_JSON
return new JsonNetSerializer();
#else
throw new NotSupportedException();
#endif
}

private void TryCreateEncoder()
{
if (_algorithm is null && _algFactory is null)
Expand Down
13 changes: 0 additions & 13 deletions tests/JWT.Tests.Common/Builder/JwtBuilderDecodeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -399,18 +399,5 @@ public void Decode_ToDictionary_Without_Serializer_Should_Throw_Exception()
action.Should()
.Throw<InvalidOperationException>("because token can't be decoded without valid serializer");
}

[TestMethod]
public void Decode_Should_Be_Able_To_Deserialize_Payload_As_CamelCase()
{
var token = JwtBuilder.Create()
.WithAlgorithm(TestData.HMACSHA256Algorithm)
.WithSecret(TestData.Secrets)
.MustVerifySignature()
.Decode<Customer>(TestData.TokenPayloadAsCamelCase);

token.FirstName.Should().Be("Jesus");
token.Age.Should().Be(33);
}
}
}
38 changes: 0 additions & 38 deletions tests/JWT.Tests.Common/Builder/JwtBuilderEncodeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,43 +228,5 @@ private sealed class CustomFactory : IAlgorithmFactory
public IJwtAlgorithm Create(JwtDecoderContext context) =>
new HMACSHA256Algorithm();
}

[TestMethod]
public void Encode_Should_Be_Able_To_Serialize_Payload_As_CamelCase()
{
var algorithm = new HMACSHA256Algorithm();
var secret = _fixture.Create<string>();
const string expected = TestData.TokenPayloadAsCamelCase;

var token = JwtBuilder.Create()
.WithAlgorithm(algorithm)
.WithSecret(secret)
.Encode();

token.Should()
.NotBeNullOrEmpty("because the token should contains some data");
token.Split('.')
.Should()
.HaveCount(3, "because the token should consist of three parts");

token.Should()
.Be(expected, "because the same data encoded with the same key must result in the same token");
}

private static string Base64Decode(string base64EncodedData)
{
switch (base64EncodedData.Length % 4)
{
case 2:
base64EncodedData += "==";
break;
case 3:
base64EncodedData += "=";
break;
}

var base64EncodedBytes = Convert.FromBase64String(base64EncodedData);
return Encoding.UTF8.GetString(base64EncodedBytes);
}
}
}
20 changes: 0 additions & 20 deletions tests/JWT.Tests.Common/JwtDecoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -538,25 +538,5 @@ public void DecodeToObject_Should_Throw_Exception_On_Null_NotBefore_Claim()
.Throw<SignatureVerificationException>()
.WithMessage("Claim 'nbf' must be a number.", "because the invalid 'nbf' must result in an exception on decoding");
}

[TestMethod]
public void Decode_Should_Be_Able_To_Deserialize_Payload_As_CamelCase()
{
const string key = TestData.Secret;
const string token = TestData.TokenPayloadAsCamelCase;
var payload = TestData.Customer;

var serializer = CreateSerializer();
var dateTimeProvider = new UtcDateTimeProvider();
var validator = new JwtValidator(serializer, dateTimeProvider);
var urlEncoder = new JwtBase64UrlEncoder();
var decoder = new JwtDecoder(serializer, validator, urlEncoder, TestData.HMACSHA256Algorithm);

var actual = decoder.Decode(token, new[] { key }, verify: true);
var expected = serializer.Serialize(payload);

actual.Should()
.Be(expected, "because the provided object should be correctly serialized in the token");
}
}
}
36 changes: 0 additions & 36 deletions tests/JWT.Tests.Common/JwtEncoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,41 +91,5 @@ public void Encode_With_NoAlgorithm_Should_Encode_To_Token()
actual.Should()
.Be(expected, "because the same data encoded with the same key must result in the same token");
}

[TestMethod]
public void Encode_Should_Be_Able_To_Serialize_Payload_As_CamelCase()
{
const string key = TestData.Secret;
var toEncode = TestData.Customer;
const string expected = TestData.TokenPayloadAsCamelCase;

var algorithm = new HMACSHA256Algorithm();
var urlEncoder = new JwtBase64UrlEncoder();
var serializer = CreateSerializer();
var encoder = new JwtEncoder(algorithm, serializer, urlEncoder);

var actual = encoder.Encode(toEncode, key);

actual.Should()
.Be(expected, "because the same data encoded with the same key must result in the same token");
}

[TestMethod]
public void Encode_Should_Be_Able_To_Serialize_Payload_As_PascalCase()
{
const string key = TestData.Secret;
var toEncode = TestData.Customer;
const string expected = TestData.Token;

var algorithm = new HMACSHA256Algorithm();
var urlEncoder = new JwtBase64UrlEncoder();
var serializer = CreateSerializer();
var encoder = new JwtEncoder(algorithm, serializer, urlEncoder);

var actual = encoder.Encode(toEncode, key);

actual.Should()
.Be(expected, "because the same data encoded with the same key must result in the same token");
}
}
}
4 changes: 1 addition & 3 deletions tests/JWT.Tests.Net40/JWT.Tests.Net40.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<!-- Unless I change this to something below net462 i get no exception about missing implementation in JsonNetSerializer.cs -->
<!-- Had to set to net461, because I get that some nugget don't support net40 -->
<TargetFramework>net461</TargetFramework>
<TargetFramework>net462</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 46eae50

Please sign in to comment.