3

How can i test return value of "ProcessRequest" method in a generic handler with unit Test?

 public class Handler1 : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.Write("Hello World");
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}
sadeghhp
  • 621
  • 14
  • 28
  • What is your assertion? – haim770 Dec 28 '14 at 08:45
  • Assert.AreEqual("Hello World", result); assertion on response – sadeghhp Dec 28 '14 at 08:46
  • Show your current Unit-Testing code. How is the `HttpContext` created? – haim770 Dec 28 '14 at 08:56
  • possible duplicate of [Unit Testing ASP.NET Generic Handlers without using HttpWebRequest](http://stackoverflow.com/questions/12560213/unit-testing-asp-net-generic-handlers-without-using-httpwebrequest) – ale Dec 28 '14 at 09:02
  • I want to use something like mock , but the problem is that mock cannot create httpcontext ! , it can only mock HttpContextBase : var context = new Mock(); – sadeghhp Dec 28 '14 at 09:03

1 Answers1

2

Instead of using mock, try to create test HttpContext with SimpleWorkerRequest like this:

SimpleWorkerRequest testRequest = new SimpleWorkerRequest("","","", null, new StringWriter());
HttpContext testContext = new HttpContext(testRequest);
HttpContext.Current = testContext;

Then you could create your handler and provide testContext to the ProcessRequest method:

var handler = new Handler1();
handler.ProcessRequest(testContext);

Then you could check HttpContext.Current.Response to make assertion about your test.

UPDATE:

I am attaching the full example of working unit-test(implementation of OutputFilterStream was taken from here):

    [TestFixture]
    [Category("Unit")]
    public class WhenProcessingRequest
    {
        public class Handler1 : IHttpHandler
        {

            public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "text/plain";
                context.Response.Write("Hello World");
            }

            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }

        public class OutputFilterStream : Stream
        {
            private readonly Stream InnerStream;
            private readonly MemoryStream CopyStream;

            public OutputFilterStream(Stream inner)
            {
                this.InnerStream = inner;
                this.CopyStream = new MemoryStream();
            }

            public string ReadStream()
            {
                lock (this.InnerStream)
                {
                    if (this.CopyStream.Length <= 0L ||
                        !this.CopyStream.CanRead ||
                        !this.CopyStream.CanSeek)
                    {
                        return String.Empty;
                    }

                    long pos = this.CopyStream.Position;
                    this.CopyStream.Position = 0L;
                    try
                    {
                        return new StreamReader(this.CopyStream).ReadToEnd();
                    }
                    finally
                    {
                        try
                        {
                            this.CopyStream.Position = pos;
                        }
                        catch { }
                    }
                }
            }


            public override bool CanRead
            {
                get { return this.InnerStream.CanRead; }
            }

            public override bool CanSeek
            {
                get { return this.InnerStream.CanSeek; }
            }

            public override bool CanWrite
            {
                get { return this.InnerStream.CanWrite; }
            }

            public override void Flush()
            {
                this.InnerStream.Flush();
            }

            public override long Length
            {
                get { return this.InnerStream.Length; }
            }

            public override long Position
            {
                get { return this.InnerStream.Position; }
                set { this.CopyStream.Position = this.InnerStream.Position = value; }
            }

            public override int Read(byte[] buffer, int offset, int count)
            {
                return this.InnerStream.Read(buffer, offset, count);
            }

            public override long Seek(long offset, SeekOrigin origin)
            {
                this.CopyStream.Seek(offset, origin);
                return this.InnerStream.Seek(offset, origin);
            }

            public override void SetLength(long value)
            {
                this.CopyStream.SetLength(value);
                this.InnerStream.SetLength(value);
            }

            public override void Write(byte[] buffer, int offset, int count)
            {
                this.CopyStream.Write(buffer, offset, count);
                this.InnerStream.Write(buffer, offset, count);
            }
        }


        [Test]
        public void should_write_response()
        {
            //arrange
            SimpleWorkerRequest testRequest = new SimpleWorkerRequest("", "", "", null, new StringWriter());                
            HttpContext testContext = new HttpContext(testRequest);                
            testContext.Response.Filter = new OutputFilterStream(testContext.Response.Filter);                

            //act
            var handler = new Handler1();
            handler.ProcessRequest(testContext);                
            testContext.Response.End();//end request here(if it is not done in your Handler1)

            //assert
            var result = ((OutputFilterStream)testContext.Response.Filter).ReadStream();
            Assert.AreEqual("Hello World", result);
        }
    }
Community
  • 1
  • 1
alekseevi15
  • 1,732
  • 2
  • 16
  • 20
  • perfect! but how to get "Hello World!" string? – sadeghhp Dec 28 '14 at 13:50
  • As it turned out there is no "easy" way to extract response from HttpContext.Current.Response. But it's [doable](http://stackoverflow.com/questions/16189834/read-httpcontext-current-response-outputstream-in-global-asax?lq=1). You need to create Filter to sniff response. Unfortunately I don't have VS at hand today to try it. – alekseevi15 Dec 28 '14 at 15:06
  • 1
    @sadeghhp, I've just updated answer to include fully working unit-test. – alekseevi15 Dec 29 '14 at 09:41