Пример #1
0
        public TSelf HaveFailed()
        {
            if (Response.IsSuccess)
            {
                throw AssertionExceptionFactory.CreateForResponse(Response, "Expected response to have failed, but it succeeded.");
            }

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

            return(this);
        }
Пример #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);
        }
        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);
        }
Пример #6
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);
        }
Пример #8
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);
        }