Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are no query parameters being passed to my NancyFX module?

Tags:

c#

nancy

I am running a self-hosted NancyFX web server inside of my application. Right now I have one module hosted:

public class MetricsModule : NancyModule
{
    private IStorageEngine _storageEngine;

    public MetricsModule(IStorageEngine storageEngine) : base("/metrics")
    {
        _storageEngine = storageEngine;

        Get["/list"] = parameters =>
        {
            var metrics = _storageEngine.GetKnownMetrics();
            return Response.AsJson(metrics.ToArray());
        };

        Get["/query"] = parameters =>
        {
            var rawStart = parameters.start;
            var rawEnd = parameters.end;
            var metrics = parameters.metrics;

            return Response.AsJson(0);
        };
    }
}

My Bootstrapper class is:

public class OverlookBootStrapper : DefaultNancyBootstrapper
{
    private readonly IStorageEngine _storageEngine;

    public OverlookBootStrapper(IStorageEngine storageEngine)
    {
        _storageEngine = storageEngine;
    }

    protected override void ConfigureApplicationContainer(TinyIoCContainer container)
    {
        container.Register(_storageEngine);
    }
}

I am trying to test it with the following test:

    [TestInitialize]
    public void Init()
    {
        _storageEngine = new Mock<IStorageEngine>();

         var bootstrapper = new OverlookBootStrapper(_storageEngine.Object);
         _browser = new Browser(bootstrapper);
    }

    [TestMethod]
    public void Query_Builds_Correct_Query_From_Parameters()
    {
        var metric = new Metric("device", "category", "name", "suffix");
        var startDate = DateTime.Now;
        var endDate = DateTime.Now.AddMinutes(10);
        var path = "/metrics/query";

        var response = _browser.Get(path, with =>
        {
            with.HttpRequest();
            with.Query("start", startDate.ToString());
            with.Query("end", endDate.ToString());
            with.Query("metrics", metric.ToParsableString());
        });

        Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, "Incorrect status code returned");
        _storageEngine.Verify(x => x.ExecuteQuery(It.Is<Query>(y => y.StartDate == startDate)), Times.Once());
        _storageEngine.Verify(x => x.ExecuteQuery(It.Is<Query>(y => y.EndDate == endDate)), Times.Once());
        _storageEngine.Verify(x => x.ExecuteQuery(It.Is<Query>(y => y.Metrics.Contains(metric))), Times.Once());
    }

When this test is debugged and a breakpoint is put on return Response.AsJson(0);, I inspected the parameters object and noticed that parameters.Count is zero, and all 3 values are null.

What am I doing incorrectly?

Edit: When I bring up this endpoint in the web browser, the same issue occurs. I get a result of 0 sent back to my browser, but when debugging I see that no query string parameters I specify have been recognized by NancyFX.

like image 880
KallDrexx Avatar asked Aug 18 '13 19:08

KallDrexx


1 Answers

The parameters argument to your lambda contains the route parameters you captured in the in your Get["/query"]. In this case nothing. See @thecodejunkie's comment for an example where there is something.

To get to the query paramters use Request.Query. That's also a dynamic and will contain whatever query parameters was in the request. Like so:

 Get["/query"] = parameters =>
    {
        var rawStart = Request.Query.start;
        var rawEnd = Request.Query.end;
        var metrics = Request.Query.metrics;

        return Response.AsJson(0);
    };

This should work with your tests too.

like image 81
Christian Horsdal Avatar answered Sep 28 '22 07:09

Christian Horsdal