Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
HtmlRetrieverTest
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using FluentAssertions;
using LinkReader.Installer;
using LinkReader.Reader;
using LinkReader.Retriever;
using LinkReader.Retriever.Interfaces;
using Moq;
using Xunit;
namespace LinkReaderTest
{
public class HtmlRetrieverTest
{
[Fact]
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();
}
[Fact]
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);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.