我如何模拟带有CharacterSet属性的HttpWebResponse进行单元测试?在我的代码中,我正在检查HttpWebResponse.CharacterSet,如下所示。我可以模拟HttpWebResponse,但在获取属性“ CharacterSet”时却得到空引用异常。请注意,我无法在模拟中设置“ CharacterSet”,因为它是只读的非虚拟属性。我正在使用.Net 4.6.1
public string ReadResponse(HttpWebResponse response)
{
var encoding = (response.CharacterSet == null || response.CharacterSet == "") ? Encoding.UTF8 : Encoding.GetEncoding(response.CharacterSet);
using (var stream = response.GetResponseStream())
{
var reader = new StreamReader(stream, encoding);
var responseString = reader.ReadToEnd();
return responseString;
}
}
我为HttpWebReponse编写的模拟文件是
public static HttpWebResponse CreateRequestWithResponse(string responseContent)
{
var response = new Mock<HttpWebResponse>(MockBehavior.Loose);
var responseStream = new MemoryStream(Encoding.UTF8.GetBytes(responseContent));
response.Setup(c => c.StatusCode).Returns(HttpStatusCode.OK);
response.Setup(c => c.ContentType).Returns("text/xml;charset=\"utf-8\"");
response.Setup(c => c.GetResponseStream()).Returns(responseStream);
var request = new Mock<HttpWebRequest>();
request.Setup(a => a.ContentType).Returns("text/xml;charset=\"utf-8\"");
request.Setup(s => s.GetResponse()).Returns(response.Object);
return response.Object;
}
这是我在获取“ CharacterSet”时遇到的错误,
答案 0 :(得分:1)
我对此做了进一步的研究,看来您将无法做自己想做的事情。问题在于CharacterSet属性是使用headers集合派生的。虽然您可以覆盖Headers属性,但CharacterSet属性将直接在该字段上查看。标头集合字段设置为非null值的唯一方法是通过内部构造函数,该构造函数从名为cordata的对象中复制它。
结果是没有一种模拟CharacterSet的安全方法。我想您可以使用反射来找到该字段并以这种方式进行设置,但这似乎不是一个好主意,因为实现可能会更改并破坏您的测试。
答案 1 :(得分:1)
了解source code后,您可以手动设置HttpWebResponse
属性的Get-Handler中使用的CharacterSet
类的私有属性:
public static HttpWebResponse CreateRequestWithResponse(string responseContent)
{
var response = new Mock<HttpWebResponse>(MockBehavior.Loose);
var responseStream = new MemoryStream(Encoding.UTF8.GetBytes(responseContent));
response.Setup(c => c.StatusCode).Returns(HttpStatusCode.OK);
response.Setup(c => c.ContentType).Returns("text/xml;charset=\"utf-8\"");
response.Setup(c => c.GetResponseStream()).Returns(responseStream);
HttpWebResponse result = response.Object;
// Set private field behind CharacterSet property
var prop = typeof(HttpWebResponse).GetField("m_CharacterSet", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
prop.SetValue(result, "utf-8");
// Set private field used in CharacterSet getter
prop = typeof(HttpWebResponse).GetField("m_HttpResponseHeaders", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
prop.SetValue(result, new WebHeaderCollection());
return result;
}
这取决于内部实现。因此,如果Microsoft更改HttpWebResponse
类的内部实现,则测试将被破坏。