Exemplo n.º 1
0
        /// <summary>
        ///   Performs the tasks needed to configure Web API for use with the Order Fulfillment
        ///   API offerings.
        /// </summary>
        ///
        /// <param name="config">The HTTP configuration to be used with Web API.</param>
        ///
        internal static void ConfigureWebApi(HttpConfiguration config)
        {
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            // Register any route constraints that have been discovered.

            var constraintLocator  = new RouteConstraintLocator();
            var constraintResolver = new DefaultInlineConstraintResolver();

            foreach (var constraintPair in constraintLocator.DiscoveredConstraints)
            {
                constraintResolver.ConstraintMap.Add(constraintPair.Key, constraintPair.Value);
            }

            // Map the API routes that were defined using attributes, using the discovered constraints.

            config.MapHttpAttributeRoutes(constraintResolver);

            // Configure the JSON serializer to use enumeration names (instead of index) and adhere to
            // camel casing for serialized members.

            var jsonFormatter = config.Formatters.JsonFormatter;

            jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
            jsonFormatter.SerializerSettings.Converters.Add(new StringEnumConverter());

            // Clear all of the other formatters, supporting only JSON.

            config.Formatters.Clear();
            config.Formatters.Add(jsonFormatter);
        }
        public void LocatorFindsConstraints()
        {
            var locator = new RouteConstraintLocator(Assembly.GetExecutingAssembly());

            locator.DiscoveredConstraints.Should().NotBeNull("because there are classes decorated with constraints");
            locator.DiscoveredConstraints.Count.Should().BeGreaterOrEqualTo(1, "because at least one class was decorated with constraints");
        }
        public void LocatorAllowsMultipleDecorators()
        {
            var locator = new RouteConstraintLocator(Assembly.GetExecutingAssembly());

            locator.DiscoveredConstraints.Where(pair => ((pair.Key == "C") || (pair.Key == "D")))
            .Select(pair => pair.Value.Name)
            .Distinct()
            .Should().HaveCount(1, "because TestConstraintC should handle two constraints");
        }
        public void LocatorCorrectlyAssociatesConstraints()
        {
            var locator = new RouteConstraintLocator(Assembly.GetExecutingAssembly());

            foreach (var constraintPair in locator.DiscoveredConstraints.Where(pair => pair.Key != "D"))
            {
                constraintPair.Value.Name.EndsWith(constraintPair.Key).Should().BeTrue("because the constraint name should be associated with the decorated class");
            }
        }