/// <summary>
        /// Writes the content as a response using <see cref="Encoding.UTF8"/>.
        /// </summary>
        /// <param name="action"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static MockRequestAction ApplyContent(this MockRequestAction action, string content)
        {
            ParamAssert.NotNull(action, nameof(action));
            ParamAssert.NotNull(content, nameof(content));

            return(action.Apply(async ctx => await ctx.Response.WriteAsync(content, ctx.RequestAborted)));
        }
Example #2
0
        /// <summary>
        /// Matches the <see cref="HttpRequest.Method"/> with the provided <see cref="HttpMethod"/>,
        /// returning true if they are the equal.
        /// </summary>
        /// <param name="filter"></param>
        /// <param name="method"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static MockRequestFilter WhenMethod(this MockRequestFilter filter, HttpMethod method)
        {
            ParamAssert.NotNull(filter, nameof(filter));
            ParamAssert.NotNull(method, nameof(method));

            return(filter.When(ctx => ctx.Request.Method == method.Method));
        }
Example #3
0
        /// <summary>
        /// Matches the <see cref="HttpRequest.Path"/> with the provided <see cref="Regex"/>,
        /// returning true if they are a match.
        /// </summary>
        /// <param name="filter"></param>
        /// <param name="other"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static MockRequestFilter WhenPathIsMatch(this MockRequestFilter filter, Regex other)
        {
            ParamAssert.NotNull(filter, nameof(filter));
            ParamAssert.NotNull(other, nameof(other));

            return(filter.When(ctx => other.IsMatch(ctx.Request.Path)));
        }
Example #4
0
        /// <summary>
        /// Matches the <see cref="HttpRequest.Path"/> with the provided <see cref="PathString"/>,
        /// returning true if it contains the second one.
        /// </summary>
        /// <param name="filter"></param>
        /// <param name="other"></param>
        /// <param name="comparison"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static MockRequestFilter WhenPathContains(this MockRequestFilter filter,
                                                         PathString other, StringComparison comparison = StringComparison.OrdinalIgnoreCase)
        {
            ParamAssert.NotNull(filter, nameof(filter));

            return(filter.When(ctx => ctx.Request.Path.Value.IndexOf(other, comparison) >= 0));
        }
Example #5
0
        /// <summary>
        /// Matches the <see cref="HttpRequest.Path"/> with the provided <see cref="PathString"/>,
        /// returning true if the beginning is the same.
        /// </summary>
        /// <param name="filter"></param>
        /// <param name="other"></param>
        /// <param name="comparison"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static MockRequestFilter WhenPathStartsWithSegments(this MockRequestFilter filter,
                                                                   PathString other, StringComparison comparison = StringComparison.OrdinalIgnoreCase)
        {
            ParamAssert.NotNull(filter, nameof(filter));

            return(filter.When(ctx => ctx.Request.Path.StartsWithSegments(other, comparison)));
        }
Example #6
0
        /// <summary>
        /// Adds a predicate to filter a given request
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public MockRequestFilter When(Func <HttpContext, bool> predicate)
        {
            ParamAssert.NotNull(predicate, nameof(predicate));

            _predicates.Add(predicate);

            return(this);
        }
Example #7
0
        /// <summary>
        /// Matches the <see cref="HttpRequest.Method"/> with the provided <see cref="HttpMethod"/> collection,
        /// returning true if any of them are equal.
        /// </summary>
        /// <param name="filter"></param>
        /// <param name="methods"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentException"></exception>
        public static MockRequestFilter WhenMethod(this MockRequestFilter filter, params HttpMethod[] methods)
        {
            ParamAssert.NotNull(filter, nameof(filter));
            ParamAssert.NotNull(methods, nameof(methods));
            ParamAssert.NotEmpty(methods, nameof(methods));

            return(filter.When(ctx => methods.Any(method => ctx.Request.Method == method.Method)));
        }
Example #8
0
        /// <summary>
        /// Adds an handler to create a response
        /// </summary>
        /// <param name="handler"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public MockRequestAction Apply(Func <HttpContext, Task> handler)
        {
            ParamAssert.NotNull(handler, nameof(handler));

            _handlers.Add(handler);

            return(this);
        }
        /// <summary>
        /// Sets the response status code
        /// </summary>
        /// <param name="action"></param>
        /// <param name="statusCode"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static MockRequestAction ApplyStatusCode(this MockRequestAction action, int statusCode)
        {
            ParamAssert.NotNull(action, nameof(action));

            return(action.Apply(ctx =>
            {
                ctx.Response.StatusCode = statusCode;
                return Task.CompletedTask;
            }));
        }