Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed some NullReferenceException, added an alternative way of configuring the objects and added fluent configurators. #9

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,39 @@ public void User_can_be_authenticated_with_convenience_method()
}
```

You can also set any virtual property from one of the `Http*` classes, even those that aren't explicitly settable. e.g., for `HttpContext`:
All properties for the Request/HttpContext/Response can be set using the `Control` property, even those that normally are read only.

```c#
var context = new FakeHttpContext();
var request = new FakeHttpRequest();
var uri = new Uri("http://www.google.com/");

//set the UrlReferrer
context.Set(ctx => ctx.UrlReferrer, uri);
request.Control.UrlReferrer = uri;

//get the UrlReferrer
Console.WriteLine(context.UrlReferrer); //<http://www.google.com/>
Console.WriteLine(request.UrlReferrer); //<http://www.google.com/>
```

You can also use our fluent builders:

```csharp
// simple context
var httpContext = builder
.Post("http://localhost/?A=1")
.WithForm(new { FirstName = "Jonas", LastName = "Gauffin", UserName = "jgauffin" })
.RespondWith(403)
.Build();

// configuring the session
var httpContext = builder
.UsingSession(new {UserId = 10})
.Build();

// setting principal
var principal ? new GenericPrincipal(new GenericIdentity("Arne"), new []{"Admin"});
var httpContext = builder
.UsePrincipal(principal)
.Build();
```

This means no more `NotImplementedException`s in your tests.
Expand Down
73 changes: 73 additions & 0 deletions src/Web/FakeHttpApplication.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using System.Web;

namespace FakeN.Web
{
public class FakeHttpApplication : HttpApplication
{
/// <summary>
/// Used to override <c>GetOutputCacheProviderName</c>
/// </summary>
public Func<HttpContext, string> GetOutputCacheProviderNameCallback;

/// <summary>
/// Used to ovveride <c>GetVaryByCustomString</c>.
/// </summary>
public Func<HttpContext, string, string> GetVaryByCustomStringCallback;

public FakeHttpApplication(HttpApplication assignedApplication)
{
AssignedApplication = assignedApplication;
}

public FakeHttpApplication()

{
}

public HttpApplication AssignedApplication { get; set; }

/// <summary>
/// Gets the name of the default output-cache provider that is configured for a Web site.
/// </summary>
/// <returns>
/// The name of the default provider.
/// </returns>
/// <param name="context">
/// An <see cref="T:System.Web.HttpContext" /> that provides references to intrinsic server objects
/// that are used to service HTTP requests.
/// </param>
/// <exception cref="T:System.Configuration.Provider.ProviderException">
/// <paramref name="context" /> is null or is an empty
/// string.
/// </exception>
public override string GetOutputCacheProviderName(HttpContext context)
{
if (GetOutputCacheProviderNameCallback != null)
return GetOutputCacheProviderNameCallback(context);

return base.GetOutputCacheProviderName(context);
}

/// <summary>
/// Provides an application-wide implementation of the
/// <see cref="P:System.Web.UI.PartialCachingAttribute.VaryByCustom" /> property.
/// </summary>
/// <returns>
/// If the value of the <paramref name="custom" /> parameter is "browser", the browser's
/// <see cref="P:System.Web.Configuration.HttpCapabilitiesBase.Type" />; otherwise, null.
/// </returns>
/// <param name="context">
/// An <see cref="T:System.Web.HttpContext" /> object that contains information about the current Web
/// request.
/// </param>
/// <param name="custom">The custom string that specifies which cached response is used to respond to the current request. </param>
public override string GetVaryByCustomString(HttpContext context, string custom)
{
if (GetVaryByCustomStringCallback != null)
return GetVaryByCustomStringCallback(context, custom);

return base.GetVaryByCustomString(context, custom);
}
}
}
220 changes: 142 additions & 78 deletions src/Web/FakeHttpContext.cs
Original file line number Diff line number Diff line change
@@ -1,88 +1,152 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Security.Principal;
using System.Web;
using System.Web.Caching;
using System.Web.Profile;

namespace FakeN.Web
{
public class FakeHttpContext : HttpContextBase
{
private readonly HttpRequestBase request;
private readonly HttpResponseBase response;
private readonly HttpSessionStateBase session;
private readonly IDictionary items;

private TraceContext trace;
private DateTime timestamp;
private bool skipAuthorization;
private HttpServerUtilityBase server;
private ProfileBase profile;
private IHttpHandler previousHandler;
private bool isPostNotification;
private bool isDebuggingEnabled;
private bool isCustomErrorEnabled;
private IHttpHandler handler;
private Exception error;
private RequestNotification currentNotification;
private IHttpHandler currentHandler;
private Cache cache;
private HttpApplication applicationInstance;
private HttpApplicationStateBase application;
private Exception[] allErrors;

public FakeHttpContext(
HttpRequestBase request = null,
HttpResponseBase response = null,
HttpSessionStateBase session = null)
{
this.request = request ?? new FakeHttpRequest();
this.response = response ?? new FakeHttpResponse();
this.session = session ?? new FakeHttpSessionState();
items = new Dictionary<object, object>();
User = new GenericPrincipal(new MutableIdentity(), new string[] { });
}

public override HttpRequestBase Request
{
get { return request; }
}

public override HttpResponseBase Response
{
get { return response; }
}

public override HttpSessionStateBase Session
{
get { return session; }
}

public override IDictionary Items
{
get { return items; }
}

public override IPrincipal User { get; set; }

public override TraceContext Trace { get { return trace; } }
public override DateTime Timestamp { get { return timestamp; } }
public override bool SkipAuthorization { get { return skipAuthorization; } set { skipAuthorization = value; } }
public override HttpServerUtilityBase Server { get { return server; } }
public override ProfileBase Profile { get { return profile; } }
public override IHttpHandler PreviousHandler { get { return previousHandler; } }
public override bool IsPostNotification { get { return isPostNotification; } }
public override bool IsDebuggingEnabled { get { return isDebuggingEnabled; } }
public override bool IsCustomErrorEnabled { get { return isCustomErrorEnabled; } }
public override IHttpHandler Handler { get { return handler; } set { handler = value; } }
public override Exception Error { get { return error; } }
public override RequestNotification CurrentNotification { get { return currentNotification; } }
public override IHttpHandler CurrentHandler { get { return currentHandler; } }
public override Cache Cache { get { return cache; } }
public override HttpApplication ApplicationInstance { get { return applicationInstance; } set { applicationInstance = value; } }
public override HttpApplicationStateBase Application { get { return application; } }
public override Exception[] AllErrors { get { return allErrors; } }
}
public class FakeHttpContext : HttpContextBase
{
private readonly ProfileBase _profile;
private readonly HttpRequestBase request;
private readonly HttpResponseBase response;
private readonly HttpSessionStateBase session;

public FakeHttpContext(
HttpRequestBase request = null,
HttpResponseBase response = null,
HttpSessionStateBase session = null)
{
this.request = request;
this.response = response;
this.session = session;
_profile = new ProfileBase();
Control = new FakeHttpContextControl(this);
}

public FakeHttpContext(FakeHttpContextControl control)
{
Control = control;
_profile = new ProfileBase();
}

public FakeHttpContext()
{
Control = new FakeHttpContextControl();
_profile = new ProfileBase();
}

/// <summary>
/// Used to control this context.
/// </summary>
public FakeHttpContextControl Control { get; private set; }

public override HttpRequestBase Request
{
get { return request ?? Control.Request; }
}

public override HttpResponseBase Response
{
get { return response ?? Control.Response; }
}

public override HttpSessionStateBase Session
{
get { return session ?? Control.Session; }
}

public override IDictionary Items
{
get { return Control.Items; }
}

public override IPrincipal User { get { return Control.User; } set { Control.User = value; } }

public override TraceContext Trace
{
get { return Control.TraceContext; }
}

public override DateTime Timestamp
{
get { return Control.TimeStamp; }
}

public override bool SkipAuthorization { get; set; }

public override HttpServerUtilityBase Server
{
get { return Control.Server; }
}

public override ProfileBase Profile
{
get { return _profile; }
}

public override IHttpHandler PreviousHandler
{
get { return Control.PreviousHandler; }
}

public override bool IsPostNotification
{
get { return Control.IsPostNotification; }
}

public override bool IsDebuggingEnabled
{
get { return Control.IsDebuggingEnabled; }
}

public override bool IsCustomErrorEnabled
{
get { return Control.IsCustomErrorsEnabled; }
}

public override IHttpHandler Handler
{
get { return Control.Handler; }
set { Control.Handler = value; }
}

public override Exception Error
{
get { return Control.Error; }
}

public override RequestNotification CurrentNotification
{
get { return Control.CurrentNotification; }
}

public override IHttpHandler CurrentHandler
{
get { return Control.CurrentHandler; }
}

public override Cache Cache
{
get { return Control.Cache; }
}

public override HttpApplication ApplicationInstance
{
get { return Control.ApplicationInstance; }
set { Control.ApplicationInstance = new FakeHttpApplication(value); }
}

public override HttpApplicationStateBase Application
{
get { return Control.Application; }
}

public override Exception[] AllErrors
{
get { return Control.AllErrors.ToArray(); }
}
}
}
Loading