Exemplo n.º 1
0
        public void Encode_With_PayloadWithClaims_Should_Return_Token()
        {
            var algorithm = new HMACSHA256Algorithm();
            var claims    = new Dictionary <string, object>();
            var builder   = new JwtBuilder();
            var secret    = _fixture.Create <string>();

            var nbClaims    = _fixture.Create <int>();
            var claimKeys   = new string[nbClaims];
            var claimValues = new string[nbClaims];

            for (var i = 0; i < nbClaims; ++i)
            {
                claimKeys[i]   = _fixture.Create <string>();
                claimValues[i] = _fixture.Create <string>();

                claims.Add(claimKeys[i], claimValues[i]);
            }

            var token = builder.WithAlgorithm(algorithm)
                        .WithSecret(secret)
                        .AddClaims(claims)
                        .Encode();

            var decodedToken = new UTF8Encoding(false).GetString(
                new JwtBase64UrlEncoder().Decode(token.Split('.')[1]));

            token.Should()
            .NotBeNullOrEmpty("because the token should contains some data");

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

            decodedToken.Should()
            .ContainAll(claimKeys, "because all used keys should be retrieved in the token");

            decodedToken.Should()
            .ContainAll(claimValues, "because all values associated with the claims should be retrieved in the token");
        }