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

Commit

Permalink
Merge pull request #2890 from youngbob/fix-status-code-xml-serialize
Browse files Browse the repository at this point in the history
 Ensure DefaultStatusCodeHandlerResult can be XML serialized
  • Loading branch information
thecodejunkie committed May 10, 2018
2 parents a082f04 + 3515c2e commit 3cef9e6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/Nancy/ErrorHandling/DefaultStatusCodeHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,13 @@ public void Handle(HttpStatusCode statusCode, NancyContext context)
? DisplayErrorTracesFalseMessage
: string.Concat("<pre>", context.GetExceptionDetails().Replace("<", "&gt;").Replace(">", "&lt;"), "</pre>");

var result = new DefaultStatusCodeHandlerResult(statusCode, this.errorMessages[statusCode], details);
var result = new DefaultStatusCodeHandlerResult
{
Details = details,
Message = this.errorMessages[statusCode],
StatusCode = statusCode
};

try
{
context.Response = this.responseNegotiator.NegotiateResponse(result, context);
Expand Down Expand Up @@ -152,20 +158,13 @@ private static string LoadResource(string filename)
}
}

internal class DefaultStatusCodeHandlerResult
public class DefaultStatusCodeHandlerResult
{
public DefaultStatusCodeHandlerResult(HttpStatusCode statusCode, string message, string details)
{
this.StatusCode = statusCode;
this.Message = message;
this.Details = details;
}

public HttpStatusCode StatusCode { get; private set; }
public HttpStatusCode StatusCode { get; set; }

public string Message { get; private set; }
public string Message { get; set; }

public string Details { get; private set; }
public string Details { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -212,5 +212,36 @@ public void Should_reset_negotiation_context()
// Then
Assert.Null(context.NegotiationContext.ViewName);
}

[Fact]
public void Should_be_xml_serializable()
{
// Given
var environment = new DefaultNancyEnvironment();
environment.AddValue(Xml.XmlConfiguration.Default);
environment.Tracing(
enabled: true,
displayErrorTraces: true);
var serializer = new Nancy.Responses.DefaultXmlSerializer(environment);
var model = new DefaultStatusCodeHandler.DefaultStatusCodeHandlerResult()
{
StatusCode = HttpStatusCode.NotFound,
Message = "not found",
Details = "not found details"
};

// When
var xml = new System.Xml.XmlDocument();
using (var stream = new MemoryStream())
{
serializer.Serialize("application/xml", model, stream);

stream.Position = 0;
xml.Load(stream);
}

// Then
Assert.Equal("DefaultStatusCodeHandlerResult", xml.DocumentElement.Name);
}
}
}

0 comments on commit 3cef9e6

Please sign in to comment.