Skip to content
This repository has been archived by the owner on Jan 24, 2021. It is now read-only.

Case sensitivity options #299

Merged
merged 2 commits into from
Sep 30, 2011
Merged
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
128 changes: 128 additions & 0 deletions src/Nancy.Testing.Tests/CaseSensitivityFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
namespace Nancy.Testing.Tests
{
using Xunit;
using Nancy.Tests;
using Nancy.ModelBinding;

public class CaseSensitivityFixture
{
private Browser browser;

public class MainModule : NancyModule
{
public MainModule()
{
Get["/"] = _ =>
{
string name = Request.Query.animal.HasValue ? Request.Query.animal : "";
return name;
};

Get["/{ANIMAL}"] = args =>
{
string name = args.animal.HasValue ? args.animal : "";
return name;
};

Get["/animal"] = _ =>
{
Animal animal = this.Bind<Animal>();
return animal.Type;
};
}
}

public class Animal
{
public string Type { get; set; }
}

public CaseSensitivityFixture()
{
var bootstrapper = new ConfigurableBootstrapper(with =>
{
with.Module<MainModule>();
});

this.browser = new Browser(bootstrapper);
}

[Fact]
public void Should_pull_query_parameter_with_different_case()
{
StaticConfiguration.CaseSensitive = false;
string animal = "dog";
var response = browser.Get("/", with =>
{
with.Query("ANIMAL", animal);
});

response.Body.AsString().ShouldEqual(animal);
}

[Fact]
public void Should_not_pull_query_parameter_with_different_case_when_sensitivity_is_on()
{
StaticConfiguration.CaseSensitive = true;
string animal = "dog";
var response = browser.Get("/", with =>
{
with.Query("ANIMAL", animal);
});

response.Body.AsString().ShouldEqual("");
}

[Fact]
public void Should_pull_parameter_with_different_case()
{
StaticConfiguration.CaseSensitive = false;
string animal = "dog";
var response = browser.Get("/dog", with =>
{
});

response.Body.AsString().ShouldEqual(animal);
}

[Fact]
public void Should_not_pull_parameter_with_different_case_when_sensitivity_is_on()
{
StaticConfiguration.CaseSensitive = true;
string animal = "dog";
var response = browser.Get("/dog", with =>
{
});

response.Body.AsString().ShouldEqual("");
}

[Fact]
public void Should_bind_with_different_case()
{
StaticConfiguration.CaseSensitive = false;
string animal = "dog";
var response = browser.Get("/animal", with =>
{
with.Query("TYPE", animal);
});

response.Body.AsString().ShouldEqual(animal);
}

[Fact]
public void Should_not_bind_with_different_case_when_sensitivity_is_on()
{
StaticConfiguration.CaseSensitive = true;
string animal = "dog";
var response = browser.Get("/animal", with =>
{
with.Query("TYPE", animal);
});

response.Body.AsString().ShouldEqual("");
}


}
}
1 change: 1 addition & 0 deletions src/Nancy.Testing.Tests/Nancy.Testing.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
<Compile Include="AndConnectorTests.cs" />
<Compile Include="BrowserResponseBodyWrapperExtensionsFixture.cs" />
<Compile Include="BrowserResponseBodyWrapperFixture.cs" />
<Compile Include="CaseSensitivityFixture.cs" />
<Compile Include="ConfigurableBootstrapperFixture.cs" />
<Compile Include="ContextExtensionsTests.cs" />
<Compile Include="DocumentWrapperTests.cs" />
Expand Down
3 changes: 2 additions & 1 deletion src/Nancy/DynamicDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

public class DynamicDictionary : DynamicObject, IEquatable<DynamicDictionary>, IHideObjectMembers
{
private readonly Dictionary<string, object> dictionary = new Dictionary<string, object>();
private readonly Dictionary<string, object> dictionary =
new Dictionary<string, object>(StaticConfiguration.CaseSensitive ? StringComparer.InvariantCulture : StringComparer.InvariantCultureIgnoreCase);

/// <summary>
/// Returns an empty dynamic dictionary.
Expand Down
6 changes: 4 additions & 2 deletions src/Nancy/Extensions/CollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace Nancy.Extensions
{
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
Expand All @@ -26,9 +27,10 @@ public static NameValueCollection ToNameValueCollection(this IDictionary<string,
return collection;
}

public static IDictionary<TKey, TValue> Merge<TKey, TValue>(this IEnumerable<IDictionary<TKey, TValue>> dictionaries)
public static IDictionary<string, string> Merge(this IEnumerable<IDictionary<string, string>> dictionaries)
{
var output = new Dictionary<TKey, TValue>();
var output =
new Dictionary<string, string>(StaticConfiguration.CaseSensitive ? StringComparer.InvariantCulture : StringComparer.InvariantCultureIgnoreCase);

foreach (var dictionary in dictionaries.Where(d => d != null))
{
Expand Down
10 changes: 10 additions & 0 deletions src/Nancy/StaticConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,15 @@ private static bool GetDebugMode()
return false;
}
}

private static bool caseSensitive = false;
/// <summary>
/// Gets or sets a value indicating whether or not to enable case sensitivity in query, parameters (DynamicDictionary) and model binding. Enable this to conform with RFC3986.
/// </summary>
public static bool CaseSensitive
{
get { return caseSensitive; }
set { caseSensitive = value; }
}
}
}