public async Task <IActionResult> Post([FromBody] JObject harFileData)
        {
            if (harFileData != null)
            {
                var harData = HarConvert.Deserialize(harFileData.ToString());

                // validating manually
                var valid = this.ValidateHarFile(harData);

                if (valid)
                {
                    var firstPage = harData.Log.Pages.First();

                    var harFile = new HarFile();
                    harFile.URL              = firstPage.Title;
                    harFile.StartedDateTime  = firstPage.StartedDateTime;
                    harFile.HarContentString = JsonConvert.SerializeObject(harFileData);

                    await this._harFilesService.SaveAsync(harFile);
                }
                else
                {
                    return(BadRequest("The HAR file data is invalid."));
                }
            }

            return(CreatedAtAction(nameof(Post), null));
        }
        public async Task <IActionResult> Put(long id, [FromBody] Har harData)
        {
            // validating by model
            if (!ModelState.IsValid)
            {
                // could have more robust error object returned
                return(BadRequest(ModelState));
            }

            var existingHarFile = await this._harFilesService.GetByIdAsync(id);

            if (existingHarFile == null)
            {
                // returing 404 in this case, could use 409 or 403
                return(NotFound());
            }
            else
            {
                var firstPage = harData.Log.Pages.First();

                var harFile = new HarFile();
                existingHarFile.URL              = firstPage.Title;
                existingHarFile.StartedDateTime  = firstPage.StartedDateTime;
                existingHarFile.HarContentString = JsonConvert.SerializeObject(harData);

                await this._harFilesService.UpdateAsync(existingHarFile);
            }

            return(Ok());
        }
        public async Task UpdateAsync(HarFile harFile)
        {
            if (harFile == null)
            {
                throw new ArgumentNullException(nameof(harFile));
            }

            await this._dbContext.SaveChangesAsync();
        }
 public async Task UpdateAsync(HarFile harFile)
 {
     try
     {
         await this._harFileRepository.UpdateAsync(harFile);
     }
     catch (Exception ex)
     {
         throw new Exception("Unhandled Service Exception", ex);
     }
 }
Beispiel #5
0
        private void AddTestData(ApiDbContext dbContext)
        {
            var test = new HarFile
            {
                HarFileId        = 1,
                StartedDateTime  = DateTime.Now,
                URL              = "https://www.microsoft.com/net/",
                HarContentString = null
            };

            dbContext.HarFiles.Add(test);
            dbContext.SaveChanges();
        }
        public async void GetAverageBodySize()
        {
            var harFile = new HarFile
            {
                StartedDateTime  = DateTime.Now,
                URL              = "https://www.microsoft.com/net",
                HarContentString = File.ReadAllText(@"www.microsoft.com.har")
            };

            await this._harFileRepository.SaveAsync(harFile);

            var avgBodySize = await this._harFilesService.GetAverageBodySize(1);

            Assert.Equal(1383.2258064516129, avgBodySize);
        }
Beispiel #7
0
        static void Main(string[] args)
        {
            var    filename = string.Join(" ", args);
            string content  = null;

            try {
                content = File.ReadAllText(filename);
            }
            catch (Exception ex) {
                Console.WriteLine("Unable to open file: " + filename);
                Console.WriteLine(ex.Message);
                Environment.Exit(1);
            }

            var options = new JsonSerializerOptions
            {
                PropertyNameCaseInsensitive = true,
            };

            HarFile har = null;

            try {
                har = JsonSerializer.Deserialize <HarFile>(content, options);
            }
            catch (Exception ex) {
                Console.WriteLine("Unable to deserialize file content: " + filename);
                Console.WriteLine(ex.Message);
                Environment.Exit(2);
            }

            List <InMemoryFile> files = har.Log?.Entries?
                                        .Where(x => x?.Response?.BodySize > 0)
                                        .Select(x => ToInMemoryFile(x))
                                        .GroupBy(x => x.FileName)
                                        .Select(x => x.First())
                                        .ToList();

            try {
                var zip = GetZipArchive(files);
                File.WriteAllBytes(filename + ".zip", zip);
            }
            catch (Exception ex) {
                Console.WriteLine("Unable to create zip file from files:");
                Console.WriteLine(ex.Message);
                Environment.Exit(4);
            }
        }
        public async void GetRequestUrlsByFilter()
        {
            var harFile = new HarFile
            {
                StartedDateTime  = DateTime.Now,
                URL              = "https://www.microsoft.com/net",
                HarContentString = File.ReadAllText(@"www.microsoft.com.har")
            };

            await this._harFileRepository.SaveAsync(harFile);

            var filter = "images";

            var foundUrls = await this._harFilesService.GetRequestUrlsByFilter(1, filter);

            Assert.Equal(11, foundUrls.Count());
        }