<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Claim.Extensions;
using Claim.ValueMatchers;

namespace Claim.Asserts
{
    public enum PropertyComparison : byte
    {
        IgnoreCase
    }

    public class JsonAssert : IAssert
    {
        private readonly object _expectedResponseStructure;
        private readonly PropertyComparison? _propertyComparison;
        private readonly ICollection<string> _errors = new List<string>();
        private readonly Stack<string> _expectedPropertyPath = new Stack<string>();

        public JsonAssert(object expectedResponseStructure, PropertyComparison? propertyComparison = null)
        {
            _expectedResponseStructure = expectedResponseStructure;
            _propertyComparison = propertyComparison;
        }

        public JsonAssert(string expectedResponseStructure, PropertyComparison? propertyComparison = null)
        {
            _expectedResponseStructure = JsonConvert.DeserializeObject(expectedResponseStructure);
            _propertyComparison = propertyComparison;
        }

        public static IEnumerable<string> CompareArrays(PropertyInfo expectedProperty, object expected, JToken actual, bool matchLength, bool checkPropertyType)
        {
            var assert = new JsonAssert(expected);

            assert.PushExpectedPropertyPath(expectedProperty.Name);
            assert.CompareArraysInternal(expectedProperty, expected, actual, matchLength, checkPropertyType);
            assert.PopExpectedPropertyPath();

            return assert._errors;
        }

        public Result Assert(Response response)
        {
            if(response.ResponseContent == null || response.ResponseContent.Length == 0)
                return Result.Failed<JsonAssert>("JsonAssert: Empty response body.");

            Verify(_expectedResponseStructure, JObject.Parse(Encoding.UTF8.GetString(response.ResponseContent, 0, response.ResponseContent.Length)));

            if (_errors.Any())
                return Result.Failed<JsonAssert>(_errors.ToNewLineSeparatedList());

            return Result.Passed<JsonAssert>();<commit_msg>Handle JsonReaderExceptions in the JsonAssert class<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Claim.Extensions;
using Claim.ValueMatchers;

namespace Claim.Asserts
{
    public enum PropertyComparison : byte
    {
        IgnoreCase
    }

    public class JsonAssert : IAssert
    {
        private readonly object _expectedResponseStructure;
        private readonly PropertyComparison? _propertyComparison;
        private readonly ICollection<string> _errors = new List<string>();
        private readonly Stack<string> _expectedPropertyPath = new Stack<string>();

        public JsonAssert(object expectedResponseStructure, PropertyComparison? propertyComparison = null)
        {
            _expectedResponseStructure = expectedResponseStructure;
            _propertyComparison = propertyComparison;
        }

        public JsonAssert(string expectedResponseStructure, PropertyComparison? propertyComparison = null)
        {
            _expectedResponseStructure = JsonConvert.DeserializeObject(expectedResponseStructure);
            _propertyComparison = propertyComparison;
        }

        public static IEnumerable<string> CompareArrays(PropertyInfo expectedProperty, object expected, JToken actual, bool matchLength, bool checkPropertyType)
        {
            var assert = new JsonAssert(expected);

            assert.PushExpectedPropertyPath(expectedProperty.Name);
            assert.CompareArraysInternal(expectedProperty, expected, actual, matchLength, checkPropertyType);
            assert.PopExpectedPropertyPath();

            return assert._errors;
        }

        public Result Assert(Response response)
        {
            if(response.ResponseContent == null || response.ResponseContent.Length == 0)
                return Result.Failed<JsonAssert>("JsonAssert: Empty response body.");

            try
            {
                Verify(_expectedResponseStructure, JObject.Parse(Encoding.UTF8.GetString(response.ResponseContent, 0, response.ResponseContent.Length)));
            }
            catch (JsonReaderException)
            {
                var actualContentType = response.ResponseMessage?.Content?.Headers?.ContentType?.ToString() ?? "Unknown";
                return Result.Failed<JsonAssert>($"JsonAssert: Not a valid json response. Actual content type: '{actualContentType}'");
            }

            if (_errors.Any())
                return Result.Failed<JsonAssert>(_errors.ToNewLineSeparatedList());

            return Result.Passed<JsonAssert>();
{'fraction_non_alphanumeric': 0.07609139126886985, 'fraction_numerical': 0.0012239902080783353, 'mean_word_length': 3.130581297388374, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7368216', 'n_tokens_mistral': 1103, 'n_tokens_neox': 1071, 'n_words': 258}