AddRule() public method

Adds the rule to the Stump.
public AddRule ( IStumpRule rule ) : void
rule IStumpRule The rule to add to the Stump.
return void
Example #1
0
        public void IsMatch_WithFailingRule_TriesAllRulesReturnsFalse()
        {
            var stump = new Stump("ABC");

            var context = Substitute.For <IStumpsHttpContext>();
            var request = Substitute.For <IStumpsHttpRequest>();

            context.Request.Returns(request);

            var rule1 = Substitute.For <IStumpRule>();

            rule1.IsMatch(request).Returns(true);

            var rule2 = Substitute.For <IStumpRule>();

            rule2.IsMatch(request).Returns(false);

            stump.AddRule(rule1);
            stump.AddRule(rule2);

            stump.Responds();

            var matches = stump.IsMatch(context);

            rule1.Received(1).IsMatch(request);
            rule2.Received(1).IsMatch(request);
            Assert.IsFalse(matches);
        }
Example #2
0
        /// <summary>
        ///     Requires the incoming HTTP request to match the specified HTTP method.
        /// </summary>
        /// <param name="stump">The <see cref="Stump"/> intercepting incoming HTTP requests.</param>
        /// <param name="httpMethod">The HTTP method to match.</param>
        /// <returns>The calling <see cref="Stump"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="stump"/> is <c>null</c>.</exception>
        public static Stump MatchingMethod(this Stump stump, string httpMethod)
        {
            stump = stump ?? throw new ArgumentNullException(nameof(stump));

            stump.AddRule(new HttpMethodRule(httpMethod));
            return(stump);
        }
Example #3
0
        /// <summary>
        ///     Requires the incoming HTTP request to match the specified header.
        /// </summary>
        /// <param name="stump">The <see cref="Stump"/> intercepting incoming HTTP requests.</param>
        /// <param name="headerName">The name of the header to match.</param>
        /// <param name="headerValue">The value of the header to match.</param>
        /// <returns>The calling <see cref="Stump"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="stump"/> is <c>null</c>.</exception>
        public static Stump MatchingHeader(this Stump stump, string headerName, string headerValue)
        {
            stump = stump ?? throw new ArgumentNullException(nameof(stump));

            stump.AddRule(new HeaderRule(headerName, headerValue));
            return(stump);
        }
Example #4
0
        /// <summary>
        ///     Requires the incoming HTTP request to contain the specified text in the body.
        /// </summary>
        /// <param name="stump">The <see cref="Stump"/> intercepting incoming HTTP requests.</param>
        /// <param name="text">The text that must be contained within the body.</param>
        /// <returns>The calling <see cref="Stump"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="stump"/> is <c>null</c>.</exception>
        public static Stump MatchingBodyContaining(this Stump stump, string[] text)
        {
            stump = stump ?? throw new ArgumentNullException(nameof(stump));

            stump.AddRule(new BodyContentRule(text));
            return(stump);
        }
Example #5
0
        public static Stump MatchingUrl(this Stump stump, string url)
        {
            stump = stump ?? throw new ArgumentNullException(nameof(stump));

            stump.AddRule(new UrlRule(url));
            return(stump);
        }
Example #6
0
        /// <summary>
        ///     Requires the incoming HTTP request to match the specified <see cref="IStumpRule"/>.
        /// </summary>
        /// <param name="stump">The <see cref="Stump"/> intercepting incoming HTTP requests.</param>
        /// <param name="rule">The <see cref="IStumpRule"/> required to match.</param>
        /// <returns>The calling <see cref="Stump"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="stump"/> is <c>null</c>.</exception>
        public static Stump MatchingRule(this Stump stump, IStumpRule rule)
        {
            stump = stump ?? throw new ArgumentNullException(nameof(stump));

            stump.AddRule(rule);
            return(stump);
        }
Example #7
0
        public void AddRule_WithRule_AddedToCollection()
        {
            var stump = new Stump("ABC");

            stump.AddRule(Substitute.For <IStumpRule>());
            Assert.AreEqual(1, stump.Count);
        }
Example #8
0
        /// <summary>
        ///     Defines the entry point of the application.
        /// </summary>
        /// <param name="args">The command-line arguments.</param>
        public static void Main(string[] args)
        {
            ConsoleHelper.ApplicationBanner("Hello World API");

            // Create a new Stumps Server
            var server = new StumpsServer();

            // An open port will be chosen automatically unless specified
            // server.ListensOnPort = 9100;

            // Create a new Stump for the server
            var stump = new Stump("HelloWorldStump");

            // Add two rules that stumps out HTTP GET requests for the url /HeloWorld.htm
            stump.AddRule(new HttpMethodRule("GET"));
            stump.AddRule(new UrlRule("/HelloWorld.htm"));

            // Create a response for the rule
            var response = new BasicHttpResponse();
            response.Headers["Content-Type"] = "text/html;charset=UTF-8";
            response.AppendToBody(
                "<html><header><title>Stumps Hello World</title></header><body><p>Hello From Stumps</p></body></html>");

            // Add the response to the stump
            stump.Response = response;

            // Add the stump to the server
            server.AddStump(stump);

            // Show the requests that are incomming
            server.RequestProcessed += (o, e) => ConsoleHelper.ShowHttpResponse(server, e);

            // Start the server and wait!
            server.Start();

            // Show the URL to the user
            Console.WriteLine("Browse to http://localhost:{0}/HelloWorld.htm", server.ListeningPort);
            Console.WriteLine();

            // Wait to exit
            ConsoleHelper.WaitForExit();

            server.Shutdown();
            server.Dispose();
        }
Example #9
0
        /// <summary>
        ///     Requires the incoming HTTP request to match the specified body.
        /// </summary>
        /// <param name="stump">The <see cref="Stump"/> intercepting incoming HTTP requests.</param>
        /// <param name="buffer">The array of bytes for the body.</param>
        /// <returns>The calling <see cref="Stump"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="stump"/> is <c>null</c>.</exception>
        public static Stump MatchingBody(this Stump stump, byte[] buffer)
        {
            stump = stump ?? throw new ArgumentNullException(nameof(stump));

            if (buffer != null)
            {
                stump.AddRule(new BodyMatchRule(buffer.Length, CreateMd5Hash(buffer)));
            }

            return(stump);
        }
Example #10
0
        public void IsMatch_WithNullResponse_ReturnsFalse()
        {
            var stump = new Stump("ABC");

            var rule1 = Substitute.For <IStumpRule>();

            rule1.IsMatch(null).Returns(true);

            stump.AddRule(rule1);

            Assert.IsFalse(stump.IsMatch(Substitute.For <IStumpsHttpContext>()));
        }
Example #11
0
        public void IsMatch_WithFailingRule_TriesAllRulesReturnsFalse()
        {
            var stump = new Stump("ABC");

            var context = Substitute.For<IStumpsHttpContext>();
            var request = Substitute.For<IStumpsHttpRequest>();
            context.Request.Returns(request);

            var rule1 = Substitute.For<IStumpRule>();
            rule1.IsMatch(request).Returns(true);

            var rule2 = Substitute.For<IStumpRule>();
            rule2.IsMatch(request).Returns(false);

            stump.AddRule(rule1);
            stump.AddRule(rule2);

            stump.Response = new BasicHttpResponse();

            var matches = stump.IsMatch(context);
            rule1.Received(1).IsMatch(request);
            rule2.Received(1).IsMatch(request);
            Assert.IsFalse(matches);
        }
Example #12
0
 public void AddRule_WithRule_AddedToCollection()
 {
     var stump = new Stump("ABC");
     stump.AddRule(Substitute.For<IStumpRule>());
     Assert.AreEqual(1, stump.Count);
 }
Example #13
0
        public void IsMatch_WithNullResponse_ReturnsFalse()
        {
            var stump = new Stump("ABC");

            var rule1 = Substitute.For<IStumpRule>();
            rule1.IsMatch(null).Returns(true);

            stump.AddRule(rule1);

            Assert.IsFalse(stump.IsMatch(Substitute.For<IStumpsHttpContext>()));
        }