asp.net-web-apifast-endpoints

FastEndpoints with query string parameters


FastEndpoints support query string parameter and we can take advantage of Query to get the parameters, which is working fine. The only issue is it does not seem to be a straightforward to test it with App.Client.GETAsync as I cannot see any overloading method supporting query string any ideas?

I am excepting some sort of overloading method in app.Client.GETAsync method to support query string


Solution

  • i'm assuming you don't have a request dto for this endpoint and you're probably doing something like the following:

    sealed class MyEndpoint : EndpointWithoutRequest<MyResponse>
    {
        public override void Configure()
        {
            Get("hello");
            AllowAnonymous();
        }
    
        public override async Task HandleAsync(CancellationToken ct)
        {
            var name = Query<string>("name");
            await SendAsync(new() { Message = $"hello {name}" });
        }
    }
    
    sealed class MyResponse
    {
        public string Message { get; set; }
    }
    

    if so, you can construct the url of the endpoint yourself which includes the query parameter value and do the test something like the following:

    public class MyTests(Sut App) : TestBase<Sut>
    {
        [Fact]
        public async Task Query_Param_Test()
        {
            const string nameQueryParam = "john doe";
            var url = $"hello?name={nameQueryParam}";
            var (rsp, res) = await App.Client.GETAsync<EmptyRequest, MyResponse>(url, new());
            rsp.IsSuccessStatusCode.Should().BeTrue();
            res.Message.Should().Be("hello john doe");
        }
    }
    

    if there is a request dto on the otherhand, you can simply construct a request dto instance and pass it down to the http call like so:

    var request = new MyRequest { Name = "john doe" };
    var (rsp, res) = await App.Client.GETAsync<MyEndpoint, MyRequest, MyResponse>(request);