GivenIdempotencyRegisterWhenInstantiateWithStatusCodeAndStreamAndStatusCodeIsOutRangeShouldThrowsArgumentException( HttpStatusCode statusCode) { Func <IIdempotencyRegister> action = () => HttpIdempotencyRegister.Of("key", statusCode, new MemoryStream()); action.Should().Throw <ArgumentException>(); }
GivenRequestWhenIdempotencyKeyAvaiableAndFailsSaveKeyAndDoesntHaveResponseShouldReturnStatusCode409() { var idempotencyKey = Guid.NewGuid().ToString(); var idempotencyRegister = HttpIdempotencyRegister.Of(idempotencyKey); var contextMock = new Mock <HttpContext>(); _keyReaderMock.Setup(x => x.Read(It.IsAny <HttpRequest>())) .Returns(idempotencyKey) .Verifiable(); contextMock.SetupGet(x => x.Request.Method) .Returns(HttpMethods.Post) .Verifiable(); contextMock.SetupSet(x => x.Response.StatusCode = (int)HttpStatusCode.Conflict) .Verifiable(); contextMock.SetupSet(x => x.Response.ContentType = MediaTypeNames.Application.Json) .Verifiable(); _repositoryMock.Setup(x => x.TryAddAsync(idempotencyKey)) .ReturnsAsync(false) .Verifiable(); _repositoryMock.Setup(x => x.GetAsync <HttpIdempotencyRegister>(idempotencyKey)) .ReturnsAsync(idempotencyRegister) .Verifiable(); await _middleware.InvokeAsync(contextMock.Object); contextMock.VerifyAll(); _keyReaderMock.VerifyAll(); _repositoryMock.VerifyAll(); _nextMock.VerifyAll(); }
GivenIdempotencyRegisterWhenInstantiateWithStatusCodeAndStreamAndKeyIsEmptyShouldThrowsArgumentNullException( string key) { Func <IIdempotencyRegister> action = () => HttpIdempotencyRegister.Of(key, HttpStatusCode.OK, new MemoryStream()); action.Should().Throw <ArgumentNullException>(); }
public void GivenHttpIdempotencySerializerShouldSerializeIdempotencyRegister() { var register = HttpIdempotencyRegister.Of("key", HttpStatusCode.OK, new MemoryStream(Encoding.UTF8.GetBytes("body"))); var serializedObject = _serializer.Serialize(register); serializedObject.Should().NotBeNullOrWhiteSpace(); }
public void GivenIdempotencyRegisterWhenInstantiateShouldRewindResponseStream() { var stream = new MemoryStream(Encoding.UTF8.GetBytes("Message")); var register = HttpIdempotencyRegister.Of("key", HttpStatusCode.OK, stream); register.Value.Should().Be("Message"); new StreamReader(stream).ReadToEnd().Should().Be("Message"); }
public void GivenIdempotencyRegisterWhenInstantiateShouldReturnsInstanceWithKeySet(string key) { var register = HttpIdempotencyRegister.Of(key); register.Should().NotBeNull(); register.Key.Should().Be(key); register.Value.Should().BeNull(); register.IsCompleted.Should().BeFalse(); register.StatusCode.Should().Be(0); }
public void GivenHttpIdempotencySerializerShouldDeserializeIdempotencyRegister() { var expectedRegister = HttpIdempotencyRegister.Of("key", HttpStatusCode.OK, new MemoryStream(Encoding.UTF8.GetBytes("body"))); var serializedObject = _serializer.Serialize(expectedRegister); var register = _serializer.Deserialize <HttpIdempotencyRegister>(serializedObject); register.Key.Should().Be(expectedRegister.Key); register.IsCompleted.Should().Be(expectedRegister.IsCompleted); register.StatusCode.Should().Be(HttpStatusCode.OK); register.Value.Should().Be(expectedRegister.Value); }
GivenIdempotencyRegisterWhenInstantiateWithStatusCodeAndStreamShouldReturnsInstanceWithKeyStatusCodeAndBodySet( string key, HttpStatusCode statusCode, string body) { var stream = new MemoryStream(Encoding.UTF8.GetBytes(body)); var register = HttpIdempotencyRegister.Of(key, statusCode, stream); register.Should().NotBeNull(); register.Key.Should().Be(key); register.IsCompleted.Should().BeTrue(); register.Value.Should().Be(body); register.StatusCode.Should().Be(statusCode); }
public async Task GivenRequestWhenHasResponseShouldLogKeyDetectedAndResponseFromCache() { var idempotencyKey = Guid.NewGuid().ToString(); var idempotencyRegister = HttpIdempotencyRegister.Of( idempotencyKey, HttpStatusCode.OK, new MemoryStream(Encoding.UTF8.GetBytes("Message cached."))); var contextMock = new Mock <HttpContext>(); var responseMock = new Mock <HttpResponse>(); var responseBody = new MemoryStream(); _keyReaderMock.Setup(x => x.Read(It.IsAny <HttpRequest>())) .Returns(idempotencyKey) .Verifiable(); contextMock.SetupGet(x => x.Request.Method) .Returns(HttpMethods.Post) .Verifiable(); contextMock.SetupGet(x => x.Response) .Returns(responseMock.Object) .Verifiable(); responseMock.SetupGet(x => x.Body) .Returns(responseBody) .Verifiable(); responseMock.SetupSet(x => x.StatusCode = (int)HttpStatusCode.OK) .Verifiable(); responseMock.SetupSet(x => x.ContentType = MediaTypeNames.Application.Json) .Verifiable(); _repositoryMock.Setup(x => x.TryAddAsync(idempotencyKey)) .ReturnsAsync(false) .Verifiable(); _repositoryMock.Setup(x => x.GetAsync <HttpIdempotencyRegister>(idempotencyKey)) .ReturnsAsync(idempotencyRegister) .Verifiable(); _loggerMock.Setup(x => x.WriteInformation(idempotencyKey, "Idempotency: Key detected.")) .Verifiable(); _loggerMock.Setup(x => x.WriteResponse(idempotencyKey, "Idempotency: Response from cache.", responseMock.Object)) .Verifiable(); await _middleware.InvokeAsync(contextMock.Object); contextMock.VerifyAll(); responseMock.VerifyAll(); _keyReaderMock.VerifyAll(); _repositoryMock.VerifyAll(); _nextMock.VerifyAll(); _loggerMock.VerifyAll(); }
public async Task GivenIdempotencyKeyWhenGetShouldReturnRegister() { var register = HttpIdempotencyRegister.Of( Guid.NewGuid().ToString(), HttpStatusCode.OK, new MemoryStream(Encoding.UTF8.GetBytes("Message body"))); _serializerMock.Setup(x => x.Deserialize <HttpIdempotencyRegister>(It.IsAny <string>())) .Returns(register) .Verifiable(); _redisDatabaseMock.Setup(x => x.StringGetAsync(register.Key, CommandFlags.None)) .ReturnsAsync(It.IsAny <string>()) .Verifiable(); await _repository.GetAsync <HttpIdempotencyRegister>(register.Key); _serializerMock.VerifyAll(); _redisDatabaseMock.VerifyAll(); }
public void GivenIdempotencyKeyWhenUpdateShouldChangeValueAndSetExpirationTime() { var idempotencyKey = HttpIdempotencyRegister.Of( Guid.NewGuid().ToString(), HttpStatusCode.OK, new MemoryStream()); _serializerMock.Setup(x => x.Serialize(It.IsAny <IIdempotencyRegister>())) .Returns(string.Empty) .Verifiable(); _redisDatabaseMock.Setup(x => x.StringSetAsync(idempotencyKey.Key, string.Empty, TimeSpan.FromDays(1), When.Exists, CommandFlags.None)) .Verifiable(); _repository.Awaiting(async x => await x.UpdateAsync(idempotencyKey.Key, idempotencyKey)) .Should().NotThrow(); _serializerMock.VerifyAll(); _redisDatabaseMock.VerifyAll(); }
public async Task GivenRequestWhenIdempotencyKeyAvaiableAndFailsSaveKeyAndHasResponseShouldReturnResponse() { var idempotencyKey = Guid.NewGuid().ToString(); var idempotencyRegister = HttpIdempotencyRegister.Of( idempotencyKey, HttpStatusCode.OK, new MemoryStream(Encoding.UTF8.GetBytes("Successful saved message"))); var responseBody = new MemoryStream(); var contextMock = new Mock <HttpContext>(); _keyReaderMock.Setup(x => x.Read(It.IsAny <HttpRequest>())) .Returns(idempotencyKey) .Verifiable(); contextMock.SetupGet(x => x.Request.Method) .Returns(HttpMethods.Post) .Verifiable(); contextMock.SetupSet(x => x.Response.StatusCode = (int)HttpStatusCode.OK) .Verifiable(); contextMock.SetupSet(x => x.Response.ContentType = MediaTypeNames.Application.Json) .Verifiable(); contextMock.SetupGet(x => x.Response.Body) .Returns(responseBody) .Verifiable(); _repositoryMock.Setup(x => x.TryAddAsync(idempotencyKey)) .ReturnsAsync(false) .Verifiable(); _repositoryMock.Setup(x => x.GetAsync <HttpIdempotencyRegister>(idempotencyKey)) .ReturnsAsync(idempotencyRegister) .Verifiable(); await _middleware.InvokeAsync(contextMock.Object); contextMock.VerifyAll(); _keyReaderMock.VerifyAll(); _repositoryMock.VerifyAll(); _nextMock.VerifyAll(); responseBody.Length.Should().NotBe(0); }
public void GivenIdempotencyRegisterWhenInstantiateAndKeyIsEmptyShouldThrowsArgumentNullException(string key) { Func <IIdempotencyRegister> action = () => HttpIdempotencyRegister.Of(key); action.Should().Throw <ArgumentNullException>(); }
GivenIdempotencyRegisterWhenInstantiateWithStatusCodeAndStreamAndStreamIsNullShouldThrowsArgumentNullException() { Func <IIdempotencyRegister> action = () => HttpIdempotencyRegister.Of("key", HttpStatusCode.OK, null); action.Should().Throw <ArgumentNullException>(); }