Exemple #1
0
        public async Task AwaitResource_awaits_until_response_is_success(
            HttpMessageHandlerDouble stub,
            Uri endpoint,
            string path,
            [Range(1, 2000)] int delay)
        {
            // Arrange
            bool complete = false;

            bool predicate(HttpRequestMessage request)
            => request.Method == HttpMethod.Get &&
            request.RequestUri == new Uri(endpoint, path) &&
            complete;

            stub.AddAnswer(predicate, HttpStatusCode.OK);
            stub.AddAnswer(_ => true, HttpStatusCode.NotFound);

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

            // Act
            Task.Delay(millisecondsDelay: delay)
            .ContinueWith(_ => complete = true)
            .GetAwaiter();

            var stopwatch = Stopwatch.StartNew();

            await sut.AwaitResource(new Uri(path, UriKind.Relative));

            stopwatch.Stop();

            // Assert
            stopwatch.ElapsedMilliseconds.Should().BeCloseTo(delay, delta: 550);
        }
Exemple #2
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 });
        }
Exemple #3
0
        public async Task given_success_response_then_FindMovie_returns_response_content_correctly(
            HttpMessageHandlerDouble handlerStub,
            Uri endpoint,
            MovieDto content)
        {
            // Arrange
            Guid movieId = content.Id;

            bool predicate(HttpRequestMessage request)
            {
                string path = $"api/queries/Movies/{movieId}";

                return(request.Method == HttpMethod.Get &&
                       request.RequestUri == new Uri(endpoint, path) &&
                       request.Content == default);
            }

            var response = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = CreateContent(content),
            };

            handlerStub.AddAnswer(predicate, answer: response);

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

            // Act
            MovieDto actual = await sut.FindMovie(movieId);

            // Assert
            actual.Should().BeEquivalentTo(content);
        }
Exemple #4
0
        public async Task GetAllMovies_returns_response_content_correctly(
            HttpMessageHandlerDouble handlerStub,
            Uri endpoint,
            ImmutableArray <MovieDto> content)
        {
            // Arrange
            bool predicate(HttpRequestMessage request)
            {
                string path = "api/queries/Movies";

                return(request.Method == HttpMethod.Get &&
                       request.RequestUri == new Uri(endpoint, path) &&
                       request.Content == default);
            }

            var response = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = CreateContent(content),
            };

            handlerStub.AddAnswer(predicate, answer: response);

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

            // Act
            IEnumerable <MovieDto> actual = await sut.GetAllMovies();

            // Assert
            actual.Should().BeEquivalentTo(content);
        }
Exemple #5
0
        public async Task given_not_found_response_then_FindMovie_returns_null(
            HttpMessageHandlerDouble handlerStub, Uri endpoint, Guid movieId)
        {
            // Arrange
            bool predicate(HttpRequestMessage request)
            {
                string path = $"api/queries/Movies/{movieId}";

                return(request.Method == HttpMethod.Get &&
                       request.RequestUri == new Uri(endpoint, path) &&
                       request.Content == default);
            }

            handlerStub.AddAnswer(predicate, HttpStatusCode.NotFound);

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

            // Act
            MovieDto actual = await sut.FindMovie(movieId);

            // Assert
            actual.Should().BeNull();
        }
Exemple #6
0
        public async Task given_bad_command_then_SendCreateTheaterCommand_returns_error_result(
            HttpMessageHandlerDouble handlerStub,
            Uri endpoint,
            CreateNewTheater command,
            ModelStateDictionary state,
            string key,
            string errorMessage)
        {
            // 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.BadRequest);

            state.AddModelError(key, errorMessage);
            response.Content = CreateContent(new SerializableError(state));

            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 <Error <TheaterLocation> >();
            actual.Should().BeEquivalentTo(new { Message = errorMessage });
        }