Beispiel #1
0
        public async Task RetrieveAsteroidsHandlerShouldThrowJsonReaderException()
        {
            var repository = new Mock <IAsteroidRepository>();
            var logger     = new Mock <ILogger <RetrieveAsteroidsHandler> >();
            var httpMock   = new Mock <HttpMessageHandler>(MockBehavior.Strict);

            httpMock.Protected()
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>()
                )
            .ReturnsAsync(new HttpResponseMessage(System.Net.HttpStatusCode.OK)
            {
                Content = new StringContent("<xml>")
            })
            .Verifiable();
            var publisher = new Mock <IEventPublisher <AsteroidRetrievedEvent> >();

            var baseUrl   = "http://www.google.com/";
            var startPage = string.Empty;
            var command   = new RetrieveAsteroidsCommand(baseUrl, startPage);
            var handler   = new RetrieveAsteroidsHandler(repository.Object, new HttpClient(httpMock.Object), logger.Object);

            await Assert.ThrowsAnyAsync <JsonException>(() =>
                                                        handler.Handle(command, new System.Threading.CancellationToken())
                                                        );
        }
Beispiel #2
0
        public async Task RetrieveAsteroidsHandlerShouldThrowHttpResponseException()
        {
            var repository = new Mock <IAsteroidRepository>();
            var logger     = new Mock <ILogger <RetrieveAsteroidsHandler> >();
            var httpMock   = new Mock <HttpMessageHandler>(MockBehavior.Strict);

            httpMock.Protected()
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>()
                )
            .ReturnsAsync(new HttpResponseMessage(System.Net.HttpStatusCode.InternalServerError))
            .Verifiable();
            var publisher = new Mock <IEventPublisher <AsteroidRetrievedEvent> >();

            var baseUrl   = "http://www.google.com/";
            var startPage = string.Empty;
            var command   = new RetrieveAsteroidsCommand(baseUrl, startPage);
            var handler   = new RetrieveAsteroidsHandler(repository.Object, new HttpClient(httpMock.Object), logger.Object);

            var ex = await Assert.ThrowsAsync <HttpResponseException>(() =>
                                                                      handler.Handle(command, new System.Threading.CancellationToken())
                                                                      );

            Assert.Equal <int>(0,
                               Uri.Compare(new Uri($"{baseUrl}{startPage}"),
                                           ex.Uri, UriComponents.HttpRequestUrl, UriFormat.UriEscaped, StringComparison.InvariantCultureIgnoreCase)
                               );
        }
Beispiel #3
0
        public async Task RetrieveAsteroidsHandlerShouldNotThrowExceptionIfNearEarthObjectsIsMissing()
        {
            var repository = new Mock <IAsteroidRepository>();
            var logger     = new Mock <ILogger <RetrieveAsteroidsHandler> >();
            var httpMock   = new Mock <HttpMessageHandler>(MockBehavior.Strict);

            httpMock.Protected()
            .SetupSequence <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>()
                )
            .ReturnsAsync(new HttpResponseMessage()
            {
                StatusCode = System.Net.HttpStatusCode.OK,
                Content    = new StringContent("{\"links\":{ \"next\": \"http://www.google.com/\" }}")
            })
            .ReturnsAsync(new HttpResponseMessage(System.Net.HttpStatusCode.OK)
            {
                Content = new StringContent("{}")
            });
            var publisher = new Mock <IEventPublisher <AsteroidRetrievedEvent> >();

            var baseUrl   = "http://www.google.com/";
            var startPage = string.Empty;
            var command   = new RetrieveAsteroidsCommand(baseUrl, startPage);
            var handler   = new RetrieveAsteroidsHandler(repository.Object, new HttpClient(httpMock.Object), logger.Object);

            repository.Verify(r => r.BulkUpsert(It.IsAny <IEnumerable <AsteroidInfo> >()), Times.Never());
            await handler.Handle(command, new System.Threading.CancellationToken());
        }
Beispiel #4
0
        public async Task RetrieveAsteroidsHandlerShouldThrowUriFormatException()
        {
            var repository = new Mock <IAsteroidRepository>();
            var logger     = new Mock <ILogger <RetrieveAsteroidsHandler> >();
            var httpMock   = new Mock <HttpMessageHandler>(MockBehavior.Strict);

            httpMock.Protected()
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>()
                )
            .ReturnsAsync(new HttpResponseMessage(System.Net.HttpStatusCode.OK))
            .Verifiable();
            var publisher = new Mock <IEventPublisher <AsteroidRetrievedEvent> >();

            var command = new RetrieveAsteroidsCommand("invalid base url", "invalid start page url");
            var handler = new RetrieveAsteroidsHandler(repository.Object, new HttpClient(httpMock.Object), logger.Object);

            await Assert.ThrowsAsync <UriFormatException>(() =>
                                                          handler.Handle(command, new System.Threading.CancellationToken())
                                                          );
        }