Skip to content

Commit

Permalink
fix passing exact media type to serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
tpluscode committed Apr 1, 2016
1 parent 4b4b526 commit a26910c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
45 changes: 34 additions & 11 deletions src/Nancy.Rdf.Tests/RdfResponseProcessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ public class RdfResponseProcessorTests
{
private const string FallbackSerializationKey = "__nrfs";

private static readonly NancyContext NancyContext;

static RdfResponseProcessorTests()
{
NancyContext = new NancyContext
{
Request = new Request(
"GET",
new Url("http://example.com/api/test")
{
BasePath = "api"
})
};
}

[Test]
public void Should_not_allow_processing_when_no_compatible_serializer_is_available()
{
Expand Down Expand Up @@ -87,27 +102,35 @@ public void Should_pass_SiteBase_from_context_to_serializtion()
var serializer = A.Fake<IRdfSerializer>();
A.CallTo(() => serializer.CanSerialize(A<string>.Ignored)).Returns(true);
var processor = new RdfResponseProcessorTestable(new[] { serializer });
var nancyContext = new NancyContext
{
Request = new Request(
"GET",
new Url("http://example.com/api/test")
{
BasePath = "api"
})
};

// when
var response = processor.Process(new MediaRange("application/rdf+xml"), new object(), nancyContext);
var response = processor.Process(new MediaRange("application/rdf+xml"), new object(), NancyContext);
response.Contents(new MemoryStream());

// then
A.CallTo(() => serializer.Serialize(
RdfSerialization.RdfXml.MediaType,
A<WrappedModel>.That.Matches(wm => wm.BaseUrl == new Uri(nancyContext.Request.Url.SiteBase)),
A<WrappedModel>.That.Matches(wm => wm.BaseUrl == new Uri(NancyContext.Request.Url.SiteBase)),
A<MemoryStream>._)).MustHaveHappened();
}

[Test]
public void Should_pass_actual_requested()
{
// given
const string contentType = "application/rdf+xml;profile=testprofile";
var serializer = A.Fake<IRdfSerializer>();
A.CallTo(() => serializer.CanSerialize(A<string>.Ignored)).Returns(true);
var processor = new RdfResponseProcessorTestable(new[] { serializer });

// when
var response = processor.Process(new MediaRange(contentType), new object(), NancyContext);
response.Contents(new MemoryStream());

// then
A.CallTo(() => serializer.Serialize(contentType, A<WrappedModel>._, A<MemoryStream>._)).MustHaveHappened();
}

private class RdfResponseProcessorTestable : RdfResponseProcessor
{
public RdfResponseProcessorTestable(IEnumerable<ISerializer> serializers)
Expand Down
2 changes: 1 addition & 1 deletion src/Nancy.Rdf/Responses/RdfResponseProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public Response Process(MediaRange requestedMediaRange, dynamic model, NancyCont
Contents = stream =>
{
var wrappedModel = new WrappedModel(model, context.Request.Url.SiteBase);
_serializer.Serialize(_serialization.MediaType, wrappedModel, stream);
_serializer.Serialize(requestedMediaRange, wrappedModel, stream);
},
StatusCode = HttpStatusCode.OK,
ContentType = _serialization.MediaType
Expand Down

0 comments on commit a26910c

Please sign in to comment.