Пример #1
0
        public void ShouldReturnEmptyStringWhenStatusCodeIsNotOk()
        {
            var moq = new Mock <IWebRequest>();
            var moqHttpWebRequest  = new Mock <HttpWebRequest>();
            var moqHttpWebResponse = new Mock <HttpWebResponse>();

            moqHttpWebResponse.Setup(_ => _.StatusCode).Returns(HttpStatusCode.BadRequest);
            moqHttpWebRequest.Setup(_ => _.GetResponse()).Returns(moqHttpWebResponse.Object);
            moq.Setup(_ => _.Create(It.IsAny <string>())).Returns(moqHttpWebRequest.Object);

            var retriever = new HtmlRetriever(moq.Object);

            var result = retriever.Retrieve("test");

            result.Should().BeEmpty();
        }
Пример #2
0
        public void ShouldReturnContentStringWhenStatusCodeIsOk()
        {
            var resultContent      = "<html><b>I am the test</b></html>";
            var resultContentBytes = Encoding.ASCII.GetBytes(resultContent);
            var moq = new Mock <IWebRequest>();
            var moqHttpWebRequest  = new Mock <HttpWebRequest>();
            var moqHttpWebResponse = new Mock <HttpWebResponse>();

            moqHttpWebResponse.Setup(_ => _.StatusCode).Returns(HttpStatusCode.OK);

            moqHttpWebResponse.Setup(_ => _.GetResponseStream()).Returns(new MemoryStream(resultContentBytes));

            moqHttpWebRequest.Setup(_ => _.GetResponse()).Returns(moqHttpWebResponse.Object);
            moq.Setup(_ => _.Create(It.IsAny <string>())).Returns(moqHttpWebRequest.Object);

            var retriever = new HtmlRetriever(moq.Object);

            var result = retriever.Retrieve("test");

            result.Should().Be(resultContent);
        }