public
Last active

Classes for mocking HttpContext in ASP.NET MVC with Moq.

  • Download Gist
gistfile1.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
using System.Collections.Generic;
using System.Security.Principal;
using System.Web;
using Moq;
 
namespace Tests.Utils
{
public class HttpContextMock
{
public HttpContextMock(IEnumerable<KeyValuePair<string, string>> requestParams)
: this(null, false, requestParams)
{}
 
public HttpContextMock(IPrincipal user = null, bool isAjaxRequest = false,
IEnumerable<KeyValuePair<string, string>> requestParams = null)
{
Server = new Mock<HttpServerUtilityBase>(MockBehavior.Loose);
 
Response = new Mock<HttpResponseBase>(MockBehavior.Strict);
Response.Setup(res => res.ApplyAppPathModifier(It.IsAny<string>())).Returns((string virtualPath) => virtualPath);
Response.Setup(res => res.Cookies).Returns(new HttpCookieCollection());
 
Request = new Mock<HttpRequestBase>(MockBehavior.Strict);
Request.Setup(req => req.UserHostAddress).Returns("127.0.0.1");
Request.Setup(req => req.ApplicationPath).Returns("~/");
Request.Setup(req => req.AppRelativeCurrentExecutionFilePath).Returns("~/");
Request.Setup(req => req.PathInfo).Returns(string.Empty);
Request.Setup(req => req.Cookies).Returns(new HttpCookieCollection());
Request.Setup(req => req.QueryString).Returns(new NameValueCollection());
Request.Setup(req => req.Form).Returns(new NameValueCollection());
 
if(isAjaxRequest)
AddRequestParameter("X-Requested-With", "XMLHttpRequest");
 
if(requestParams != null)
AddRequestParameters(requestParams);
 
Session = new HttpSessionMock();
Items = new Dictionary<object, object>();
 
Context = new Mock<HttpContextBase>();
Context.SetupGet(c => c.Request).Returns(Request.Object);
Context.SetupGet(c => c.Response).Returns(Response.Object);
Context.SetupGet(c => c.Server).Returns(Server.Object);
Context.SetupGet(c => c.Session).Returns(Session);
Context.SetupGet(c => c.Items).Returns(Items);
 
if (user == null) return;
 
Context.SetupGet(x => x.User).Returns(user);
Context.SetupGet(x => x.Request.IsAuthenticated)
.Returns(user.Identity.IsAuthenticated);
}
 
public HttpContextBase HttpContext { get { return Context.Object; } }
 
public Mock<HttpContextBase> Context { get; set; }
public Mock<HttpServerUtilityBase> Server { get; set; }
public Mock<HttpResponseBase> Response { get; set; }
public Mock<HttpRequestBase> Request { get; set; }
public HttpSessionMock Session { get; set; }
public Dictionary<object, object> Items { get; set; }
 
public void AddRequestParameters(IEnumerable<KeyValuePair<string, string>> parameters)
{
foreach (var param in parameters)
{
AddRequestParameter(param.Key, param.Value);
}
}
 
public void AddRequestParameter(string name, string value)
{
Request.Setup(r => r[name]).Returns(value);
}
}
 
public class HttpSessionMock : HttpSessionStateBase
{
private readonly Dictionary<string, object> _objects = new Dictionary<string, object>();
 
public override object this[string name]
{
get { return (_objects.ContainsKey(name)) ? _objects[name] : null; }
set { _objects[name] = value; }
}
 
public override void Add(string name, object value)
{
_objects.Add(name, value);
base.Add(name, value);
}
 
public override void Remove(string name)
{
_objects.Remove(name);
base.Remove(name);
}
 
public override void Clear()
{
_objects.Clear();
base.Clear();
}
 
public override void RemoveAll()
{
Clear();
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.