feanz
12/11/2013 - 10:16 AM

Mock Http Context Fixture (Builder class for test helping)

Mock Http Context Fixture (Builder class for test helping)

 public class HttpContextBuilder
    {
        private Mock<HttpContextBase> _context;
        private Mock<HttpRequestBase> _request;
        private Mock<HttpResponseBase> _response;
        private Mock<HttpSessionStateBase> _session;
        private Mock<HttpServerUtilityBase> _server;
        private Mock<IPrincipal> _user;
        private Mock<IIdentity> _identity;

        private NameValueCollection _serverVariables;
        private HttpCookieCollection _requestCookies;
        private HttpCookieCollection _responseCookies;

        public HttpContextBase HttpContextBase { get; private set; }

        public HttpContextBuilder()
        {
            SetDefaults();
        }

        public HttpContextBuilder WithQueryString(string queryString)
        {
            _request.Setup(req => req.QueryString).Returns(HttpUtility.ParseQueryString(queryString));
            return this;
        }

        public HttpContextBuilder WithHttpMethod(string httpMethod)
        {
            _request.Setup(req => req.HttpMethod).Returns(httpMethod);
            return this;
        }

        public HttpContextBuilder WithUrl(string url)
        {
            _request.Setup(req => req.Url).Returns(new Uri("url"));
            return this;
        }

        public HttpContextBuilder WithHttpMethod(HttpVerbs httpMethod)
        {
            _request.Setup(req => req.HttpMethod).Returns(httpMethod.ToString());
            return this;
        }

        public HttpContextBase Build()
        {
            HttpContextBase = _context.Object;
            return HttpContextBase;
        }

        private void SetDefaults()
        {
            var mockRepository = new MockRepository(MockBehavior.Loose) { DefaultValue = DefaultValue.Mock };
            _context = mockRepository.Create<HttpContextBase>();
            _request = mockRepository.Create<HttpRequestBase>();
            _response = mockRepository.Create<HttpResponseBase>();
            _session = mockRepository.Create<HttpSessionStateBase>();
            _server = mockRepository.Create<HttpServerUtilityBase>();
            _user = mockRepository.Create<IPrincipal>();
            _identity = mockRepository.Create<IIdentity>();

            _requestCookies = new HttpCookieCollection();
            _responseCookies = new HttpCookieCollection();
            _serverVariables = new NameValueCollection();

            _user.Setup(ctx => ctx.Identity).Returns(_identity.Object);
            _identity.Setup(id => id.IsAuthenticated).Returns(true);
            _identity.Setup(id => id.Name).Returns("test");

            var requestContext = mockRepository.Create<RequestContext>();
            requestContext.Setup(x => x.HttpContext).Returns(_context.Object);
            requestContext.Setup(x => x.RouteData).Returns(new RouteData());

            _request.Setup(req => req.Url).Returns(new Uri("http://www.someurl.com"));
            _request.Setup(req => req.QueryString).Returns(new NameValueCollection());
            _request.Setup(req => req.RequestContext).Returns(requestContext.Object);
            _request.Setup(req => req.HttpMethod).Returns("GET");
            _request.Setup(r => r.Cookies).Returns(_requestCookies);
            _request.Setup(r => r.ServerVariables).Returns(_serverVariables);

            _response.Setup(r => r.Cookies).Returns(_responseCookies);

            _context.Setup(ctx => ctx.Request).Returns(_request.Object);
            _context.Setup(ctx => ctx.Response).Returns(_response.Object);
            _context.Setup(ctx => ctx.Session).Returns(_session.Object);
            _context.Setup(ctx => ctx.Server).Returns(_server.Object);
            _context.Setup(ctx => ctx.User).Returns(_user.Object);
        }
    }