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

Commit

Permalink
Updated code to consume non-static JsonConfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
thecodejunkie committed Nov 26, 2015
1 parent dc2896c commit 793104f
Show file tree
Hide file tree
Showing 33 changed files with 545 additions and 278 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace Nancy.Testing.Tests
{
using System.IO;

using FakeItEasy;
using Nancy.Tests;

using Xunit;
Expand All @@ -20,7 +20,7 @@ public void Should_convert_to_string()
writer.Write("This is the content");
writer.Flush();
}
});
}, A.Dummy<BrowserContext>());

// When
var result = body.AsString();
Expand Down
8 changes: 4 additions & 4 deletions src/Nancy.Testing.Tests/BrowserResponseBodyWrapperFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.IO;
using System.Linq;
using System.Text;

using FakeItEasy;
using Nancy.Tests;

using Xunit;
Expand All @@ -22,7 +22,7 @@ public void Should_contain_response_body()
writer.Write("This is the content");
writer.Flush();
}
});
}, A.Dummy<BrowserContext>());

var content = Encoding.ASCII.GetBytes("This is the content");

Expand All @@ -45,7 +45,7 @@ public void Should_return_querywrapper_for_css_selector_match()
writer.Write("<div>Outer and <div id='bar'>inner</div></div>");
writer.Flush();
}
});
}, A.Dummy<BrowserContext>());

// When
var result = body["#bar"];
Expand All @@ -58,4 +58,4 @@ public void Should_return_querywrapper_for_css_selector_match()
#endif
}
}
}
}
14 changes: 10 additions & 4 deletions src/Nancy.Testing.Tests/BrowserResponseExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
namespace Nancy.Testing.Tests
{
using System.Xml;

using FakeItEasy;

using Xunit;

public class BrowserResponseExtensionsTests
Expand All @@ -13,19 +11,27 @@ public class BrowserResponseExtensionsTests
[Fact]
public void Should_create_xdocument_from_xml_body()
{
// Given
var context = new NancyContext() { Response = "<tag />" };
sut = new BrowserResponse(context, A.Fake<Browser>());
sut = new BrowserResponse(context, A.Fake<Browser>(), A.Dummy<BrowserContext>());

// When
var bodyAsXml = sut.BodyAsXml();

// Then
Assert.NotNull(bodyAsXml.Element("tag"));
}

[Fact]
public void Should_fail_to_create_xdocument_from_non_xml_body()
{
// Given
var context = new NancyContext() { Response = "hello" };
sut = new BrowserResponse(context, A.Fake<Browser>());

// When
sut = new BrowserResponse(context, A.Fake<Browser>(), A.Dummy<BrowserContext>());

// Then
Assert.Throws<XmlException>(() => sut.BodyAsXml());
}
}
Expand Down
17 changes: 15 additions & 2 deletions src/Nancy.Testing.Tests/ContextExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace Nancy.Testing.Tests
using System.IO;
using System.Text;
using Nancy.Configuration;
using Nancy.Json;
using Nancy.Responses;
using Nancy.Tests;
using Nancy.Xml;
Expand Down Expand Up @@ -70,7 +71,8 @@ public void Should_use_jsonresponse_from_context_if_it_is_present()
public void Should_create_new_wrapper_from_json_response_if_not_already_present()
{
// Given
var response = new JsonResponse<Model>(new Model() { Dummy = "Data" }, new DefaultJsonSerializer());
var environment = GetTestingEnvironment();
var response = new JsonResponse<Model>(new Model() { Dummy = "Data" }, new DefaultJsonSerializer(environment), environment);
var context = new NancyContext() { Response = response };

// When
Expand Down Expand Up @@ -114,7 +116,8 @@ public void Should_create_new_wrapper_from_xml_response_if_not_already_present()
public void Should_fail_to_return_xml_body_on_non_xml_response()
{
// Given
var response = new JsonResponse<Model>(new Model() { Dummy = "Data" }, new DefaultJsonSerializer());
var environment = GetTestingEnvironment();
var response = new JsonResponse<Model>(new Model() { Dummy = "Data" }, new DefaultJsonSerializer(environment), environment);
var context = new NancyContext() { Response = response };

// When
Expand All @@ -124,6 +127,16 @@ public void Should_fail_to_return_xml_body_on_non_xml_response()
result.ShouldNotBeNull();
}

private static INancyEnvironment GetTestingEnvironment()
{
var envionment =
new DefaultNancyEnvironment();

envionment.AddValue(JsonConfiguration.Default);

return envionment;
}

public class Model
{
public string Dummy { get; set; }
Expand Down
20 changes: 14 additions & 6 deletions src/Nancy.Testing/Browser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,13 @@ public BrowserResponse Put(Url url, Action<BrowserContext> browserContext = null
/// <returns>An <see cref="BrowserResponse"/> instance of the executed request.</returns>
public BrowserResponse HandleRequest(string method, Url url, Action<BrowserContext> browserContext)
{
var browserContextValues =
BuildBrowserContextValues(browserContext ?? (with => { }));

var request =
this.CreateRequest(method, url, browserContext ?? (with => {}));
CreateRequest(method, url, browserContextValues);

var response = new BrowserResponse(this.engine.HandleRequest(request), this);
var response = new BrowserResponse(this.engine.HandleRequest(request), this, (BrowserContext)browserContextValues);

this.CaptureCookies(response);

Expand Down Expand Up @@ -296,7 +299,7 @@ private static void BuildRequestBody(IBrowserContextValues contextValues)
contextValues.Body = new MemoryStream(bodyBytes);
}

private Request CreateRequest(string method, Url url, Action<BrowserContext> browserContext)
private IBrowserContextValues BuildBrowserContextValues(Action<BrowserContext> browserContext)
{
var context =
new BrowserContext(this.environment);
Expand All @@ -314,14 +317,19 @@ private Request CreateRequest(string method, Url url, Action<BrowserContext> bro
contextValues.Headers.Add("user-agent", new[] { "Nancy.Testing.Browser" });
}

return contextValues;
}

private static Request CreateRequest(string method, Url url, IBrowserContextValues contextValues)
{
BuildRequestBody(contextValues);

var requestStream =
RequestStream.FromStream(contextValues.Body, 0, true);

var certBytes = (contextValues.ClientCertificate == null) ?
new byte[] { } :
contextValues.ClientCertificate.GetRawCertData();
var certBytes = (contextValues.ClientCertificate == null)
? new byte[] { }
: contextValues.ClientCertificate.GetRawCertData();

var requestUrl = url;
requestUrl.Scheme = string.IsNullOrWhiteSpace(contextValues.Protocol) ? requestUrl.Scheme : contextValues.Protocol;
Expand Down
2 changes: 1 addition & 1 deletion src/Nancy.Testing/BrowserContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static void JsonBody<TModel>(this BrowserContext browserContext, TModel m
{
if (serializer == null)
{
serializer = new DefaultJsonSerializer();
serializer = new DefaultJsonSerializer(browserContext.Environment);
}

var contextValues =
Expand Down
11 changes: 6 additions & 5 deletions src/Nancy.Testing/BrowserResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
{
using System;
using System.Collections.Generic;

using Nancy.Cookies;

/// <summary>
Expand All @@ -11,16 +10,17 @@
public class BrowserResponse
{
private readonly Browser hostBrowser;

private readonly BrowserContext browserContext;
private BrowserResponseBodyWrapper body;

/// <summary>
/// Initializes a new instance of the <see cref="BrowserResponse"/> class.
/// </summary>
/// <param name="context">The <see cref="NancyContext"/> that <see cref="Browser"/> was invoked with.</param>
/// <param name="hostBrowser">Host browser object</param>
/// <param name="browserContext">An <see cref="BrowserContext"/> instance.</param>
/// <exception cref="ArgumentNullException">The value of the <paramref name="context"/> parameter was <see langword="null"/>.</exception>
public BrowserResponse(NancyContext context, Browser hostBrowser)
public BrowserResponse(NancyContext context, Browser hostBrowser, BrowserContext browserContext)
{
if (context == null)
{
Expand All @@ -33,6 +33,7 @@ public BrowserResponse(NancyContext context, Browser hostBrowser)
}

this.hostBrowser = hostBrowser;
this.browserContext = browserContext;

this.Context = context;
}
Expand All @@ -45,7 +46,7 @@ public BrowserResponseBodyWrapper Body
{
get
{
return this.body ?? (this.body = new BrowserResponseBodyWrapper(this.Context.Response));
return this.body ?? (this.body = new BrowserResponseBodyWrapper(this.Context.Response, this.browserContext));
}
}

Expand Down Expand Up @@ -110,4 +111,4 @@ public Browser Then
}
}
}
}
}
10 changes: 9 additions & 1 deletion src/Nancy.Testing/BrowserResponseBodyWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ public class BrowserResponseBodyWrapper : IEnumerable<byte>
/// Initializes a new instance of the <see cref="BrowserResponseBodyWrapper"/> class.
/// </summary>
/// <param name="response">The <see cref="Response"/> to wrap.</param>
public BrowserResponseBodyWrapper(Response response)
/// <param name="browserContext">The <see cref="BrowserContext"/> of the request that generated the response.</param>
public BrowserResponseBodyWrapper(Response response, BrowserContext browserContext)
{
this.BrowserContext = browserContext;
var contentStream = GetContentStream(response);

this.responseBytes = contentStream.ToArray();
Expand All @@ -35,6 +37,12 @@ public string ContentType
get { return this.contentType; }
}

/// <summary>
/// Gets the <see cref="BrowserContext"/> of the request that generated the response.
/// </summary>
/// <value>A <see cref="BrowserContext"/> intance.</value>
public BrowserContext BrowserContext { get; private set; }

private static MemoryStream GetContentStream(Response response)
{
var contentsStream = new MemoryStream();
Expand Down
2 changes: 1 addition & 1 deletion src/Nancy.Testing/BrowserResponseBodyWrapperExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static XmlDocument AsXmlDocument(this BrowserResponseBodyWrapper bodyWrap
/// <value>A <typeparamref name="TModel"/> instance representation of the HTTP response body.</value>
public static TModel DeserializeJson<TModel>(this BrowserResponseBodyWrapper bodyWrapper)
{
var bodyDeserializer = new JsonBodyDeserializer();
var bodyDeserializer = new JsonBodyDeserializer(bodyWrapper.BrowserContext.Environment);

return bodyWrapper.Deserialize<TModel>(bodyDeserializer);
}
Expand Down
18 changes: 15 additions & 3 deletions src/Nancy.Tests/Unit/DefaultSerializerFactoryFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Collections.Generic;
using System.IO;
using System.Threading;
using Nancy.Configuration;
using Nancy.Json;
using Nancy.Responses;
using Nancy.Responses.Negotiation;
using Xunit;
Expand Down Expand Up @@ -100,7 +102,7 @@ public void Should_not_include_default_serializers_when_counting_matches()
var serializers = new ISerializer[]
{
new TestableSerializer("application/json"),
new DefaultJsonSerializer()
new DefaultJsonSerializer(GetTestingEnvironment())
};

this.serializerFactory = new DefaultSerializerFactory(serializers);
Expand All @@ -119,7 +121,7 @@ public void Should_prioritize_non_default_serializer_match()

var serializers = new ISerializer[]
{
new DefaultJsonSerializer(),
new DefaultJsonSerializer(GetTestingEnvironment()),
expectedSerializer
};

Expand All @@ -137,7 +139,7 @@ public void Should_return_default_serializer_if_no_other_match_could_be_made()
{
// Given
var expectedSerializer =
new DefaultJsonSerializer();
new DefaultJsonSerializer(GetTestingEnvironment());

var serializers = new ISerializer[]
{
Expand All @@ -155,6 +157,16 @@ public void Should_return_default_serializer_if_no_other_match_could_be_made()
result.ShouldBeSameAs(expectedSerializer);
}

private static INancyEnvironment GetTestingEnvironment()
{
var envionment =
new DefaultNancyEnvironment();

envionment.AddValue(JsonConfiguration.Default);

return envionment;
}

internal class ExceptionThrowingSerializer : ISerializer
{
public bool CanSerialize(MediaRange mediaRange)
Expand Down
Loading

0 comments on commit 793104f

Please sign in to comment.