A class that represents a Stump.
Esempio n. 1
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);
        }
Esempio n. 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);
        }
Esempio n. 3
0
        public void AddRule_WithRule_AddedToCollection()
        {
            var stump = new Stump("ABC");

            stump.AddRule(Substitute.For <IStumpRule>());
            Assert.AreEqual(1, stump.Count);
        }
Esempio n. 4
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);
        }
Esempio n. 5
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);
        }
Esempio n. 6
0
        public static Stump MatchingUrl(this Stump stump, string url)
        {
            stump = stump ?? throw new ArgumentNullException(nameof(stump));

            stump.AddRule(new UrlRule(url));
            return(stump);
        }
Esempio n. 7
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);
        }
Esempio n. 8
0
        public void IsMatch_WithoutRules_ReturnsFalse()
        {
            var stump = new Stump("ABC");

            stump.Responds();

            Assert.IsFalse(stump.IsMatch(Substitute.For <IStumpsHttpContext>()));
        }
Esempio n. 9
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));

            var textArray = new[] { text };

            var stumpResponse = stump.MatchingBodyContaining(textArray);

            return(stumpResponse);
        }
Esempio n. 10
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);
        }
Esempio n. 11
0
        /// <summary>
        /// Respondswithes the multiple.
        /// </summary>
        /// <param name="stump">The <see cref="Stump"/> intercepting incoming HTTP requests.</param>
        /// <param name="behavior">The behavior of the <see cref="StumpResponseFactory.CreateResponse(IStumpsHttpRequest)"/> method when retrieving the next <see cref="IStumpsHttpResponse"/>.</param>
        /// <returns>The calling <see cref="Stump"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="stump"/> is <c>null</c>.</exception>
        public static Stump ReturnsMultipleResponses(this Stump stump, ResponseFactoryBehavior behavior)
        {
            stump = stump ?? throw new ArgumentNullException(nameof(stump));

            if (stump.Responses == null || stump.Responses.Behavior != behavior)
            {
                stump.Responses = new StumpResponseFactory(behavior);
            }

            return(stump);
        }
Esempio n. 12
0
        public void ResposeFactory_GetSet_ReturnsResponseFactory()
        {
            var responseFactory = new StumpResponseFactory();

            var stump = new Stump("ABC")
            {
                Responses = responseFactory
            };

            Assert.AreEqual(responseFactory, stump.Responses);
        }
Esempio n. 13
0
        /// <summary>
        ///     Asserts that the <see cref="Stump"/> will respond with a <see cref="BasicHttpResponse"/>.
        /// </summary>
        /// <param name="stump">The <see cref="Stump"/> intercepting incoming HTTP requests.</param>
        /// <returns>A <see cref="BasicHttpResponse"/> created for the <paramref name="stump"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="stump"/> is <c>null</c>.</exception>
        public static BasicHttpResponse Responds(this Stump stump)
        {
            stump = stump ?? throw new ArgumentNullException(nameof(stump));

            stump.Responses = stump.Responses ?? new StumpResponseFactory();

            var response = new BasicHttpResponse();

            stump.Responses.Add(response);

            return(response);
        }
Esempio n. 14
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>()));
        }
Esempio n. 15
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();
        }
Esempio n. 16
0
        public void IsMatch_WithResponseFactoryNoResponse_ReturnsFalse()
        {
            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 matches = stump.IsMatch(context);

            rule1.DidNotReceive().IsMatch(request);
            Assert.IsFalse(matches);
        }
Esempio n. 17
0
        /// <summary>
        ///     Adds a new <see cref="T:Stumps.Stump" /> to the collection.
        /// </summary>
        /// <param name="stump">The <see cref="T:Stumps.Stump" /> to add to the collection.</param>
        /// <exception cref="System.ArgumentNullException"><paramref name="stump"/> is <c>null</c>.</exception>
        /// <exception cref="System.ArgumentException">A <see cref="T:Stumps.Stump" /> with the same identifier already exists.</exception>
        public void AddStump(Stump stump)
        {
            if (stump == null)
            {
                throw new ArgumentNullException("stump");
            }

            if (_stumpReference.ContainsKey(stump.StumpId))
            {
                throw new ArgumentException(BaseResources.StumpAlreadyExistsError, "stump");
            }

            _lock.EnterWriteLock();

            _stumpList.Add(stump);
            _stumpReference.Add(stump.StumpId, stump);

            _lock.ExitWriteLock();
        }
Esempio n. 18
0
        public void IsMatch_WithMultipleMatchingRules_TriesAllRulesReturnsTrue()
        {
            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(true);

            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.IsTrue(matches);
        }
Esempio n. 19
0
 public void Constuctor_WithValidId_UpdatesProperty()
 {
     var stump = new Stump("ABC");
     Assert.AreEqual("ABC", stump.StumpId);
 }
Esempio n. 20
0
        /// <summary>
        ///     Populates the response of the HTTP context from the Stump.
        /// </summary>
        /// <param name="incommingHttpContext">The incomming HTTP context.</param>
        /// <param name="stump">The <see cref="T:Stumps.Stump"/> used to populate the response.</param>
        private void PopulateResponse(IStumpsHttpContext incommingHttpContext, Stump stump)
        {

            // Write the status code information
            incommingHttpContext.Response.StatusCode = stump.Response.StatusCode;
            incommingHttpContext.Response.StatusDescription = stump.Response.StatusDescription;

            // Write the headers
            incommingHttpContext.Response.Headers.Clear();

            stump.Response.Headers.CopyTo(incommingHttpContext.Response.Headers);

            // Write the body
            incommingHttpContext.Response.ClearBody();
            
            if (stump.Response.BodyLength > 0)
            {
                var buffer = stump.Response.GetBody();
                if (stump.Response.Headers["Content-Encoding"] != null)
                {
                    var encoder = new ContentEncoder(stump.Response.Headers["Content-Encoding"]);
                    buffer = encoder.Encode(buffer);
                }

                incommingHttpContext.Response.AppendToBody(buffer);
            }

        }
Esempio n. 21
0
 public void AddRule_WithRule_AddedToCollection()
 {
     var stump = new Stump("ABC");
     stump.AddRule(Substitute.For<IStumpRule>());
     Assert.AreEqual(1, stump.Count);
 }
Esempio n. 22
0
 /// <summary>
 ///     Adds a new <see cref="T:Stumps.Stump" /> with a specified identifier to the collection.
 /// </summary>
 /// <param name="stumpId">The unique identifier for the <see cref="T:Stumps.Stump" />.</param>
 /// <returns>A new <see cref="T:Stumps.Stump"/> with the specified <paramref name="stumpId"/>.</returns>
 /// <exception cref="System.ArgumentNullException"><paramref name="stumpId"/> is <c>null</c>.</exception>
 /// <exception cref="System.ArgumentException">A <see cref="T:Stumps.Stump" /> with the same identifier already exists.</exception>
 public Stump AddNewStump(string stumpId)
 {
     var stump = new Stump(stumpId);
     _stumpsManager.AddStump(stump);
     return stump;
 }
Esempio n. 23
0
 /// <summary>
 ///     Adds a new <see cref="T:Stumps.Stump" /> to the collection.
 /// </summary>
 /// <param name="stump">The <see cref="T:Stumps.Stump" /> to add to the collection.</param>
 /// <exception cref="System.ArgumentNullException"><paramref name="stump"/> is <c>null</c>.</exception>
 /// <exception cref="System.ArgumentException">A <see cref="T:Stumps.Stump" /> with the same identifier already exists.</exception>
 public void AddStump(Stump stump)
 {
     _stumpsManager.AddStump(stump);
 }
Esempio n. 24
0
        public void IsMatch_WithoutRules_ReturnsFalse()
        {
            var stump = new Stump("ABC");
            stump.Response = new BasicHttpResponse();

            Assert.IsFalse(stump.IsMatch(Substitute.For<IStumpsHttpContext>()));
        }
Esempio n. 25
0
 public void Respose_GetSet_ReturnsResponse()
 {
     var response = new BasicHttpResponse();
     var stump = new Stump("ABC");
     stump.Response = response;
     Assert.AreEqual(response, stump.Response);
 }
Esempio n. 26
0
        public void Constuctor_WithValidId_UpdatesProperty()
        {
            var stump = new Stump("ABC");

            Assert.AreEqual("ABC", stump.StumpId);
        }
Esempio n. 27
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>()));
        }