PragmaticFlow / NBomber

Modern and flexible load testing framework for Pull and Push scenarios, designed to test any system regardless a protocol (HTTP/WebSockets/AMQP etc) or a semantic model (Pull/Push).
https://nbomber.com
Other
2.08k stars 128 forks source link

Feature request: Add .WithCallback() to HttpRequest #220

Closed lkurzyniec closed 4 years ago

lkurzyniec commented 4 years ago

Right now I'm trying to write load test for POST method. My API returns some validation errors. When executing nbomber with .RunTest() there is no way to check the messages of any single response. By adding .WithCallback() to HttpRequest, I can do sth like this .WithCallback(async (response) => { Console.WriteLine(await response.ReadAsStringAsync()) });

lkurzyniec commented 4 years ago

I achieved what I expected in .WithCheck(), but it's kind of workaround.

                    .WithCheck(async response =>
                    {
                        if (!response.IsSuccessStatusCode)
                        {
                            Trace.TraceError(await response.Content.ReadAsStringAsync());
                        }
                        return response.IsSuccessStatusCode;
                    })
lkurzyniec commented 4 years ago

Here is my example including two steps: https://github.com/lkurzyniec/netcore-boilerplate/blob/master/test/HappyCode.NetCoreBoilerplate.Api.LoadTests/EmployeesControllerTests.cs

I got there:

            var get = HttpStep.Create("get emp", ctx =>
            {
                var response = ctx.GetPreviousStepResponse<HttpResponseMessage>();
                var content = response.Content.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult();
                var emp = content.Deserialize<EmployeeDto>();
                Trace.WriteLine($"The previous step created employee with Id = {emp.Id}");

                var getUrl = response.Headers.Location;

                return Http.CreateRequest("GET", getUrl.ToString())
                    .WithHeader("Authorization", "ApiKey ABC-xyz")
                    .WithCheck(response => Task.FromResult(response.IsSuccessStatusCode));
            });

I don't like this line: response.Content.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult();

AntyaDev commented 4 years ago

Hi @lkurzyniec Why don't try using async await? https://github.com/PragmaticFlow/NBomber.Http/blob/dev/examples/NBomber.Http.CSharp/SequentialSteps.cs#L13

lkurzyniec commented 4 years ago

@AntyaDev you are right, I didn't notice that there is overload with Task ;)

@AntyaDev one more question - I would like to log both Request and Response that nbomber send and receive from API - how can I achieve that? With a Response I use .WithCheck() as a workaround, but I feel there should be a better way. And how about Request?

AntyaDev commented 4 years ago

Hi @lkurzyniec I recently added tracing for NBomber.Http plugin. please take a look at this example: https://github.com/PragmaticFlow/NBomber/blob/dev/examples/CSharp/HttpTests/TracingHttp.cs#L38

this functionality is builtin for 1.0.0-rc4