Пример #1
0
        public async Task Should_invoke_predispatch_hook()
        {
            var          module           = new CommandHandlerModule();
            string       correlationId    = null;
            const string correlationIdKey = "CorrelationId";

            module.For <Command>().Handle((commandMessage, __) =>
            {
                correlationId = commandMessage.Metadata.Get <string>(correlationIdKey);
                return(Task.FromResult(0));
            });
            var settings = new CommandHandlingSettings(new CommandHandlerResolver(module))
            {
                OnPredispatch = (metadata, headers) =>
                {
                    var correlationIdHeader = headers.SingleOrDefault(kvp => kvp.Key == correlationIdKey);
                    if (correlationIdHeader.Value != null)
                    {
                        metadata[correlationIdKey] = correlationIdHeader.Value.SingleOrDefault();
                    }
                }
            };

            var midFunc = CommandHandlingMiddleware.HandleCommands(settings);

            using (var client = midFunc.CreateEmbeddedClient())
            {
                await client.PutCommand(new Command(), Guid.NewGuid(), customizeRequest : request =>
                {
                    request.Headers.Add(correlationIdKey, "cor-1");
                });

                correlationId.Should().Be("cor-1");
            }
        }
        public void When_add_duplicate_command_then_should_throw()
        {
            var module = new CommandHandlerModule();

            module.For <Command>().Handle((_, __) => Task.FromResult(0));

            Action act = () => module.For <Command>().Handle((_, __) => Task.FromResult(0));

            act.ShouldThrow <InvalidOperationException>();
        }
        public void Can_get_command_types()
        {
            var module = new CommandHandlerModule();

            module.For <Command>().Handle((_, __) => Task.FromResult(0));

            var commandTypes = module.CommandTypes.ToList();

            commandTypes.Should().HaveCount(1);
            commandTypes.Single().Should().Be(typeof(Command));
        }
        private CommandHandlerModule CreateCommandHandlerModule()
        {
            var module = new CommandHandlerModule();

            module.For <Command>()
            .Handle(commandMessage => { _receivedCommands.Add(commandMessage); });

            module.For <CommandThatThrowsStandardException>()
            .Handle(_ => { throw new InvalidOperationException(); });

            module.For <CommandThatThrowsProblemDetailsException>()
            .Handle((_, __) =>
            {
                var problemDetails = new HttpProblemDetails
                {
                    Status   = (int)HttpStatusCode.BadRequest,
                    Type     = "http://localhost/type",
                    Detail   = "You done goof'd",
                    Instance = "http://localhost/errors/1",
                    Title    = "Jimmies Ruslted"
                };
                throw new HttpProblemDetailsException <HttpProblemDetails>(problemDetails);
            });

            module.For <CommandThatThrowsMappedException>()
            .Handle((_, __) => { throw new ApplicationException("Mapped application exception"); });

            module.For <CommandThatThrowsCustomProblemDetailsException>()
            .Handle((_, __) =>
            {
                var problemDetails = new CustomHttpProblemDetails()
                {
                    Status   = (int)HttpStatusCode.BadRequest,
                    Type     = "http://localhost/type",
                    Detail   = "You done goof'd",
                    Instance = "http://localhost/errors/1",
                    Title    = "Jimmies Ruslted",
                    Name     = "Damo"
                };
                throw new CustomProblemDetailsException(problemDetails);
            });

            return(module);
        }