public async Task apply_should_have_candidate_for_matched_api_version()
        {
            // arrange
            var feature = new Mock <IApiVersioningFeature>();
            var context = new EndpointSelectorContext();
            var items   = new object[]
            {
                new ActionDescriptor()
                {
                    Properties = { [typeof(ApiVersionModel)] = new ApiVersionModel(new ApiVersion(1, 0)) },
                },
            };
            var endpoint   = new Endpoint(c => CompletedTask, new EndpointMetadataCollection(items), default);
            var candidates = new CandidateSet(new[] { endpoint }, new[] { new RouteValueDictionary() }, new[] { 0 });
            var policy     = new ApiVersionMatcherPolicy(NewDefaultOptions(), NewReporter(), NewLoggerFactory());

            feature.SetupProperty(f => f.RequestedApiVersion, new ApiVersion(1, 0));

            var httpContext = NewHttpContext(feature);

            // act
            await policy.ApplyAsync(httpContext, context, candidates);

            // assert
            candidates.IsValidCandidate(0).Should().BeTrue();
        }
        public async Task apply_should_use_400_endpoint_for_ambiguous_api_version()
        {
            // arrange
            var feature        = new Mock <IApiVersioningFeature>();
            var errorResponses = new Mock <IErrorResponseProvider>();
            var result         = new Mock <IActionResult>();

            feature.SetupGet(f => f.RequestedApiVersion).Throws(new AmbiguousApiVersionException("Test", new[] { "1.0", "2.0" }));
            result.Setup(r => r.ExecuteResultAsync(It.IsAny <ActionContext>())).Returns(CompletedTask);
            errorResponses.Setup(er => er.CreateResponse(It.IsAny <ErrorResponseContext>())).Returns(result.Object);

            var options = Options.Create(new ApiVersioningOptions()
            {
                ErrorResponses = errorResponses.Object
            });
            var policy      = new ApiVersionMatcherPolicy(options, NewReporter(), NewLoggerFactory());
            var httpContext = NewHttpContext(feature);
            var context     = new EndpointSelectorContext();
            var candidates  = new CandidateSet(Empty <Endpoint>(), Empty <RouteValueDictionary>(), Empty <int>());

            // act
            await policy.ApplyAsync(httpContext, context, candidates);

            await context.Endpoint.RequestDelegate(httpContext);

            // assert
            result.Verify(r => r.ExecuteResultAsync(It.IsAny <ActionContext>()), Once());
            errorResponses.Verify(er => er.CreateResponse(It.Is <ErrorResponseContext>(c => c.StatusCode == 400 && c.ErrorCode == "AmbiguousApiVersion")), Once());
        }
コード例 #3
0
 public void applies_to_endpoints_should_return_true_for_api_versioned_actions()
 {
     // arrange
     var policy = new ApiVersionMatcherPolicy(NewDefaultOptions(), NewReporter(), NewLoggerFactory());
     var items  = new object[]
     {
         new ActionDescriptor()
         {
             Properties = { [typeof(ApiVersionModel)] = ApiVersionModel.Default },
         },
     };
     var endpoints = new[]
        public async Task apply_should_use_400_endpoint_for_unmatched_api_version()
        {
            // arrange
            var feature        = new Mock <IApiVersioningFeature>();
            var errorResponses = new Mock <IErrorResponseProvider>();
            var result         = new Mock <IActionResult>();

            feature.SetupProperty(f => f.RawRequestedApiVersion, "2.0");
            feature.SetupProperty(f => f.RequestedApiVersion, new ApiVersion(2, 0));
            result.Setup(r => r.ExecuteResultAsync(It.IsAny <ActionContext>())).Returns(CompletedTask);
            errorResponses.Setup(er => er.CreateResponse(It.IsAny <ErrorResponseContext>())).Returns(result.Object);

            var options = Options.Create(new ApiVersioningOptions()
            {
                ErrorResponses = errorResponses.Object
            });
            var policy  = new ApiVersionMatcherPolicy(options, NewReporter(), NewLoggerFactory());
            var context = new EndpointSelectorContext();
            var items   = new object[]
            {
                new ActionDescriptor()
                {
                    DisplayName       = "Test",
                    ActionConstraints = new IActionConstraintMetadata[] { new HttpMethodActionConstraint(new[] { "GET" }) },
                    Properties        = { [typeof(ApiVersionModel)] = new ApiVersionModel(new ApiVersion(1, 0)) },
                },
            };
            var endpoint    = new Endpoint(c => CompletedTask, new EndpointMetadataCollection(items), default);
            var candidates  = new CandidateSet(new[] { endpoint }, new[] { new RouteValueDictionary() }, new[] { 0 });
            var httpContext = NewHttpContext(feature);

            // act
            await policy.ApplyAsync(httpContext, context, candidates);

            await context.Endpoint.RequestDelegate(httpContext);

            // assert
            result.Verify(r => r.ExecuteResultAsync(It.IsAny <ActionContext>()), Once());
            errorResponses.Verify(er => er.CreateResponse(It.Is <ErrorResponseContext>(c => c.StatusCode == 400 && c.ErrorCode == "UnsupportedApiVersion")), Once());
        }