Example #1
0
        static async Task <int> Main(string[] args)
        {
            Console.WriteLine("Please enter a web page address of a food recipe:");

            var address = Console.ReadLine();

            try
            {
                var webpage = new Uri(address);

                var parser    = new PageParser();
                var pageEater = new EaterMain(client, parser);

                var recipe = await pageEater.ScrapeWebPage(webpage);

                Console.WriteLine(recipe);

                return(0);
            }
            catch (UriFormatException e)
            {
                Console.WriteLine($"Sorry, {address} is not a valid url.");
                return(1);
            }
        }
Example #2
0
        public async Task ThrowsWhenWebAddressNotFound()
        {
            mockMessageHandler.Protected().Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>()
                )
            .ReturnsAsync(new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.NotFound,
                Content    = new StringContent("")
            });

            var main = new EaterMain(client, pageParser.Object);

            await Assert.ThrowsAsync <NoRecipePresentException>(
                async() => await main.ScrapeWebPage(testWebpageAddress)
                );
        }
Example #3
0
        public async Task ReturnsRecipeMarkupWhenPageContainsIt()
        {
            mockMessageHandler.Protected().Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>()
                )
            .ReturnsAsync(new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent("<some_markup_that_contains>\"@type\": \"Recipe\"</some_markup_that_contains>")
            });

            pageParser.Setup(p => p.ParseRecipeFromMarkup(It.IsAny <string>()))
            .Returns("\"@type\": \"Recipe\"");

            var main = new EaterMain(client, pageParser.Object);

            var recipeMarkup = await main.ScrapeWebPage(new Uri("http://example.com/recipe1"));

            Assert.Contains(("\"@type\": \"Recipe\""), recipeMarkup);
        }
Example #4
0
        public async Task ThrowsWhenNoRecipeMarkupExistsInPage()
        {
            mockMessageHandler.Protected().Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>()
                )
            .ReturnsAsync(new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent("Some markup but no recipe")
            });

            pageParser.Setup(p => p.ParseRecipeFromMarkup(It.IsAny <string>()))
            .Returns(string.Empty);

            var main = new EaterMain(client, pageParser.Object);

            await Assert.ThrowsAsync <NoRecipePresentException>(
                async() => await main.ScrapeWebPage(testWebpageAddress)
                );
        }