Exemplo n.º 1
0
        public async Task When_UserAsksForMovieThatDoesntExist_Then_Return400Status()
        {
            // arrange
            var client = Fixture.Server.CreateClient();

            client.CreateSession();
            var response = ResponseFactory.FromRecordedFile($"{Fixture.StubsFolder}/OmdbApi/Real_Responses/Happy/200_MovieNotFound.txt");

            client.AppendHttpCallStub(HttpMethod.Get, new System.Uri($"{MovieUrl}&t=some_weird_title"), response);

            // act
            var httpResponse = await client.GetAsync("/api/movie/some_weird_title");

            using (new AssertionScope())
            {
                // assert logs
                client.GetSessionLogs().Should().BeEmpty();

                // assert outgoing
                var outgoingRequests = client.GetSessionOutgoingRequests();
                outgoingRequests.Count.Should().Be(1);
                outgoingRequests[0].GetEndpoint().Should().Be($"GET {MovieUrl}&t=some_weird_title");
                outgoingRequests[0].GetHeaderValue("Referer").Should().Be(MovieProject.Logic.Constants.Website);

                // assert return
                httpResponse.StatusCode.Should().Be(HttpStatusCode.NotFound);
                var message = await httpResponse.ReadBody();

                message.Should().Be("Search terms didn't match any movie");
            }
        }
        public async Task When_FilesAlreadyExistInFolder_And_ValidRequestResponse_Then_CreateTextFileInRightFormat_And_CanLoadFile()
        {
            // this test relies on usage of the FileSystem, it's unusual, but considered best balance
            // of efficient + realist testing vs potential downsides, using the temporary folder of the machine
            // should be ok

            // arrange
            if (Directory.Exists(folder))
            {
                Directory.Delete(folder, true);                          // if folder exists, it was the result of previous tests
            }
            // create folder and some files in it, so we make sure our code creates the next file correctly
            Directory.CreateDirectory(folder);
            File.CreateText(Path.Combine(folder, "OK_00001.txt"));
            File.CreateText(Path.Combine(folder, "Forbidden.txt"));
            File.CreateText(Path.Combine(folder, "Whatever.txt"));
            File.CreateText(Path.Combine(folder, "some_random_file.txt"));

            var sut = new RecordingManager(folder);

            var input = CreateLog();

            // act
            var fileName = sut.Save(input);

            // asserts
            fileName.Should().Be("OK_0002");
            var createdFile = Path.Combine(folder, "OK_0002.txt");

            File.Exists(createdFile).Should().BeTrue();

            var content = File.ReadAllText(createdFile);

            content.Should().Be(expectedFileContent);

            var deserializedResponse = ResponseFactory.FromRecordedFile(createdFile);

            deserializedResponse.StatusCode.Should().Be(input.Response.Status);
            (await deserializedResponse.Content.ReadAsStringAsync()).Should().Be(input.Response.Body);

            foreach (var item in input.Response.Headers)
            {
                deserializedResponse.Headers.ShouldContainHeader(item.Key, item.Value);
            }
        }
Exemplo n.º 3
0
        public async Task When_UserAsksForMovieWithMostFields_Then_ReturnMovieProperly()
        {
            // arrange
            var client = Fixture.Server.CreateClient();

            client.CreateSession();
            var response       = ResponseFactory.FromRecordedFile($"{Fixture.StubsFolder}/OmdbApi/Real_Responses/Happy/200_ContainsMostFields_TheMatrix.txt");
            var matrixMovieUrl = $"{MovieUrl}&t=matrix";

            client.AppendHttpCallStub(HttpMethod.Get, new System.Uri(matrixMovieUrl), response);

            // act
            var httpResponse = await client.GetAsync("/api/movie/matrix");

            using (new AssertionScope())
            {
                // assert logs
                client.GetSessionLogs().Should().BeEmpty();

                // assert outgoing
                var outgoingRequests = client.GetSessionOutgoingRequests();
                outgoingRequests.Count.Should().Be(1);
                outgoingRequests[0].GetEndpoint().Should().Be($"GET {matrixMovieUrl}");
                outgoingRequests[0].GetHeaderValue("Referer").Should().Be(MovieProject.Logic.Constants.Website);

                // assert return
                httpResponse.StatusCode.Should().Be(HttpStatusCode.OK);

                var movie = await httpResponse.ReadJsonBody <MovieProject.Logic.DTO.Media>();

                movie.Id.Should().Be("tt0133093");
                movie.Name.Should().Be("The Matrix");
                movie.Year.Should().Be("1999");
                movie.Runtime.Should().Be("2.3h");
                movie.Plot.Should().Be("A computer hacker learns from mysterious rebels about the true nature of his reality and his role in the war against its controllers.");
                movie.Language.Should().Be(MovieProject.Logic.DTO.Media.Languages.English);
            }
        }