Skip to content

Instantly share code, notes, and snippets.

View trailmax's full-sized avatar

Max Vasilyev trailmax

View GitHub Profile
@trailmax
trailmax / gist:5548014
Created May 9, 2013 15:01
List of actions in a controller. Ignoring actions that come from base controller.
public virtual ActionResult SiteMap()
{
var assembly = Assembly.GetExecutingAssembly();
var controllers = assembly.GetTypes().Where(x => x != typeof(BaseController) && typeof(BaseController).IsAssignableFrom(x)).ToList();
controllers = controllers.Where(c => !c.Name.StartsWith("T4MVC")).ToList();
var controller = controllers.FirstOrDefault(c => c.FullName.Contains("HomePage"));
var actions = GetActionResults(controller);
@trailmax
trailmax / AzureStorageApi
Last active March 10, 2021 11:02
Unit testing the functionality to upload files to Azure Blob Storage via REST API. For blog post: http://tech.trailmax.info/2013/11/how-to-test-code-for-accessing-azure-storage-rest-api/ Please note, this implementation only supports file up to 64Mb in size. Anything larger and you need to chop files in pieces and upload them separately. There i…
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
namespace PackageUploader.Azure
{
public class AzureStorageApi
{
public void UploadFile(String fullFilePath, String blobSasUri, Dictionary<String, String> metadata = null)
@trailmax
trailmax / HttpSimulator.cs
Created November 21, 2013 23:03
HttpSimulator code from Phil Haak (http://haacked.com/archive/2007/06/19/unit-tests-web-code-without-a-web-server-using-httpsimulator.aspx) This version is StyleCop-ed. Also backing-up in case that page dies. Linked to my blog-post
using System;
using System.Collections.Specialized;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Configuration;
using System.Web.Hosting;
using System.Web.SessionState;
<?xml version="1.0" encoding="utf-8"?>
<!--
This file is used by the publish/package process of your Web project. You can customize the behavior of this process
by editing this MSBuild file. In order to learn more about this please visit http://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<PackagePath Condition=" '$(PackagePath)'=='' ">website</PackagePath>
<EnableAddReplaceToUpdatePacakgePath Condition=" '$(EnableAddReplaceToUpdatePacakgePath)'=='' ">true</EnableAddReplaceToUpdatePacakgePath>
@trailmax
trailmax / AzureStorageApi
Created January 9, 2014 02:43
Uploading large files to Azure Blob Storage via REST API. Code for blog post http://tech.trailmax.info/2014/01/uploading-large-files-to-azure-blob-storage-through-rest-api/
public class AzureStorageApi
{
private const string AzureApiVersion = "2012-02-12";
private const int BlockSize = 4 * 1024 * 1024;
public void UploadFile(string fullFilePath, string blobSasUri, Dictionary<string, string> metadata = null, int timeout = 60)
{
var blocks = new List<String>();
var tasks = new List<Task>();
public class CreateLocationAddressCommandHandlerTests
{
private IFixture fixture;
[SetUp]
public void SetUp()
{
fixture = new Fixture().Customize(new AutofixtureOnboardCustomization())
.Customize(new IgnoreVirtualMembersCustomisation());
[Theory, AutoDomainData]
public void FrozenObjects_AreInjected_IntoSut([Frozen]ILocationAddressRepository repository,
CreateLocationAddressCommandHandler sut, CreateLocationAddressCommand command)
{
// Act
sut.Handle(command);
// Assert
repository.Received().Insert(Arg.Any<LocationAddress>());
repository.Received().Save();
@trailmax
trailmax / gist:9026408
Last active August 29, 2015 13:56
Verify that all appropriate controller actions have a sitemap/breadcrumb attribute. Rewritten in xUnit data Attribute.
// Issue with this approach is when there is nothing is wrong with the solution and no actions are in the bad, i.e. test should pass
// this test still fail with error saying "No data found".
public class ControllerActionsDataAttribute : DataAttribute
{
public override IEnumerable<object[]> GetData(MethodInfo methodUnderTest, Type[] parameterTypes)
{
var controllerTypes = SitemapControllersTests.GetControllerTypes();
var offendingActions = controllerTypes.Where(ct => !SitemapControllersTests.ExcludedControllers.Contains(ct))
.SelectMany(c => c.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
@trailmax
trailmax / RequreSecureConnectionFilter
Created February 22, 2014 21:50
RequreSecureConnectionFilter implementation and unit tests
using System;
using System.Web.Mvc;
public class RequreSecureConnectionFilter : RequireHttpsAttribute
{
public override void OnAuthorization(AuthorizationContext filterContext)
{
if (filterContext == null)
{
@trailmax
trailmax / web.Release.config
Created February 22, 2014 22:33
Securing your cookies and STS header in web.config transformation
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
<httpCookies httpOnlyCookies="true" requireSSL="true" lockItem="true" xdt:Transform="Replace" />
<authentication mode="Forms">
<forms loginUrl="~/Logon/LogOn" timeout="2880" requireSSL="true" xdt:Transform="Replace"/>
</authentication>
</system.web>