I'm back filling some unit tests in our app at work, and came across the following method.
public virtual void WriteBodyToRequestStream(HttpWebRequest webRequest, byte[] redirectBodyBuffer) {
var requestStream = webRequest.GetRequestStream();
requestStream.Write(redirectBodyBuffer, 0, redirectBodyBuffer.Length);
requestStream.Close();
}
I'm trying to write a simple test that inserts some text in the stream. The problem I'm having is figuring out how to verify the text after it's written into the stream. Here's what I have so far.
[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);
}
When I run this test, the stream
var is null.