diff --git a/Momento/JwtUtils.cs b/Momento/JwtUtils.cs index cb4403dc..c2b332ba 100644 --- a/Momento/JwtUtils.cs +++ b/Momento/JwtUtils.cs @@ -6,20 +6,20 @@ namespace MomentoSdk { - public class JwtUtils - { + class JwtUtils + { /// /// extracts the controlEndpoint and cacheEndpoint /// from the jwt /// /// /// - public static Claims decodeJwt(string jwt) + public static Claims DecodeJwt(string jwt) { IJsonSerializer serializer = new JsonNetSerializer(); IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder(); - JwtDecoder decoder = new JWT.JwtDecoder(serializer, urlEncoder); + JwtDecoder decoder = new JwtDecoder(serializer, urlEncoder); try { var decodedJwt = decoder.Decode(jwt); @@ -31,17 +31,17 @@ public static Claims decodeJwt(string jwt) } } - public class Claims + class Claims { [JsonProperty(PropertyName = "cp", Required = Required.Always)] - public string controlEndpoint { get; set; } + public string ControlEndpoint { get; private set; } [JsonProperty(PropertyName = "c", Required = Required.Always)] - public string cacheEndpoint { get; set; } + public string CacheEndpoint { get; private set; } public Claims(string cacheEndpoint, string controlEndpoint) { - this.cacheEndpoint = cacheEndpoint; - this.controlEndpoint = controlEndpoint; + this.CacheEndpoint = cacheEndpoint; + this.ControlEndpoint = controlEndpoint; } } } diff --git a/Momento/Momento.cs b/Momento/Momento.cs index 7a65f904..f2a731d0 100644 --- a/Momento/Momento.cs +++ b/Momento/Momento.cs @@ -1,7 +1,5 @@ using System; using ControlClient; -using CacheClient; -using static CacheClient.Scs; using static ControlClient.ScsControl; using Grpc.Core; using Grpc.Core.Interceptors; @@ -24,13 +22,13 @@ public class Momento : IDisposable /// Momento jwt public Momento(string authToken) { - Claims claims = JwtUtils.decodeJwt(authToken); - this.channel = GrpcChannel.ForAddress("https://" + claims.controlEndpoint + ":443", new GrpcChannelOptions() { Credentials = ChannelCredentials.SecureSsl }); + Claims claims = JwtUtils.DecodeJwt(authToken); + this.channel = GrpcChannel.ForAddress("https://" + claims.ControlEndpoint + ":443", new GrpcChannelOptions() { Credentials = ChannelCredentials.SecureSsl }); Header[] headers = { new Header(name: "Authorization", value: authToken) }; CallInvoker invoker = this.channel.Intercept(new HeaderInterceptor(headers)); this.client = new ScsControlClient(invoker); this.authToken = authToken; - this.cacheEndpoint = "https://" + claims.cacheEndpoint + ":443"; + this.cacheEndpoint = "https://" + claims.CacheEndpoint + ":443"; } /// @@ -62,9 +60,9 @@ public Responses.CreateCacheResponse CreateCache (String cacheName) try { CreateCacheRequest request = new CreateCacheRequest() { CacheName = cacheName }; - this.client.CreateCache(request); + client.CreateCache(request); } - catch (Grpc.Core.RpcException e) + catch (RpcException e) { if (e.StatusCode == StatusCode.AlreadyExists) { @@ -89,7 +87,7 @@ public Responses.CreateCacheResponse CreateCache (String cacheName) public MomentoCache GetCache(String cacheName, uint defaultTtlSeconds) { CheckValidCacheName(cacheName); - return MomentoCache.Init(this.authToken, cacheName, this.cacheEndpoint, defaultTtlSeconds); + return MomentoCache.Init(authToken, cacheName, cacheEndpoint, defaultTtlSeconds); } /// @@ -102,7 +100,7 @@ public Responses.DeleteCacheResponse DeleteCache(String cacheName) DeleteCacheRequest request = new DeleteCacheRequest() { CacheName = cacheName }; try { - this.client.DeleteCache(request); + client.DeleteCache(request); return new Responses.DeleteCacheResponse(); } catch(Exception e) { diff --git a/Momento/Responses/CacheGetResponse.cs b/Momento/Responses/CacheGetResponse.cs index 51329df2..53dc7cdd 100644 --- a/Momento/Responses/CacheGetResponse.cs +++ b/Momento/Responses/CacheGetResponse.cs @@ -6,13 +6,13 @@ namespace MomentoSdk.Responses { public class CacheGetResponse : BaseCacheResponse { - public MomentoCacheResult result { get; private set; } + public MomentoCacheResult Result { get; private set; } private readonly ByteString body; public CacheGetResponse(GetResponse response) { body = response.CacheBody; - result = this.ResultMapper(response.Result); + Result = this.ResultMapper(response.Result); } public String String() @@ -22,7 +22,7 @@ public String String() public String String(Encoding encoding) { - if (result == MomentoCacheResult.Hit) + if (Result == MomentoCacheResult.Hit) { return body.ToString(encoding); } @@ -31,7 +31,7 @@ public String String(Encoding encoding) public byte[] Bytes() { - if (result == MomentoCacheResult.Hit) + if (Result == MomentoCacheResult.Hit) { return this.body.ToByteArray(); } diff --git a/Momento/Responses/CacheSetResponse.cs b/Momento/Responses/CacheSetResponse.cs index b2865b0f..9559398e 100644 --- a/Momento/Responses/CacheSetResponse.cs +++ b/Momento/Responses/CacheSetResponse.cs @@ -4,11 +4,11 @@ namespace MomentoSdk.Responses { public class CacheSetResponse : BaseCacheResponse { - public MomentoCacheResult result { get; private set; } + public MomentoCacheResult Result { get; private set; } public CacheSetResponse(SetResponse response) { - result = this.ResultMapper(response.Result); + Result = this.ResultMapper(response.Result); } } } diff --git a/MomentoIntegrationTest/CacheTest.cs b/MomentoIntegrationTest/CacheTest.cs index ea0e5047..5a01de09 100644 --- a/MomentoIntegrationTest/CacheTest.cs +++ b/MomentoIntegrationTest/CacheTest.cs @@ -35,7 +35,7 @@ public async void HappyPathExpiredTtl() cache.Set(cacheKey, cacheValue, 1); await Task.Delay(1100); CacheGetResponse result = cache.Get(cacheKey); - Assert.Equal(MomentoCacheResult.Miss, result.result); + Assert.Equal(MomentoCacheResult.Miss, result.Result); } [Fact] @@ -48,7 +48,7 @@ public async void HappyPathAsync() MomentoCache cache = momento.CreateOrGetCache(cacheName, defaultTtlSeconds); await cache.SetAsync(cacheKey, cacheValue, defaultTtlSeconds); CacheGetResponse result = await cache.GetAsync(cacheKey); - Assert.Equal(MomentoCacheResult.Hit, result.result); + Assert.Equal(MomentoCacheResult.Hit, result.Result); Assert.Equal(cacheValue, result.String()); } @@ -59,7 +59,7 @@ public void HappyPathMiss() Momento momento = new Momento(authKey); MomentoCache cache = momento.CreateOrGetCache(cacheName, defaultTtlSeconds); CacheGetResponse result = cache.Get(Guid.NewGuid().ToString()); - Assert.Equal(MomentoCacheResult.Miss, result.result); + Assert.Equal(MomentoCacheResult.Miss, result.Result); Assert.Null(result.String()); Assert.Null(result.Bytes()); } diff --git a/MomentoIntegrationTest/MomentoTest.cs b/MomentoIntegrationTest/MomentoTest.cs index 10609677..f88ff29b 100644 --- a/MomentoIntegrationTest/MomentoTest.cs +++ b/MomentoIntegrationTest/MomentoTest.cs @@ -7,12 +7,19 @@ namespace MomentoIntegrationTest public class MomentoTest { private String authKey = Environment.GetEnvironmentVariable("TEST_AUTH_TOKEN"); - private String cacheName = Environment.GetEnvironmentVariable("TEST_CACHE_NAME"); + [Fact] public void DeleteCacheThatDoesntExist() { Momento momento = new Momento(authKey); Assert.Throws(() => momento.DeleteCache("non existant cache")); } + + + [Fact] + public void InvalidJwtException() + { + Assert.Throws( () => new Momento("eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJpbnRlZ3JhdGlvbiJ9.ZOgkTs")); + } } } diff --git a/MomentoTest/JwtUtilsTest.cs b/MomentoTest/JwtUtilsTest.cs deleted file mode 100644 index ffcf2936..00000000 --- a/MomentoTest/JwtUtilsTest.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System; -using Xunit; -using MomentoSdk; -using MomentoSdk.Exceptions; - -namespace MomentoTest -{ - public class JwtUtilsTest - { - [Fact] - public void TestValidJwt() - { - string jwt = "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJzcXVpcnJlbCIsImNwIjoiY29udHJvbCBwbGFuZSBlbmRwb2ludCIsImMiOiJkYXRhIHBsYW5lIGVuZHBvaW50In0.zsTsEXFawetTCZI"; - Claims c = JwtUtils.decodeJwt(jwt); - Assert.Equal("data plane endpoint", c.cacheEndpoint); - Assert.Equal("control plane endpoint", c.controlEndpoint); - } - - [Fact] - public void TestInvalidJwt() - { - string badJwt = "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJpbnRlZ3JhdGlvbiJ9.ZOgkTs"; - Assert.Throws(() => JwtUtils.decodeJwt(badJwt)); - } - } -} diff --git a/MomentoTest/Responses/CacheGetResponseTest.cs b/MomentoTest/Responses/CacheGetResponseTest.cs index a5154629..b7de79a4 100644 --- a/MomentoTest/Responses/CacheGetResponseTest.cs +++ b/MomentoTest/Responses/CacheGetResponseTest.cs @@ -15,16 +15,16 @@ public void CorrectResultMapping() ByteString body = ByteString.CopyFromUtf8(cacheBody); GetResponse serverResponseHit = new GetResponse() { CacheBody = body, Result = ECacheResult.Hit }; CacheGetResponse responseHit = new CacheGetResponse(serverResponseHit); - Assert.Equal(MomentoCacheResult.Hit, responseHit.result); + Assert.Equal(MomentoCacheResult.Hit, responseHit.Result); Assert.Equal(cacheBody, responseHit.String()); GetResponse serverResponseMiss = new GetResponse() { Result = ECacheResult.Miss }; CacheGetResponse responseMiss = new CacheGetResponse(serverResponseMiss); - Assert.Equal(MomentoCacheResult.Miss, responseMiss.result); + Assert.Equal(MomentoCacheResult.Miss, responseMiss.Result); GetResponse serverResponseBadRequest = new GetResponse() { Result = ECacheResult.BadRequest }; CacheGetResponse responseBadRequest = new CacheGetResponse(serverResponseBadRequest); - Assert.Equal(MomentoCacheResult.Unknown, responseBadRequest.result); + Assert.Equal(MomentoCacheResult.Unknown, responseBadRequest.Result); } } }