PragmaticFlow/NBomber.Http

`Http.CreateRequest()` adds a trailing slash (`/`) in the URL

Closed this issue · 2 comments

Working with the examples provided on the README.md file, I tried to create an HttpClient instance with BaseAddress to perform each call by only providing the specific endpoint. For example:

using var httpClient = new HttpClient() {
    //Here I set the common base URL and a common Ttimeout
    BaseAddress = new Uri("https://myPersonalUrl"), Timeout = TimeSpan.FromSeconds(30)
};

var scenario = Scenario.Create("http_scenario", async context =>
{
    var step1 = await Step.Run("step_1", context, async () =>
    {
        var request =
            Http.CreateRequest("GET", "messages")
                .WithHeader("Accept", "application/json")
                .WithBody(new StringContent("{ some JSON }", Encoding.UTF8, "application/json"));

         //This request above will have as Url: "https://myPersonalUrl/messages/"

        var response = await Http.Send(httpClient, request);

        return response;
    });

    return Response.Ok();
});

So, the issue is that the Url created in the request has a trailing slash (/). Generally, the slash is ignored when the HTTP call is performed, but when you have other extension methods (like I do), you will have issues when concatenating the string to create a new Url.

Hi @konarx ,

I will need to check what can we do about this. I got your point.
This is how the URL is populated, as you can see we don't do any magic just construct plain new Uri.

https://github.com/PragmaticFlow/NBomber.Http/blob/dev/src/NBomber.Http/FSharp.fs#L62

Thank you @AntyaDev .
The following code is a workaround:

using var httpClient = new HttpClient() {
    //Here I create the URL as a whole
    BaseAddress = new Uri("https://myPersonalUrl/messages"), Timeout = TimeSpan.FromSeconds(30)
};

var scenario = Scenario.Create("http_scenario", async context =>
{
    var step1 = await Step.Run("step_1", context, async () =>
    {
        var request =
            //Here I pass empty string
            Http.CreateRequest("GET", "")
                .WithHeader("Accept", "application/json")
                .WithBody(new StringContent("{ some JSON }", Encoding.UTF8, "application/json"));

         //This request above will have as Url: "https://myPersonalUrl/messages" as expected
 
        var response = await Http.Send(httpClient, request);

        return response;
    });

    return Response.Ok();
});