示例#1
0
        public async Task given_valid_command_then_SendCreateTheaterCommand_returns_location_result(
            HttpMessageHandlerDouble handlerStub,
            Uri endpoint,
            CreateNewTheater command,
            TheaterLocation location)
        {
            // Arrange
            bool predicate(HttpRequestMessage req)
            {
                string path = "api/commands/SendCreateTheaterCommand";

                return(req.Method == HttpMethod.Post &&
                       req.RequestUri == new Uri(endpoint, path) &&
                       req.Content is ObjectContent <CreateNewTheater> content &&
                       content.Value == command &&
                       content.Formatter is JsonMediaTypeFormatter);
            }

            var response = new HttpResponseMessage(HttpStatusCode.Accepted);

            response.Headers.Location = location.Uri;

            handlerStub.AddAnswer(predicate, answer: response);

            var sut = new ApiClient(new HttpClient(handlerStub), endpoint);

            // Act
            IResult <TheaterLocation> actual = await
                                               sut.SendCreateTheaterCommand(command);

            // Assert
            actual.Should().BeOfType <Success <TheaterLocation> >();
            actual.Should().BeEquivalentTo(new { Value = location });
        }
示例#2
0
        public Task <IResult <TheaterLocation> > SendCreateTheaterCommand(
            CreateNewTheater command)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            return(Run());

            async Task <IResult <TheaterLocation> > Run()
            {
                string path = "api/commands/SendCreateTheaterCommand";

                HttpResponseMessage response = await
                                               _client.PostAsJsonAsync(new Uri(_endpoint, path), command);

                switch (response.StatusCode)
                {
                case BadRequest:
                    return(await ReadError <TheaterLocation>(response));
                }

                var location = new TheaterLocation(response.Headers.Location);

                return(new Success <TheaterLocation>(location));
            }
        }