Skip to content

Instantly share code, notes, and snippets.

View thecodejunkie's full-sized avatar

Andreas Håkansson thecodejunkie

View GitHub Profile
@thecodejunkie
thecodejunkie / ResourceExplorer.cs
Created March 22, 2011 12:25
Finding the longest common prefix of all resources and returns their names with it stripped
public class ResourceExplorer
{
public static void Inspect(Assembly assembly, IEnumerable<string> supportedExtensions)
{
var resourceNames =
from resourceStreamName in assembly.GetManifestResourceNames()
from extension in supportedExtensions
where resourceStreamName.EndsWith(extension)
select resourceStreamName;
@thecodejunkie
thecodejunkie / gist:881542
Created March 22, 2011 16:44
Spike of Nancy test harness
public class BrowserFixture
{
private readonly Browser browser;
public BrowserFixture()
{
var bootstrapper =
new FakeDefaultNancyBootstrapper();
this.browser = new Browser(bootstrapper);
@thecodejunkie
thecodejunkie / gist:883061
Created March 23, 2011 13:00
Using a custom extension to the Nancy test harness to be able to add multipart/form-data encoded data into the request
[Fact]
public void Should_add_multipart_formdata_encoded_files_to_request_filestream()
{
// Given
var stream =
CreateFakeFileStream("This is the contents of a file");
var multipart = new BrowserContextMultipartFormData(x => {
x.AddFile("foo", "foo.txt", "text/plain", stream);
});
@thecodejunkie
thecodejunkie / FakeNancyBootstrapper.cs
Created March 27, 2011 12:56
Configuring a Nancy bootstrapper with an FI
public class test
{
public test()
{
var bootstrapper = new FakeNancyBootstrapper(with => {
with.Engine<NancyEngine>();
with.ContextFactory<DefaultNancyContextFactory>();
with.RoutePatternMatcher<DefaultRoutePatternMatcher>();
});
}
@thecodejunkie
thecodejunkie / FakeNancyBootstrapper.cs
Created March 28, 2011 19:57
Rewriting the FakeNancyBootstrapper from scratch
public class FakeNancyBootstrapper : INancyBootstrapper, IApplicationPipelines, INancyModuleCatalog
{
private TinyIoCContainer container;
private const string ContextKey = "DefaultNancyBootStrapperChildContainer";
private Dictionary<Type, object> configuredDefaults;
private Dictionary<Type, IEnumerable<Tuple<Type, string>>> configuredEnumerableDefaults;
public FakeNancyBootstrapper()
{
@thecodejunkie
thecodejunkie / gist:891866
Created March 29, 2011 05:51
Too tired to think of a good description =)
protected override void RegisterDefaults(TinyIoCContainer existingContainer, IEnumerable<TypeRegistration> typeRegistrations)
{
existingContainer.Register<INancyModuleCatalog>(this);
foreach (var typeRegistration in typeRegistrations)
{
this.Register(typeRegistration.RegistrationType, typeRegistration.ImplementationType);
}
}
// -- BEFORE --
Post["/create"] = x =>
{
var be = new BeerEvent()
{
Name = Request.Form.Name,
Location = Request.Form.Location,
EventDate = Request.Form.EventDate
};
@thecodejunkie
thecodejunkie / gist:898770
Created April 1, 2011 20:14
Blog - Hello World
public class MainModule : NancyModule
{
public MainModule()
{
Get["/"] = parameters => {
return "Hello World";
};
}
}
@thecodejunkie
thecodejunkie / gist:905401
Created April 6, 2011 09:47
Hack job for returning a custom error page in Nancy
public class MyBootstrapper : DefaultNancyBootstrapper
{
protected override void InitialiseInternal(TinyIoC.TinyIoCContainer container)
{
base.InitialiseInternal(container);
this.AfterRequest += ctx => {
if (ctx.Response.StatusCode.Equals(HttpStatusCode.NotFound))
{
ctx.Response = "This is a custom error page"; ;
@thecodejunkie
thecodejunkie / gist:973532
Created May 15, 2011 21:02
Playing around with some very raw, default, view discovery conventions for Nancy
return new List<Func<string, dynamic, string, string>>
{
(viewName, model, modulePath) => {
return string.Concat("/views/", viewName);
},
(viewName, model, modulePath) => {
return string.Concat("/views", modulePath, "/", viewName);
}
};