Exemplo n.º 1
0
        public async Task TestAsync_ThrowsException_IfExpectedStatusCodeIs2xxAndRequestDoesNotMatchSpec(
            string requestUri,
            string statusCode,
            string expectedExceptionMessage)
        {
            var subject = new FakeApiTestRunner();

            subject.Configure(c =>
            {
                c.OpenApiDocs.Add("v1", new OpenApiDocument
                {
                    Paths = new OpenApiPaths
                    {
                        ["/api/products"] = new OpenApiPathItem
                        {
                            Operations = new Dictionary <OperationType, OpenApiOperation>
                            {
                                [OperationType.Get] = new OpenApiOperation
                                {
                                    OperationId = "GetProducts",
                                    Parameters  = new List <OpenApiParameter>
                                    {
                                        new OpenApiParameter
                                        {
                                            Name     = "param",
                                            Required = true,
                                            In       = ParameterLocation.Query
                                        }
                                    },
                                    Responses = new OpenApiResponses
                                    {
                                        ["200"] = new OpenApiResponse()
                                    }
                                }
                            }
                        }
                    }
                });
            });

            var exception = await Record.ExceptionAsync(() => subject.TestAsync(
                                                            "v1",
                                                            "GetProducts",
                                                            statusCode,
                                                            new HttpRequestMessage
            {
                RequestUri = new Uri(requestUri, UriKind.Relative),
                Method     = HttpMethod.Get
            },
                                                            CreateHttpClient()));

            Assert.Equal(expectedExceptionMessage, exception?.Message);
        }
Exemplo n.º 2
0
        public async Task TestAsync_ThrowsException_IfOperationNotFound()
        {
            var subject = new FakeApiTestRunner();

            subject.Configure(c =>
            {
                c.OpenApiDocs.Add("v1", new OpenApiDocument());
            });

            var exception = await Assert.ThrowsAsync <InvalidOperationException>(() => subject.TestAsync(
                                                                                     "v1",
                                                                                     "GetProducts",
                                                                                     "200",
                                                                                     new HttpRequestMessage(),
                                                                                     CreateHttpClient()));

            Assert.Equal("Operation with id 'GetProducts' not found in OpenAPI document 'v1'", exception.Message);
        }