我回来在我们的应用程序中填写一些单元测试,并遇到了以下方法。
public virtual void WriteBodyToRequestStream(HttpWebRequest webRequest, byte[] redirectBodyBuffer) {
var requestStream = webRequest.GetRequestStream();
requestStream.Write(redirectBodyBuffer, 0, redirectBodyBuffer.Length);
requestStream.Close();
}
我正在尝试编写一个简单的测试,在流中插入一些文本。我遇到的问题是弄清楚如何在将文本写入流后对其进行验证。这是我到目前为止所拥有的。
[Test, Ignore("not working yet")]
public void Should_write_to_request_stream()
{
var request = WebRequest.Create("http://localhost/") as HttpWebRequest;
request.Method = "POST";
var encoding = new System.Text.UTF8Encoding();
var bytes = encoding.GetBytes("testing");
_helper.WriteBodyToRequestStream(request, bytes);
var stream = request.GetRequestStream() as MemoryStream;
var result = System.Text.Encoding.UTF8.GetString(stream.ToArray());
Assert.AreEqual("testing", result);
}
当我运行这个测试时,stream
var 为空。