public JsonHttpResponseValidation HaveAnyContent()
        {
            if (string.IsNullOrWhiteSpace(Response.Content))
            {
                throw AssertionExceptionFactory.CreateForResponse(Response, "Expected response content to NOT be: NULL, Empty or WhiteSpace.");
            }

            return(this);
        }
示例#2
0
        public TSelf HaveFailed()
        {
            if (Response.IsSuccess)
            {
                throw AssertionExceptionFactory.CreateForResponse(Response, "Expected response to have failed, but it succeeded.");
            }

            return(this as TSelf);
        }
示例#3
0
        public TSelf BeSuccessful()
        {
            if (!Response.IsSuccess)
            {
                throw AssertionExceptionFactory.CreateForResponse(Response, "Expected response to be successful, but it was failed.");
            }

            return(this as TSelf);
        }
示例#4
0
        public TSelf HaveStatus(HttpStatusCode status)
        {
            if (Response.StatusCode != status)
            {
                throw AssertionExceptionFactory.CreateForResponse(Response, "Expected status to be '{0}', but got '{1}'.", status, Response.StatusCode);
            }

            return(this as TSelf);
        }
示例#5
0
        protected HttpResponseValidation(TResponse response)
        {
            if (response == null)
            {
                throw AssertionExceptionFactory.Create("Expected response to be an instance, but got NULL.");
            }

            Response = response;
        }
        public JsonHttpResponseValidation Match <T>(T entity) where T : class
        {
            var o = JsonConvert.DeserializeObject <T>(Response.Content);

            if (o == null)
            {
                throw AssertionExceptionFactory.CreateForResponse(Response, "Expected response content to match sent entity, but it deserialized to '{0}'", NullString.Value);
            }

            o.ShouldBeValueEqualTo(entity);

            return(this);
        }
示例#7
0
        public JsonHttpResponseValidation(HttpTextResponse response)
        {
            if (response == null)
            {
                throw AssertionExceptionFactory.Create("Expected response to be an instance, but got NULL.");
            }

            if (response.ContentType != "application/json")
            {
                throw AssertionExceptionFactory.CreateForResponse(response, "Expected response content type to be '{0}', but got '{1}'.", NullString.IfNull(response.ContentType));
            }

            Response = response;
        }
        public JsonHttpResponseValidation NotHavingSpecificValueFor <T>(string path, T unExpectedValue)
        {
            var node = JToken.Parse(Response.Content).SelectToken(path, false);

            if (node == null)
            {
                throw AssertionExceptionFactory.CreateForResponse(Response, "Expected sent path '{0}' to map to a node in the JSON document, but it did not.", path);
            }

            if (node.ValueIsEqualTo(unExpectedValue))
            {
                throw AssertionExceptionFactory.CreateForResponse(Response, "Expected sent path '{0}' to NOT return '{1}', but it sure got '{2}'.", path, unExpectedValue, node.Value <T>());
            }

            return(this);
        }
示例#9
0
        public JsonHttpResponseValidation HaveJsonConformingToSchema(string properties)
        {
            if (!properties.StartsWith("{"))
            {
                properties = "{" + properties;
            }

            if (!properties.EndsWith("}"))
            {
                properties += "}";
            }

            var schema = JsonSchema.Parse("{type: 'object', properties:" + properties + "}");

            JToken.Parse(Response.Content).Validate(schema, (sender, args) =>
            {
                throw AssertionExceptionFactory.CreateForResponse(Response,
                                                                  "Expected object to be conforming to specified JSON schema. Failed when inspecting '{0}' due to '{1}'", args.Path, args.Message);
            });

            return(this);
        }
        public async Task <JsonHttpResponseValidation> HaveJsonConformingToSchemaAsync(string jsonSchema)
        {
            var schema = await JsonSchema4.FromJsonAsync(jsonSchema);

            var errors = schema.Validate(Response.Content);

            if (!errors.Any())
            {
                return(this);
            }

            var details = new StringBuilder();

            var c = 0;

            foreach (var error in errors)
            {
                details.AppendLine($"Err#{++c}:{error.Path}:{error.Kind}");
            }

            throw AssertionExceptionFactory.CreateForResponse(Response,
                                                              "Expected object to be conforming to specified JSON schema.{0}Details:{0}{1}", Environment.NewLine, details);
        }
示例#11
0
        private static void AreValueEqual(Type type, object a, object b)
        {
            if (ReferenceEquals(a, b))
            {
                return;
            }

            if (a == null && b == null)
            {
                return;
            }

            if (a == null || b == null)
            {
                throw AssertionExceptionFactory.Create("Values of type {0} was found to be different.", type.Name);
            }

            if (type.IsEnumerableType())
            {
                var enum1 = a as IEnumerable;
                if (enum1 == null)
                {
                    throw AssertionExceptionFactory.Create("Value of type {0} was found to be null. Expected an enumerable instance.");
                }

                var enum2 = b as IEnumerable;
                if (enum2 == null)
                {
                    throw AssertionExceptionFactory.Create("Value of type {0} was found to be null. Expected an enumerable instance.");
                }

                var e1 = enum1.GetEnumerator();
                var e2 = enum2.GetEnumerator();

                while (e1.MoveNext() && e2.MoveNext())
                {
                    AreValueEqual(e1.Current.GetType(), e1.Current, e2.Current);
                }
                return;
            }

            if (type == typeof(object))
            {
                throw AssertionExceptionFactory.Create("Equality comparision not feasible to type {0}.", type.Name);
            }

            if (type.IsSimpleType())
            {
                if (!Equals(a, b))
                {
                    throw AssertionExceptionFactory.Create("Comparing simple types of '{0}', expected them to be equal but '{1}' != '{2}'", type.Name, a, b);
                }

                return;
            }

            var properties = type.GetProperties();

            if (properties.Length == 0)
            {
                if (!Equals(a, b))
                {
                    throw AssertionExceptionFactory.Create("Comparing simple types of '{0}', expected them to be equal but '{1}' != '{2}'", type.Name, a, b);
                }
            }

            foreach (var propertyInfo in type.GetProperties())
            {
                var propertyType = propertyInfo.PropertyType;
                var valueForA    = propertyInfo.GetValue(a, null);
                var valueForB    = propertyInfo.GetValue(b, null);

                var isSimpleType = propertyType.IsSimpleType();
                if (isSimpleType)
                {
                    if (!Equals(valueForA, valueForB))
                    {
                        throw AssertionExceptionFactory.Create("Values in property '{0}' doesn't match.", propertyInfo.Name);
                    }
                }
                else
                {
                    AreValueEqual(propertyType, valueForA, valueForB);
                }
            }
        }