Пример #1
0
        /// <summary>
        /// Creates a new instance of the <see cref="CorsService"/>.
        /// </summary>
        /// <param name="options">The option model representing <see cref="CorsOptions"/>.</param>
        public CorsService(IOptions <CorsOptions> options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            _options = options.Value;
        }
Пример #2
0
        /// <summary>
        /// Creates a new instance of the <see cref="CorsService"/>.
        /// </summary>
        /// <param name="options">The option model representing <see cref="CorsOptions"/>.</param>
        /// <param name="loggerFactory">The <see cref="ILoggerFactory"/>.</param>
        public CorsService(IOptions <CorsOptions> options, ILoggerFactory loggerFactory)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            _options = options.Value;
            _logger  = loggerFactory?.CreateLogger <CorsService>();
        }
Пример #3
0
        public void AddDefaultPolicy_OverridesDefaultPolicyName()
        {
            // Arrange
            var corsOptions    = new CorsOptions();
            var expectedPolicy = new CorsPolicy();

            // Act
            corsOptions.AddDefaultPolicy(new CorsPolicy());
            corsOptions.AddDefaultPolicy(expectedPolicy);

            // Assert
            var actualPolicy = corsOptions.GetPolicy(corsOptions.DefaultPolicyName);

            Assert.Same(expectedPolicy, actualPolicy);
        }
        public async Task GetsNamedPolicy(string policyName)
        {
            // Arrange
            var options = new CorsOptions();
            var policy = new CorsPolicy();
            options.AddPolicy(policyName, policy);

            var corsOptions = Options.Create(options);
            var policyProvider = new DefaultCorsPolicyProvider(corsOptions);

            // Act
            var actualPolicy = await policyProvider.GetPolicyAsync(new DefaultHttpContext(), policyName);

            // Assert
            Assert.Same(policy, actualPolicy);
        }
Пример #5
0
        public async Task UsesTheDefaultPolicyName()
        {
            // Arrange
            var options = new CorsOptions();
            var policy  = new CorsPolicy();

            options.AddPolicy(options.DefaultPolicyName, policy);

            var corsOptions = new TestCorsOptions
            {
                Value = options
            };
            var policyProvider = new DefaultCorsPolicyProvider(corsOptions);

            // Act
            var actualPolicy = await policyProvider.GetPolicyAsync(new DefaultHttpContext(), policyName : null);

            // Assert
            Assert.Same(policy, actualPolicy);
        }
Пример #6
0
        public void AddDefaultPolicy_UsingPolicyBuilder_SetsDefaultPolicyName()
        {
            // Arrange
            var        corsOptions    = new CorsOptions();
            CorsPolicy expectedPolicy = null;

            // Act
            corsOptions.AddPolicy("policy1", policyBuilder =>
            {
                policyBuilder.AllowAnyOrigin().Build();
            });
            corsOptions.AddDefaultPolicy(policyBuilder =>
            {
                expectedPolicy = policyBuilder.AllowAnyOrigin().Build();
            });
            corsOptions.AddPolicy("policy3", new CorsPolicy());

            // Assert
            var actualPolicy = corsOptions.GetPolicy(corsOptions.DefaultPolicyName);

            Assert.Same(expectedPolicy, actualPolicy);
        }
Пример #7
0
 private static CorsService GetCorsService(CorsOptions options = null)
 {
     options = options ?? new CorsOptions();
     return(new CorsService(Options.Create(options), NullLoggerFactory.Instance));
 }
Пример #8
0
 /// <summary>
 /// Creates a new instance of <see cref="DefaultCorsPolicyProvider"/>.
 /// </summary>
 /// <param name="options">The options configured for the application.</param>
 public DefaultCorsPolicyProvider(IOptions <CorsOptions> options)
 {
     _options = options.Value;
 }