public async Task ValidateResponseValidShouldBeTrueWhenResponseConformsToOpenApiSpecWithEmptyArray()
        {
            await Setup();

            await ResponseValidation.Initialise("http://localhost:3010/");

            var validationResult = await ResponseValidation.ValidateResponse(HttpMethod.Get, "/v1/pets", HttpStatusCode.OK,
                                                                             new Dictionary <string, string>(), new List <object>());

            Assert.That(validationResult.Valid, Is.True);
            Assert.That(validationResult.Errors.Count, Is.EqualTo(0));
        }
        public async Task ValidateResponseValidShouldBeFalseWhenResponseDoesNotConformToOpenApiSpec_3_NoJson()
        {
            await Setup();

            await ResponseValidation.Initialise("http://localhost:3010/");

            var validationResult = await ResponseValidation.ValidateResponse(HttpMethod.Get, "/v1/pets", HttpStatusCode.OK,
                                                                             new Dictionary <string, string>());

            Assert.That(validationResult.Valid, Is.False);
            Assert.That(validationResult.Errors.Count, Is.EqualTo(1));
            Assert.That(validationResult.Errors[0], Is.EqualTo("{\"path\":\".response\",\"message\":\"response body required.\"}"));
        }
        public async Task ValidateResponseThrowsAnExceptionWhenNotInitialised()
        {
            try
            {
                await ResponseValidation.ValidateResponse(HttpMethod.Get, "/v1/pets", HttpStatusCode.OK,
                                                          new Dictionary <string, string>(), new List <object>());

                Assert.Fail("Fail");
            }
            catch (Exception e)
            {
                Assert.That(e.Message, Is.EqualTo("You have not initialised the ResponseValidation object"));
            }
        }
        public async Task ValidateResponseValidShouldBeTrueWhenResponseConformsToOpenApiSpecWithPopulatedArrayFromJsonString()
        {
            await Setup();

            await ResponseValidation.Initialise("http://localhost:3010/");

            var jsonString = "[{\"id\":1,\"name\":\"bob\",\"type\":\"dog\"}]";
            var json       = JsonConvert.DeserializeObject(jsonString);

            var validationResult = await ResponseValidation.ValidateResponse(HttpMethod.Get, "/v1/pets", HttpStatusCode.OK,
                                                                             new Dictionary <string, string>(), json);

            Assert.That(validationResult.Valid, Is.True);
            Assert.That(validationResult.Errors.Count, Is.EqualTo(0));
        }
        public async Task ValidateResponseValidShouldBeFalseWhenResponseDoesNotConformToOpenApiSpec_2()
        {
            await Setup();

            await ResponseValidation.Initialise("http://localhost:3010/");

            var json = new { dayOfWeek = "Monday" };

            var validationResult = await ResponseValidation.ValidateResponse(HttpMethod.Get, "/v1/pets", HttpStatusCode.OK,
                                                                             new Dictionary <string, string>(), json);

            Assert.That(validationResult.Valid, Is.False);
            Assert.That(validationResult.Errors.Count, Is.EqualTo(1));
            Assert.That(validationResult.Errors[0], Is.EqualTo("{\"path\":\".response\",\"message\":\"should be array\",\"errorCode\":\"type.openapi.validation\"}"));
        }
        public async Task ValidateResponseValidShouldBeTrueWhenResponseConformsToOpenApiSpecWhenUsingRequestAndResponseOverload()
        {
            await Setup();

            await ResponseValidation.Initialise("http://localhost:3010/");

            var request  = new HttpRequestMessage(HttpMethod.Get, new Uri("http://localhost/v1/pets"));
            var response = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent("[]")
            };

            var validationResult = await ResponseValidation.ValidateResponse(request, response);

            Assert.That(validationResult.Valid, Is.True);
            Assert.That(validationResult.Errors.Count, Is.EqualTo(0));
        }
        public async Task ValidateResponseThrowsAnExceptionWhenPathIsNotDefined()
        {
            await Setup();

            await ResponseValidation.Initialise("http://localhost:3010/");

            try
            {
                await ResponseValidation.ValidateResponse(HttpMethod.Get, "", HttpStatusCode.OK,
                                                          new Dictionary <string, string>(), new List <object>());

                Assert.Fail("Fail");
            }
            catch (Exception e)
            {
                Assert.That(e.Message, Is.EqualTo("You must define a path"));
            }
        }
        public async Task ValidateResponseValidShouldBeFalseWhenResponseDoesNotConformToOpenApiSpec_1()
        {
            await Setup();

            await ResponseValidation.Initialise("http://localhost:3010/");

            var json = new List <object>
            {
                new { dayOfWeek = "Monday" }
            };

            var validationResult = await ResponseValidation.ValidateResponse(HttpMethod.Get, "/v1/pets", HttpStatusCode.OK,
                                                                             new Dictionary <string, string>(), json);

            Assert.That(validationResult.Valid, Is.False);
            Assert.That(validationResult.Errors.Count, Is.EqualTo(4));
            Assert.That(validationResult.Errors[0], Is.EqualTo("{\"path\":\".response[0].dayOfWeek\",\"message\":\"should NOT have additional properties\",\"errorCode\":\"additionalProperties.openapi.validation\"}"));
            Assert.That(validationResult.Errors[1], Is.EqualTo("{\"path\":\".response[0].id\",\"message\":\"should have required property 'id'\",\"errorCode\":\"required.openapi.validation\"}"));
            Assert.That(validationResult.Errors[2], Is.EqualTo("{\"path\":\".response[0].name\",\"message\":\"should have required property 'name'\",\"errorCode\":\"required.openapi.validation\"}"));
            Assert.That(validationResult.Errors[3], Is.EqualTo("{\"path\":\".response[0].type\",\"message\":\"should have required property 'type'\",\"errorCode\":\"required.openapi.validation\"}"));
        }