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

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into non_buffered_requ…
Browse files Browse the repository at this point in the history
…ests
  • Loading branch information
Adam Hathcock committed May 26, 2015
2 parents c7c11f6 + 188bc90 commit d67d8b1
Show file tree
Hide file tree
Showing 18 changed files with 252 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
using Bootstrapper;

public class DemoBoostrapper : DefaultNancyBootstrapper
public class DemoBootstrapper : DefaultNancyBootstrapper
{
protected override NancyInternalConfiguration InternalConfiguration
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="CustomResourceAssemblyProvider.cs" />
<Compile Include="DemoBoostrapper.cs" />
<Compile Include="DemoBootstrapper.cs" />
<Compile Include="Modules\HomeModule.cs" />
<Compile Include="Resources\Text.Designer.cs">
<AutoGen>True</AutoGen>
Expand Down
1 change: 1 addition & 0 deletions src/Nancy.Tests/Nancy.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@
<Compile Include="Unit\Json\TestData.cs" />
<Compile Include="Unit\Json\TestPrimitiveConverter.cs" />
<Compile Include="Unit\Json\TestPrimitiveConverterType.cs" />
<Compile Include="Unit\Json\TypeWithTuple.cs" />
<Compile Include="Unit\MimeTypesFixture.cs" />
<Compile Include="Unit\ModelBinding\BindingMemberInfoFixture.cs" />
<Compile Include="Unit\ModelBinding\ModelBindingExceptionFixture.cs" />
Expand Down
309 changes: 178 additions & 131 deletions src/Nancy.Tests/Unit/Json/JavaScriptSerializerFixture.cs
Original file line number Diff line number Diff line change
@@ -1,134 +1,181 @@
namespace Nancy.Tests.Unit.Json
{
using System;
using System.Collections.Generic;
using System.IO;
using FakeItEasy;
using Nancy.IO;
using Nancy.Json;
using Xunit;
using Xunit.Extensions;
using Xunit.Sdk;

public class JavaScriptSerializerFixture
{
[Fact]
public void Should_register_converters_when_asked()
{
// Given
JsonSettings.Converters.Add(new TestConverter());
JsonSettings.PrimitiveConverters.Add(new TestPrimitiveConverter());

var defaultSerializer = new JavaScriptSerializer();

// When
var serializer = new JavaScriptSerializer(
registerConverters: true,
resolver: null,
maxJsonLength: defaultSerializer.MaxJsonLength,
recursionLimit: defaultSerializer.RecursionLimit,
retainCasing: defaultSerializer.RetainCasing,
iso8601DateFormat: defaultSerializer.ISO8601DateFormat);

var data =
new TestData()
{
ConverterData =
new TestConverterType()
{
Data = 42,
},

PrimitiveConverterData =
new TestPrimitiveConverterType()
{
Data = 1701,
},
};

const string ExpectedJSON = @"{""converterData"":{""dataValue"":42},""primitiveConverterData"":1701}";

// Then
serializer.Serialize(data).ShouldEqual(ExpectedJSON);

serializer.Deserialize<TestData>(ExpectedJSON).ShouldEqual(data);
}

[Fact]
public void Should_not_register_converters_when_not_asked()
{
// Given
JsonSettings.Converters.Add(new TestConverter());
JsonSettings.PrimitiveConverters.Add(new TestPrimitiveConverter());

var defaultSerializer = new JavaScriptSerializer();

// When
var serializer = new JavaScriptSerializer(
registerConverters: false,
resolver: null,
maxJsonLength: defaultSerializer.MaxJsonLength,
recursionLimit: defaultSerializer.RecursionLimit,
retainCasing: defaultSerializer.RetainCasing,
iso8601DateFormat: defaultSerializer.ISO8601DateFormat);

var data =
new TestData()
{
ConverterData =
new TestConverterType()
{
Data = 42,
},

PrimitiveConverterData =
new TestPrimitiveConverterType()
{
Data = 1701,
},
};

const string ExpectedJSON = @"{""converterData"":{""data"":42},""primitiveConverterData"":{""data"":1701}}";

// Then
serializer.Serialize(data).ShouldEqual(ExpectedJSON);

serializer.Deserialize<TestData>(ExpectedJSON).ShouldEqual(data);
}

[Fact]
public void Should_use_primitive_converter_when_available()
{
// When
var serializer = new JavaScriptSerializer();

serializer.RegisterConverters(new JavaScriptPrimitiveConverter[] { new TestPrimitiveConverter() });

// Then
serializer.Serialize(new TestPrimitiveConverterType() { Data = 12345 }).ShouldEqual("12345");

serializer.Deserialize<TestPrimitiveConverterType>("12345").ShouldEqual(new TestPrimitiveConverterType() { Data = 12345 });
}

[Fact]
public void Should_not_use_primitive_converter_for_wrong_type()
{
// When
var serializer = new JavaScriptSerializer();

serializer.RegisterConverters(new JavaScriptPrimitiveConverter[] { new TestPrimitiveConverter() });

// Then
serializer.Serialize(new TestConverterType() { Data = 12345 }).ShouldEqual(@"{""data"":12345}");

serializer.Deserialize<TestConverterType>(@"{""data"":12345}").ShouldEqual(new TestConverterType() { Data = 12345 });

try
{
serializer.Deserialize<TestConverterType>("12345");
throw new ThrowsException(typeof(InvalidCastException));
}
catch { }
}
}
using System;
using System.Collections.Generic;
using System.IO;
using FakeItEasy;
using Nancy.IO;
using Nancy.Json;
using Nancy.Json.Converters;

using Xunit;
using Xunit.Extensions;
using Xunit.Sdk;

public class JavaScriptSerializerFixture
{
[Fact]
public void Should_register_converters_when_asked()
{
// Given
JsonSettings.Converters.Add(new TestConverter());
JsonSettings.PrimitiveConverters.Add(new TestPrimitiveConverter());

var defaultSerializer = new JavaScriptSerializer();

// When
var serializer = new JavaScriptSerializer(
registerConverters: true,
resolver: null,
maxJsonLength: defaultSerializer.MaxJsonLength,
recursionLimit: defaultSerializer.RecursionLimit,
retainCasing: defaultSerializer.RetainCasing,
iso8601DateFormat: defaultSerializer.ISO8601DateFormat);

var data =
new TestData()
{
ConverterData =
new TestConverterType()
{
Data = 42,
},

PrimitiveConverterData =
new TestPrimitiveConverterType()
{
Data = 1701,
},
};

const string ExpectedJSON = @"{""converterData"":{""dataValue"":42},""primitiveConverterData"":1701}";

// Then
serializer.Serialize(data).ShouldEqual(ExpectedJSON);

serializer.Deserialize<TestData>(ExpectedJSON).ShouldEqual(data);
}

[Fact]
public void Should_not_register_converters_when_not_asked()
{
// Given
JsonSettings.Converters.Add(new TestConverter());
JsonSettings.PrimitiveConverters.Add(new TestPrimitiveConverter());

var defaultSerializer = new JavaScriptSerializer();

// When
var serializer = new JavaScriptSerializer(
registerConverters: false,
resolver: null,
maxJsonLength: defaultSerializer.MaxJsonLength,
recursionLimit: defaultSerializer.RecursionLimit,
retainCasing: defaultSerializer.RetainCasing,
iso8601DateFormat: defaultSerializer.ISO8601DateFormat);

var data =
new TestData()
{
ConverterData =
new TestConverterType()
{
Data = 42,
},

PrimitiveConverterData =
new TestPrimitiveConverterType()
{
Data = 1701,
},
};

const string ExpectedJSON = @"{""converterData"":{""data"":42},""primitiveConverterData"":{""data"":1701}}";

// Then
serializer.Serialize(data).ShouldEqual(ExpectedJSON);

serializer.Deserialize<TestData>(ExpectedJSON).ShouldEqual(data);
}

[Fact]
public void Should_use_primitive_converter_when_available()
{
// When
var serializer = new JavaScriptSerializer();

serializer.RegisterConverters(new JavaScriptPrimitiveConverter[] { new TestPrimitiveConverter() });

// Then
serializer.Serialize(new TestPrimitiveConverterType() { Data = 12345 }).ShouldEqual("12345");

serializer.Deserialize<TestPrimitiveConverterType>("12345").ShouldEqual(new TestPrimitiveConverterType() { Data = 12345 });
}

[Fact]
public void Should_not_use_primitive_converter_for_wrong_type()
{
// When
var serializer = new JavaScriptSerializer();

serializer.RegisterConverters(new JavaScriptPrimitiveConverter[] { new TestPrimitiveConverter() });

// Then
serializer.Serialize(new TestConverterType() { Data = 12345 }).ShouldEqual(@"{""data"":12345}");

serializer.Deserialize<TestConverterType>(@"{""data"":12345}").ShouldEqual(new TestConverterType() { Data = 12345 });

try
{
serializer.Deserialize<TestConverterType>("12345");
throw new ThrowsException(typeof(InvalidCastException));
}
catch { }
}

[Fact]
public void Should_serialize_tuples()
{
var serializer = new JavaScriptSerializer();
serializer.RegisterConverters(new[] { new TupleConverter() });

var tuple = Tuple.Create(10, 11);
serializer.Serialize(tuple).ShouldEqual(@"{""item1"":10,""item2"":11}");
}

[Fact]
public void Should_deserialize_tuple()
{
var serializer = new JavaScriptSerializer();
serializer.RegisterConverters(new[] { new TupleConverter() });

string body = @"{""item1"":10,""item2"":11}";
Tuple<int, int> result = serializer.Deserialize<Tuple<int, int>>(body);
result.ToString().ShouldEqual("(10, 11)");
}

[Fact]
public void Should_deserialize_string_tuple()
{
var serializer = new JavaScriptSerializer();
serializer.RegisterConverters(new[] { new TupleConverter() });

string body = @"{""item1"":""Hello"",""item2"":""World"",""item3"":42}";
var result = serializer.Deserialize<Tuple<string, string, int>>(body);
result.ToString().ShouldEqual("(Hello, World, 42)");
}

[Fact]
public void Should_deserialize_type_with_tuples()
{
// When
var serializer = new JavaScriptSerializer();
serializer.RegisterConverters(new[] { new TupleConverter() });

// Then
var typeWithTuple = serializer.Deserialize<TypeWithTuple>(@"{""value"":{""item1"":10,""item2"":11}}");
typeWithTuple.Value.Item1.ShouldEqual(10);
typeWithTuple.Value.Item2.ShouldEqual(11);
}
}
}
9 changes: 9 additions & 0 deletions src/Nancy.Tests/Unit/Json/TypeWithTuple.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Nancy.Tests.Unit.Json
{
using System;

public class TypeWithTuple
{
public Tuple<int, int> Value { get; set; }
}
}
4 changes: 2 additions & 2 deletions src/Nancy.Tests/Unit/Responses/EmbeddedFileResponseFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void Should_contain_etag_in_response_header_if_embedded_resource_exists()
new EmbeddedFileResponse(this.GetType().Assembly, "Nancy.Tests", "Resources.Views.staticviewresource.html");

// Then
response.Headers["ETag"].ShouldEqual("\"5D6EFDFDB135DC90F16D57E05603DA1E\"");
response.Headers["ETag"].ShouldEqual("\"B9D9DC2B50ADFD0867749D4837C63556339080CE\"");
}

[Fact]
Expand All @@ -30,7 +30,7 @@ public void Should_contain_etag_in_response_header_if_embedded_resource_exists_w
response.Contents.Invoke(outputStream);

// Then
response.Headers["ETag"].ShouldEqual("\"5D6EFDFDB135DC90F16D57E05603DA1E\"");
response.Headers["ETag"].ShouldEqual("\"B9D9DC2B50ADFD0867749D4837C63556339080CE\"");
}

[Fact]
Expand Down
3 changes: 3 additions & 0 deletions src/Nancy/Bootstrapper/NancyBootstrapperBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ public void Initialise()

this.ConfigureApplicationContainer(this.ApplicationContainer);

// We need to call this to fix an issue with assemblies that are referenced by DI not being loaded
AppDomainAssemblyTypeScanner.UpdateTypes();

var typeRegistrations = this.InternalConfiguration.GetTypeRegistations()
.Concat(this.GetAdditionalTypes());

Expand Down
4 changes: 2 additions & 2 deletions src/Nancy/Bootstrapper/NancyBootstrapperLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

/// <summary>
/// Class for locating an INancyBootstrapper implementation.
///
///
/// Will search the app domain for a non-abstract one, and if it can't find one
/// it will use the default nancy one that uses TinyIoC.
/// </summary>
Expand Down Expand Up @@ -70,7 +70,7 @@ internal static bool TryFindMostDerivedType(List<Type> customBootstrappers, out
var set = new HashSet<Type>();
bootstrapper = null;

if (customBootstrappers.All(boostrapper => set.Add(boostrapper.BaseType)))
if (customBootstrappers.All(b => set.Add(b.BaseType)))
{
var except = customBootstrappers.Except(set).ToList();
bootstrapper = except.Count == 1 ? except[0] : null;
Expand Down
Loading

0 comments on commit d67d8b1

Please sign in to comment.