/// <summary>
        /// Create a <see cref="Microsoft.AspNetCore.TestHost.RequestBuilder"/> configured automatically
        /// with the generated uri from the <paramref name="actionSelector"/>.
        /// At this moment this method only resolver HTTP API using ASP.NET CORE Attribute Routing
        /// </summary>
        /// <typeparam name="TController">The controller to use</typeparam>
        /// <param name="server">The TestServer</param>
        /// <param name="actionSelector">The action selector used to discover the uri</param>
        /// <param name="tokenValues">The optional token values used to create the uri</param>
        /// <returns></returns>
        public static RequestBuilder CreateHttpApiRequest <TController>(this TestServer server,
                                                                        Expression <Func <TController, object> > actionSelector,
                                                                        object tokenValues = null)
            where TController : class
        {
            if (!IsController <TController>())
            {
                throw new InvalidOperationException($"The type {typeof(TController).FullName} is not a valid MVC controller.");
            }

            if (actionSelector == null)
            {
                throw new ArgumentNullException(nameof(actionSelector));
            }

            var action = GetTestServerAction(actionSelector);

            if (!IsValidActionMethod(action.MethodInfo))
            {
                throw new InvalidOperationException($"The action selector is not a valid action for MVC Controller.");
            }

            //the uri discover use only attribute route conventions..

            var validUri = UriDiscover.Discover <TController>(
                action, tokenValues);

            return(server.CreateRequest(validUri.ToLowerInvariant()));
        }
Exemple #2
0
        /// <summary>
        /// Create a <see cref="Microsoft.AspNetCore.TestHost.RequestBuilder"/> configured automatically
        /// with the generated uri from the <paramref name="actionSelector"/>.
        /// At this moment this method only resolver HTTP API using ASP.NET CORE Attribute Routing
        /// </summary>
        /// <typeparam name="TController">The controller to use</typeparam>
        /// <param name="server">The TestServer</param>
        /// <param name="actionSelector">The action selector used to discover the uri</param>
        /// <param name="tokenValues">The optional token values used to create the uri</param>
        /// <param name="contentOptions">Determines if [FromBody] arguments are included as request content.
        ///      By default they are included as application/json content</param>
        /// <returns></returns>
        public static RequestBuilder CreateHttpApiRequest <TController>(this TestServer server,
                                                                        Expression <Func <TController, object> > actionSelector,
                                                                        object tokenValues = null,
                                                                        RequestContentOptions contentOptions = null)
            where TController : class
        {
            if (!IsController <TController>())
            {
                throw new InvalidOperationException($"The type {typeof(TController).FullName} is not a valid MVC controller.");
            }

            if (actionSelector == null)
            {
                throw new ArgumentNullException(nameof(actionSelector));
            }

            // Include content as Json by default
            contentOptions = contentOptions ?? new IncludeContentAsJson();

            var action = GetTestServerAction(actionSelector);

            if (!IsValidActionMethod(action.MethodInfo))
            {
                throw new InvalidOperationException($"The action selector is not a valid action for MVC Controller.");
            }

            //the uri discover use only attribute route conventions..

            var validUri = UriDiscover.Discover <TController>(
                action, tokenValues);

            var requestBuilder = server.CreateRequest(validUri);

            if (contentOptions.IncludeFromBodyAsContent)
            {
                AddFromBodyArgumentsToRequestBody(requestBuilder, action, contentOptions);
            }

            if (contentOptions.IncludeFromFormAsContent)
            {
                AddFromFormArgumentsToRequestForm(requestBuilder, action, contentOptions);
            }

            AddFromHeaderArgumentsToRequestForm(requestBuilder, action);

            return(requestBuilder);
        }