Skip to content

Instantly share code, notes, and snippets.

View feanz's full-sized avatar

Richard Forrest feanz

View GitHub Profile
@feanz
feanz / DeployEnvironment.ps1
Created January 2, 2014 09:25
Sitecore Website deployment scripts
<#
.SYNOPSIS
Quick deploy to various environments.
.DESCRIPTION
Calls deploy.ps1 with relevant arguments for repeatable deployments.
.PARAMETER deployTo
Specify the environment to deploy to. Currently supports "Dev-CMS" and "Dev-CD"
@feanz
feanz / ApiIntegrationFixture.cs
Created January 2, 2014 09:22
Web API Integration Tests
public class ApiIntegrationFixture : IDisposable
{
public ApiIntegrationFixture()
{
var config = new HttpConfiguration();
WebApiConfig.Register(config);
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
//remember to add all filters
config.Filters.Add(new ValidationActionFilter());
@feanz
feanz / AutoMoqControllerDataAttribute.cs
Created January 2, 2014 09:18
AutoMoqDataAttributeVersions Uses Autofixture and Xunit Data Theory attribute.
public class AutoMoqControllerDataAttribute : AutoDataAttribute
{
public AutoMoqControllerDataAttribute()
: base(new Fixture()
.Customize(new AutoMoqControllerCustomization()))
{
}
}
public class AutoMoqControllerCustomization : CompositeCustomization
@feanz
feanz / CacheAttribute.cs
Created January 2, 2014 09:14
Cache Infrastucture
[AttributeUsage(AttributeTargets.Class,AllowMultiple = false, Inherited = false)]
public class CacheAttribute : Attribute
{
/// <summary>
/// The name of the cache profile to be used
/// </summary>
public string ProfileName { get; set; }
/// <summary>
/// Lifespan of the response in the cache
@feanz
feanz / MethodInfoExtensions.cs
Created December 11, 2013 13:09
MethodInfoExtensions
public static class MethodInfoExtensions
{
/// <summary>
/// Will return the name of the action specified in the ActionNameAttribute for a method if it has an ActionNameAttribute.
/// Will return the name of the method otherwise.
/// </summary>
/// <param name="method"></param>
/// <returns></returns>
public static string ActionName(this MethodInfo method)
{
@feanz
feanz / AttributeExtensions.cs
Created December 11, 2013 13:08
AttributeExtensions
public static class AttributeExtensions
{
/// <summary>
/// Will return true if the attributeTarget is decorated with an attribute of type TAttribute.
/// Will return false if not.
/// </summary>
/// <typeparam name="TAttribute"></typeparam>
/// <param name="attributeTarget"></param>
/// <returns></returns>
public static bool IsDecoratedWith<TAttribute>(this ICustomAttributeProvider attributeTarget) where TAttribute : Attribute
@feanz
feanz / MockHttpContextFixture.cs
Created December 11, 2013 10:16
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;
@feanz
feanz / RouteTestingExtensions.cs
Last active December 31, 2015 00:29
Route Testing Extensions
/// <summary>
/// Used to simplify testing routes and restful testing routes
/// <example>
/// This tests that incoming PUT on resource is handled by the update method of the Banner controller
/// "~/banner/1"
/// .WithMethod(HttpVerbs.Put)
/// .ShouldMapTo<BannerController>(action => action.Update(1));
/// </example>
/// </summary>
public static class RouteTestingExtensions
@feanz
feanz / ValidateObjectAttribute.cs
Created December 9, 2013 23:25
Recursive Validation
public class ValidateObjectAttribute: ValidationAttribute {
protected override ValidationResult IsValid(object value, ValidationContext validationContext) {
var results = new List<ValidationResult>();
var context = new ValidationContext(value, null, null);
Validator.TryValidateObject(value, context, results, true);
if (results.Count != 0) {
var compositeResults = new CompositeValidationResult(String.Format("Validation for {0} failed!", validationContext.DisplayName));
results.ForEach(compositeResults.AddResult);
@feanz
feanz / PostcodeAttribute.cs
Created December 2, 2013 16:38
Postcode attribute
/// <summary>
/// Postcode validation attribute
/// Must be uppercase with a single optional space
/// </summary>
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public class PostcodeAttribute : DataTypeAttribute
{
private static readonly Regex _regex = new Regex(@"^(GIR ?0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]([0-9ABEHMNPRV-Y])?)|[0-9][A-HJKPS-UW]) ?[0-9][ABD-HJLNP-UW-Z]{2})$");
//if you need lowercase and multi space use this one