Example #1
0
        public Func <PhantomAmmoInfo>[] MakeGets()
        {
            return(new Func <PhantomAmmoInfo>[]
            {
                () =>
                {
                    GetCount++;
                    var args = new Dictionary <string, string>();

                    var gender = faker.PickRandom <Name.Gender>();
                    var firstName = faker.Name.FirstName(gender);
                    var lastName = faker.Name.LastName(gender);
                    var city = faker.Address.City();

                    var minage = r.Next(10, 80);
                    var maxage = r.Next(minage, 81);
                    var(skip, take) = GenerateSkipAndTake();

                    var condition = r.Next(0, 4);
                    if (condition == 0)
                    {
                        args["name"] = $"{lastName} {firstName}";
                    }
                    else if (condition == 1)
                    {
                        args["name"] = $"{lastName}";
                    }
                    else if (condition == 2)
                    {
                        args["name"] = $"{firstName}";
                    }

                    condition = r.Next(0, 5);
                    if (condition == 0)
                    {
                        args["city"] = $"{city}";
                    }

                    condition = r.Next(0, 20);
                    if (condition == 0)
                    {
                        args["minage"] = $"{minage}";
                    }
                    else if (condition == 1)
                    {
                        args["maxage"] = $"{maxage}";
                    }
                    else if (condition == 2)
                    {
                        args["minage"] = $"{minage}";
                        args["maxage"] = $"{maxage}";
                    }

                    args["take"] = $"{take}";

                    var url = "/api/users";
                    return PhantomAmmoInfo.MakeGet(url, args);
                }
            });
        }
        public async Task InvokeAsync(HttpContext context)
        {
            var opts = optsAccessor.CurrentValue;

            if (!opts.Enabled)
            {
                await next.Invoke(context);

                return;
            }

            PhantomAmmoInfo ammo = null;

            try
            {
                ammo = await MakeAmmo(context.Request);

                await next.Invoke(context);
                await StoreAmmo(ammo, context.Response.StatusCode, opts);
            }
            catch (Exception)
            {
                await StoreAmmo(ammo, -1, opts);

                throw;
            }
        }
        public Func <PhantomAmmoInfo>[] MakeGets()
        {
            return(new Func <PhantomAmmoInfo>[]
            {
                () =>
                {
                    GetCount++;

                    var from = fromRates[r.Next(0, fromRates.Length)];
                    var to = toRates[r.Next(0, toRates.Length)];

                    var url = $"/api/v2/rates/{from}";

                    if (r.NextDouble() < 0.95)
                    {
                        url += $"/{to}";
                    }

                    return PhantomAmmoInfo.MakeGet(url, new Dictionary <string, string>());
                }
            });
        }
        private async Task StoreAmmo(PhantomAmmoInfo ammo, int responseStatusCode, PhantomAmmoCollectorOptions opts)
        {
            if (ammo == null)
            {
                return;
            }

            try
            {
                ammo.Status = GetResponseStatus(responseStatusCode);

                if (!string.IsNullOrWhiteSpace(opts.AllRequestsFile))
                {
                    using (var file = File.AppendText(opts.AllRequestsFile))
                    {
                        await file.WriteAsync(ammo.ToString());
                    }
                }
                if (!string.IsNullOrWhiteSpace(opts.GoodRequestsFile) &&
                    ammo.Status == PhantomAmmoStatuses.Good)
                {
                    using (var file = File.AppendText(opts.GoodRequestsFile))
                    {
                        await file.WriteAsync(ammo.ToString());
                    }
                }
                else if (!string.IsNullOrWhiteSpace(opts.BadRequestsFile))
                {
                    using (var file = File.AppendText(opts.BadRequestsFile))
                    {
                        await file.WriteAsync(ammo.ToString());
                    }
                }
            }
            catch (Exception e)
            {
                logger.LogWarning(e, e.Message);
            }
        }
 private async Task <PhantomAmmoInfo> MakeAmmo(HttpRequest request)
 {
     try
     {
         var url      = request.GetEncodedPathAndQuery();
         var ammoInfo = new PhantomAmmoInfo(url, request.Method)
         {
             Body     = await ExtractBody(request),
             Protocol = request.Protocol
         };
         foreach (var h in request.Headers)
         {
             ammoInfo.Headers.Add(h.Key, h.Value);
         }
         return(ammoInfo);
     }
     catch (Exception e)
     {
         logger.LogWarning(e, e.Message);
         return(null);
     }
 }