### dependency
+ NUnit 3
+ NSubstitute
+ FluentAssertions
### production code
``` csharp
public class EZHttp
{
public string Get(string url)
{
var requset = (HttpWebRequest)WebRequest.Create(url);
requset.Method = "GET";
requset.Timeout = 1;
requset.ReadWriteTimeout = 2;
requset.ContentType = "default";
using (HttpWebResponse resopnse = (HttpWebResponse)requset.GetResponse())
using (Stream s = resopnse.GetResponseStream())
using (StreamReader sr = new StreamReader(s, Encoding.UTF8))
{
return sr.ReadToEnd();
}
}
}
```
### testing code
``` csharp
[Test()]
public void GetTest_Default_ParametersOfRequestMatchExpected()
{
// arrange
var stubResponseString = "response";
var stubResponseStream = new MemoryStream(Encoding.UTF8.GetBytes(stubResponseString));
var stubResponse = Substitute.For<HttpWebResponse>();
stubResponse.GetResponseStream().Returns(stubResponseStream);
var mockRequest = Substitute.ForPartsOf<HttpWebRequest>();
mockRequest.When(r => r.GetResponse()).DoNotCallBase();
mockRequest.GetResponse().Returns(stubResponse);
// Must init header collection here to avoid NullReferenceExcetpion while try to access ContentType or other properties
// depend on private field '_HttpRequestHeaders' in framework.
// Headers also depend on it, that why we init it via Headers.
mockRequest.Headers = new WebHeaderCollection();
var stubUrl = "http://url";
// mock IWebRequestCreate makes WebRequest.Create(url) return mockedRequest,
// but unable apply to WebRequest.CreateHttp(url)
var stubRequestCreater = Substitute.For<IWebRequestCreate>();
stubRequestCreater.Create(Arg.Any<Uri>()).Returns(mockRequest);
WebRequest.RegisterPrefix(stubUrl, stubRequestCreater);
var target = new EZHttp();
string expectedMethod = "GET";
int expectedTimeout = 1;
int expectedReadWriteTimeout = 2;
string expectedContentType = "default";
// act
target.Get(stubUrl);
// assert
mockRequest.Timeout.Should().Be(expectedTimeout);
mockRequest.ReadWriteTimeout.Should().Be(expectedReadWriteTimeout);
mockRequest.ContentType.Should().Be(expectedContentType);
mockRequest.Method.Should().Be(expectedMethod);
}
```