NancyFx check returning invalid Json

We are using NancyFx to host a single page application and multiple RTST endpoints. The code and the test use the same bootloader, the only difference is that in the tests we use the Nancy browser from the test environment.

If I return a simple object from the website everything works as expected, fades through the application or test environment. However, the real problem is that the returned object contains some dynamic data, even though it is not itself a dynamic object. The app works fine, but the tests fail because the JSON is invalid, the lines are missing quotes, and the dates are not formatted correctly.

var browser = new Browser(bootstrapper);
_response = browser.Get("http://localhost/...", with =>
{
    with.HttpRequest();
    with.Header("Accept", "application/json");
});

var bstr = _response.Body.AsString();

      

I attached a method to the pipeline's AfterRequest method during testing, and I can see that the JSON string looks good when looking at the Response property in the NancyContext, but as soon as we get the response object from the browser and convert it to a string it is already invalid JSON.

protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
{
    base.ApplicationStartup(container, pipelines);
    pipelines.AfterRequest += Foo;
}

private void Foo(NancyContext obj)
{
    //Can see the Response on the NancyContext here
}

      

If I am unable to resolve this issue, I will look at how to test the Nancy test environment and my own host in the context of tests.

EDIT

I get a similar result using NancySelfHost. It looks like it doesn't pick up Json.Net serializer when using native host or test browser. I used the default custom serializer registration setup in Tiny IoC https://github.com/NancyFx/Nancy.Serialization.JsonNet .

public class CustomJsonSerializer : JsonSerializer
{
    public CustomJsonSerializer()
    {
        this.ContractResolver = new CamelCasePropertyNamesContractResolver();
        this.Formatting = Formatting.Indented;
    }
}

public class Bootstrapper : DefaultNancyBootstrapper
{
    protected override void ConfigureApplicationContainer(TinyIoCContainer container)
    {
        base.ConfigureApplicationContainer(container);

        container.Register(typeof(JsonSerializer), typeof(CustomJsonSerializer));
    }
}

      

+3


source to share


2 answers


I managed to get it to work, but the solution seems to be wrong.

This was done by removing the custom Json.Net serializer and connecting to the loader.

container.Register(typeof(JsonSerializer), typeof(CustomJsonSerializer));

      



And adding the JsonNetSerializer manually to the internal config.

protected override NancyInternalConfiguration InternalConfiguration
{
    get
    {
        return NancyInternalConfiguration
              .WithOverrides(nic =>
              {
                  ...
                  nic.Serializers.Clear();
                  nic.Serializers.Insert(0, typeof(JsonNetSerializer));
              });
    }
}

      

+1


source


I had a similar problem with ObjectId structures (MongoDB) that were serialized to JSON without quotes. Try changing Nancy's default serializer to Json.NET:

https://github.com/NancyFx/Nancy.Serialization.JsonNet



It solved this problem for me.

+1


source







All Articles