Created
June 20, 2013 03:14
-
-
Save RhysC/5820052 to your computer and use it in GitHub Desktop.
Faking out for MVC thanks to Stephen Walther : http://stephenwalther.com/archive/2008/07/01/asp-net-mvc-tip-12-faking-the-controller-context
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FakeControllerContext : ControllerContext | |
{ | |
public FakeControllerContext(IController controller) | |
: this(controller, null, null, null, null, null, null) | |
{ | |
} | |
public FakeControllerContext(IController controller, HttpCookieCollection cookies) | |
: this(controller, null, null, null, null, cookies, null) | |
{ | |
} | |
public FakeControllerContext(IController controller, SessionStateItemCollection sessionItems) | |
: this(controller, null, null, null, null, null, sessionItems) | |
{ | |
} | |
public FakeControllerContext(IController controller, NameValueCollection formParams) | |
: this(controller, null, null, formParams, null, null, null) | |
{ | |
} | |
public FakeControllerContext(IController controller, NameValueCollection formParams, NameValueCollection queryStringParams) | |
: this(controller, null, null, formParams, queryStringParams, null, null) | |
{ | |
} | |
public FakeControllerContext(IController controller, string userName) | |
: this(controller, userName, null, null, null, null, null) | |
{ | |
} | |
public FakeControllerContext(IController controller, string userName, string[] roles) | |
: this(controller, userName, roles, null, null, null, null) | |
{ | |
} | |
public FakeControllerContext | |
( | |
IController controller, | |
string userName, | |
string[] roles, | |
NameValueCollection formParams, | |
NameValueCollection queryStringParams, | |
HttpCookieCollection cookies, | |
SessionStateItemCollection sessionItems | |
) | |
: base(new FakeHttpContext(new FakePrincipal(new FakeIdentity(userName), roles), formParams, queryStringParams, cookies, sessionItems), new RouteData(), controller) | |
{ } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FakeHttpContext : HttpContextBase | |
{ | |
private readonly FakePrincipal _principal; | |
private readonly NameValueCollection _formParams; | |
private readonly NameValueCollection _queryStringParams; | |
private readonly HttpCookieCollection _cookies; | |
private readonly SessionStateItemCollection _sessionItems; | |
public FakeHttpContext(FakePrincipal principal, NameValueCollection formParams, NameValueCollection queryStringParams, HttpCookieCollection cookies, SessionStateItemCollection sessionItems) | |
{ | |
_principal = principal; | |
_formParams = formParams; | |
_queryStringParams = queryStringParams; | |
_cookies = cookies; | |
_sessionItems = sessionItems; | |
} | |
public override HttpRequestBase Request | |
{ | |
get { return new FakeHttpRequest(_formParams, _queryStringParams, _cookies); } | |
} | |
public override IPrincipal User | |
{ | |
get { return _principal; } | |
set { throw new System.NotImplementedException(); } | |
} | |
public override HttpSessionStateBase Session | |
{ | |
get { return new FakeHttpSessionState(_sessionItems); } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FakeHttpRequest : HttpRequestBase | |
{ | |
private readonly NameValueCollection _formParams; | |
private readonly NameValueCollection _queryStringParams; | |
private readonly HttpCookieCollection _cookies; | |
public FakeHttpRequest(NameValueCollection formParams, NameValueCollection queryStringParams, HttpCookieCollection cookies) | |
{ | |
_formParams = formParams; | |
_queryStringParams = queryStringParams; | |
_cookies = cookies; | |
} | |
public override NameValueCollection Form | |
{ | |
get { return _formParams; } | |
} | |
public override NameValueCollection QueryString | |
{ | |
get { return _queryStringParams; } | |
} | |
public override HttpCookieCollection Cookies | |
{ | |
get { return _cookies; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FakeHttpSessionState : HttpSessionStateBase | |
{ | |
private readonly SessionStateItemCollection _sessionItems; | |
public FakeHttpSessionState(SessionStateItemCollection sessionItems) | |
{ | |
_sessionItems = sessionItems; | |
} | |
public override void Add(string name, object value) | |
{ | |
_sessionItems[name] = value; | |
} | |
public override int Count | |
{ | |
get { return _sessionItems.Count; } | |
} | |
public override IEnumerator GetEnumerator() | |
{ | |
return _sessionItems.GetEnumerator(); | |
} | |
public override NameObjectCollectionBase.KeysCollection Keys { get { return _sessionItems.Keys; } } | |
public override object this[string name] | |
{ | |
get { return _sessionItems[name]; } | |
set { _sessionItems[name] = value; } | |
} | |
public override object this[int index] | |
{ | |
get { return _sessionItems[index]; } | |
set { _sessionItems[index] = value; } | |
} | |
public override void Remove(string name) | |
{ | |
_sessionItems.Remove(name); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FakeIdentity : IIdentity | |
{ | |
private readonly string _name; | |
public FakeIdentity(string userName) | |
{ | |
_name = userName; | |
} | |
public string AuthenticationType | |
{ | |
get { throw new NotImplementedException(); } | |
} | |
public bool IsAuthenticated | |
{ | |
get { return !String.IsNullOrEmpty(_name); } | |
} | |
public string Name | |
{ | |
get { return _name; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FakePrincipal : IPrincipal | |
{ | |
private readonly IIdentity _identity; | |
private readonly string[] _roles; | |
public FakePrincipal(IIdentity identity, string[] roles) | |
{ | |
_identity = identity; | |
_roles = roles; | |
} | |
public IIdentity Identity | |
{ | |
get { return _identity; } | |
} | |
public bool IsInRole(string role) | |
{ | |
return _roles != null && _roles.Contains(role); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment