public async Task It_should_execute_next_middleware_if_no_command_passed() { var mapping = ( command : typeof(SuccessfulTestCommand), commandHandler : typeof(SuccessfulTestCommandHandler)); var registryMock = new Mock <ICommandRegistry>(); registryMock.Setup(registry => registry.TryGetValue("successful-test-command", out mapping)) .Returns(true); var nextFlag = false; var middleware = new CommandingMiddleware( async context => nextFlag = true, registryMock.Object, Mock.Of <IMemoryCache>(), Mock.Of <ILoggerFactory>()); var httpContext = new DefaultHttpContext(); httpContext.Request.ContentType = MediaTypeNames.Application.Json; httpContext.Request.Path = "/not-command"; httpContext.Request.Method = "POST"; await middleware.InvokeAsync(httpContext); nextFlag.Should().BeTrue(); }
public async Task It_should_set_memory_cache_if_not_set() { var commandRegistryMock = new Mock <ICommandRegistry>(); commandRegistryMock.Setup(commandRegistry => commandRegistry.GetEnumerator()) .Returns(new List <(string commandName, Type command, Type commandHandler)> { ( commandName: "successfulTestCommand", command: typeof(SuccessfulTestCommand), commandHandler: typeof(SuccessfulTestCommandHandler)) }.GetEnumerator()); // Memory Cache setup var memoryCacheMock = new Mock <IMemoryCache>(); object outValue = new Dictionary <string, JsonElement>(); bool isCreateMemoCacheEntryCalled = false; memoryCacheMock.Setup(memoryCache => memoryCache.TryGetValue(It.IsAny <object>(), out outValue)) .Returns(false); memoryCacheMock.Setup(memoryCache => memoryCache.CreateEntry(It.IsAny <string>())) .Callback(() => isCreateMemoCacheEntryCalled = true) .Returns(Mock.Of <ICacheEntry>()); // Middleware setup var middleware = new CommandingMiddleware( async _ => { }, commandRegistryMock.Object, memoryCacheMock.Object, Mock.Of <ILoggerFactory>()); var responseBodyStream = new MemoryStream(); var httpContext = new DefaultHttpContext(new FeatureCollection { [typeof(IHttpResponseBodyFeature)] = new StreamResponseBodyFeature(responseBodyStream), [typeof(IHttpRequestFeature)] = new HttpRequestFeature(), [typeof(IHttpResponseFeature)] = new HttpResponseFeature() }); httpContext.Request.Path = "/command"; httpContext.Request.Method = HttpMethods.Get; await middleware.InvokeAsync(httpContext); responseBodyStream.Position = 0; httpContext.Response.StatusCode.Should().Be(StatusCodes.Status200OK); isCreateMemoCacheEntryCalled.Should().BeTrue(); }
public async Task It_should_fail_if_handler_finished_unsuccessfully() { var mapping = ( command : typeof(FailingTestCommand), commandHandler : typeof(FailingTestCommandHandler)); var registryMock = new Mock <ICommandRegistry>(); registryMock.Setup(registry => registry.TryGetValue("failing-test-command", out mapping)) .Returns(true); var middleware = new CommandingMiddleware( async context => { }, registryMock.Object, Mock.Of <IMemoryCache>(), Mock.Of <ILoggerFactory>()); var bodyRequestStream = new MemoryStream(); var bodyResponseStream = new MemoryStream(); await bodyRequestStream.WriteAsync(Encoding.UTF8.GetBytes(@"{""testProperty"": ""test"" }")); bodyRequestStream.Seek(0, SeekOrigin.Begin); var httpContext = new DefaultHttpContext(new FeatureCollection { [typeof(IHttpResponseBodyFeature)] = new StreamResponseBodyFeature(bodyResponseStream), [typeof(IHttpResponseFeature)] = new HttpResponseFeature(), [typeof(IHttpRequestFeature)] = new HttpRequestFeature { Body = bodyRequestStream, Path = "/command/failing-test-command", Method = HttpMethods.Post }, }); httpContext.Request.ContentType = MediaTypeNames.Application.Json; await middleware.InvokeAsync(httpContext); bodyResponseStream.Position = 0; var bodyContent = await new StreamReader(bodyResponseStream).ReadToEndAsync(); httpContext.Response.StatusCode.Should().Be((int)HttpStatusCode.Conflict); JsonDocument.Parse(bodyContent).RootElement .GetProperty("reasons")[0].GetString() .Should().Be("failure reason"); }
public async Task It_should_return_valid_etag() { var commandRegistryMock = new Mock <ICommandRegistry>(); commandRegistryMock.Setup(commandRegistry => commandRegistry.GetEnumerator()) .Returns(new List <(string commandName, Type command, Type commandHandler)> { ( commandName: "successfulTestCommand", command: typeof(SuccessfulTestCommand), commandHandler: typeof(SuccessfulTestCommandHandler)) }.GetEnumerator()); var cacheEntryMock = Mock.Of <ICacheEntry>(); var memoryCacheMock = new Mock <IMemoryCache>(); memoryCacheMock.Setup(memoryCache => memoryCache.CreateEntry(It.IsAny <string>())) .Returns(cacheEntryMock); var middleware = new CommandingMiddleware( async _ => { }, commandRegistryMock.Object, memoryCacheMock.Object, Mock.Of <ILoggerFactory>()); var responseBodyStream = new MemoryStream(); var httpContext = new DefaultHttpContext(new FeatureCollection { [typeof(IHttpResponseBodyFeature)] = new StreamResponseBodyFeature(responseBodyStream), [typeof(IHttpRequestFeature)] = new HttpRequestFeature(), [typeof(IHttpResponseFeature)] = new HttpResponseFeature() }); httpContext.Request.Path = "/command"; httpContext.Request.Method = HttpMethods.Get; await middleware.InvokeAsync(httpContext); responseBodyStream.Position = 0; httpContext.Response.StatusCode.Should().Be(StatusCodes.Status200OK); httpContext.Response.Headers[HeaderNames.ETag].ToString().Should().Be("sjNLocD8ap3ZER7IhQyrqQ=="); }
public async Task It_should_fail_if_handler_throws_command_exception_inside_aggregate_exception() { var mapping = ( command : typeof(ThrowingAggregateExceptionTestCommand), commandHandler : typeof(ThrowingAggregateExceptionTestCommandHandler)); var registryMock = new Mock <ICommandRegistry>(); registryMock.Setup(registry => registry.TryGetValue("throw-aggregate-exception-test-command", out mapping)) .Returns(true); var middleware = new CommandingMiddleware( async context => { }, registryMock.Object, Mock.Of <IMemoryCache>(), Mock.Of <ILoggerFactory>()); var bodyResponseStream = new MemoryStream(); var httpContext = new DefaultHttpContext(new FeatureCollection { [typeof(IHttpResponseBodyFeature)] = new StreamResponseBodyFeature(bodyResponseStream), [typeof(IHttpResponseFeature)] = new HttpResponseFeature(), [typeof(IHttpRequestFeature)] = new HttpRequestFeature { Path = "/command/throw-aggregate-exception-test-command", Method = HttpMethods.Post }, }); httpContext.Request.ContentType = MediaTypeNames.Application.Json; await middleware.InvokeAsync(httpContext); bodyResponseStream.Position = 0; var bodyContent = await new StreamReader(bodyResponseStream).ReadToEndAsync(); httpContext.Response.StatusCode.Should().Be((int)HttpStatusCode.Conflict); JsonDocument.Parse(bodyContent).RootElement .GetProperty("reasons")[0].GetString() .Should().Be("exception message"); }
public async Task It_should_return_304_Not_Modified_if_hash_matches() { var commandRegistryMock = new Mock <ICommandRegistry>(); commandRegistryMock.Setup(commandRegistry => commandRegistry.GetEnumerator()) .Returns(new List <(string commandName, Type command, Type commandHandler)> { ( commandName: "successfulTestCommand", command: typeof(SuccessfulTestCommand), commandHandler: typeof(SuccessfulTestCommandHandler)) }.GetEnumerator()); var cacheEntryMock = Mock.Of <ICacheEntry>(); var memoryCacheMock = new Mock <IMemoryCache>(); memoryCacheMock.Setup(memoryCache => memoryCache.CreateEntry(It.IsAny <string>())) .Returns(cacheEntryMock); var middleware = new CommandingMiddleware( async _ => { }, commandRegistryMock.Object, memoryCacheMock.Object, Mock.Of <ILoggerFactory>()); var httpContext = new DefaultHttpContext(); var memoryStream = httpContext.Features.Get <IHttpResponseBodyFeature>().Stream; httpContext.Request.Path = "/command"; httpContext.Request.Method = HttpMethods.Get; httpContext.Request.Headers.Add(HeaderNames.IfNoneMatch, "sjNLocD8ap3ZER7IhQyrqQ=="); await middleware.InvokeAsync(httpContext); httpContext.Response.StatusCode.Should().Be(StatusCodes.Status304NotModified); }
public async Task It_should_return_json_schema_per_command() { var commandRegistryMock = new Mock <ICommandRegistry>(); commandRegistryMock.Setup(commandRegistry => commandRegistry.GetEnumerator()) .Returns(new List <(string commandName, Type command, Type commandHandler)> { ( commandName: "successfulTestCommand", command: typeof(SuccessfulTestCommand), commandHandler: typeof(SuccessfulTestCommandHandler)) }.GetEnumerator()); var cacheEntryMock = Mock.Of <ICacheEntry>(); var memoryCacheMock = new Mock <IMemoryCache>(); memoryCacheMock.Setup(memoryCache => memoryCache.CreateEntry(It.IsAny <string>())) .Returns(cacheEntryMock); var middleware = new CommandingMiddleware( async _ => { }, commandRegistryMock.Object, memoryCacheMock.Object, Mock.Of <ILoggerFactory>()); var responseBodyStream = new MemoryStream(); var httpContext = new DefaultHttpContext(new FeatureCollection { [typeof(IHttpResponseBodyFeature)] = new StreamResponseBodyFeature(responseBodyStream), [typeof(IHttpRequestFeature)] = new HttpRequestFeature(), [typeof(IHttpResponseFeature)] = new HttpResponseFeature() }); httpContext.Request.Path = "/command"; httpContext.Request.Method = HttpMethods.Get; await middleware.InvokeAsync(httpContext); responseBodyStream.Position = 0; httpContext.Response.StatusCode.Should().Be(StatusCodes.Status200OK); responseBodyStream.ToArray() .Should().BeEquivalentTo( JsonSerializer.SerializeToUtf8Bytes( JsonDocument.Parse(@" { ""successfulTestCommand"": { ""$schema"": ""http://json-schema.org/draft-04/schema#"", ""title"": ""SuccessfulTestCommand"", ""type"": ""object"", ""additionalProperties"": false, ""properties"": { ""TestProperty"": { ""type"": [ ""null"", ""string"" ] } } } }").RootElement)); }