A class representing a simple and basic HTTP response.
Inheritance: IStumpsHttpResponse
        /// <summary>
        ///     Specifies a header returned as part of the HTTP response.
        /// </summary>
        /// <param name="response">The <see cref="BasicHttpResponse"/> that returns in response to an HTTP request.</param>
        /// <param name="headerName">The name of the header.</param>
        /// <param name="headerValue">The value of the header.</param>
        /// <returns>The calling <see cref="BasicHttpResponse"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="response"/> is <c>null</c>.</exception>
        public static BasicHttpResponse WithHeader(this BasicHttpResponse response, string headerName, string headerValue)
        {
            response = response ?? throw new ArgumentNullException(nameof(response));

            response.Headers[headerName] = headerValue;
            return(response);
        }
        /// <summary>
        ///     Specifies the description of the status code returned as part of the HTTP response.
        /// </summary>
        /// <param name="response">The <see cref="BasicHttpResponse"/> that returns in response to an HTTP request.</param>
        /// <param name="statusDescription">The description of the status code.</param>
        /// <returns>The calling <see cref="BasicHttpResponse"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="response"/> is <c>null</c>.</exception>
        public static BasicHttpResponse WithStatusDescription(this BasicHttpResponse response, string statusDescription)
        {
            response = response ?? throw new ArgumentNullException(nameof(response));

            response.StatusDescription = statusDescription;
            return(response);
        }
        /// <summary>
        ///     Specifies the status code returned as part of the HTTP response.
        /// </summary>
        /// <param name="response">The <see cref="BasicHttpResponse"/> that returns in response to an HTTP request.</param>
        /// <param name="statusCode">The status code to return.</param>
        /// <returns>The calling <see cref="BasicHttpResponse"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="response"/> is <c>null</c>.</exception>
        public static BasicHttpResponse WithStatusCode(this BasicHttpResponse response, int statusCode)
        {
            response = response ?? throw new ArgumentNullException(nameof(response));

            response.StatusCode = statusCode;
            return(response);
        }
        /// <summary>
        ///     Specifies the redirect address returned as part of the HTTP response.
        /// </summary>
        /// <param name="response">The <see cref="BasicHttpResponse"/> that returns in response to an HTTP request.</param>
        /// <param name="redirectAddress">The redirect address returned as part of the HTTP response.</param>
        /// <returns>The calling <see cref="BasicHttpResponse"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="response"/> is <c>null</c>.</exception>
        public static BasicHttpResponse WithRedirectAddress(this BasicHttpResponse response, string redirectAddress)
        {
            response = response ?? throw new ArgumentNullException(nameof(response));

            response.RedirectAddress = redirectAddress;
            return(response);
        }
        /// <summary>
        ///     Specifies the amount of time the server delays before responding with the response.
        /// </summary>
        /// <param name="response">The <see cref="BasicHttpResponse"/> that should be delayed.</param>
        /// <param name="delayMilliseconds">The amount of time, in milliseconds, the response is delayed.</param>
        /// <returns>The calling <see cref="BasicHttpResponse"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="response"/> is <c>null</c>.</exception>
        public static BasicHttpResponse DelayedBy(this BasicHttpResponse response, int delayMilliseconds)
        {
            response = response ?? throw new ArgumentNullException(nameof(response));

            response.ResponseDelay = delayMilliseconds;
            return(response);
        }
Beispiel #6
0
        public void Constructor_Default_SetsTo200Ok()
        {
            var response = new BasicHttpResponse();

            Assert.AreEqual(200, response.StatusCode);
            Assert.AreEqual("OK", response.StatusDescription);
        }
        public void Add_WithTightThread_IsReasonablyWriteSafe()
        {
            const int ItemCount = 1000;

            var factory = new StumpResponseFactory(ResponseFactoryBehavior.OrderedInfinite);

            var t = new Thread(() =>
            {
                Parallel.For(0, ItemCount, (i) =>
                {
                    var response = new BasicHttpResponse()
                    {
                        StatusCode = i
                    };

                    factory.Add(response);
                });
            });

            t.Start();

            Assert.IsTrue(t.Join(5000));

            if (t.IsAlive)
            {
                t.Abort();
            }

            Assert.AreEqual(ItemCount, factory.Count);
        }
Beispiel #8
0
        public void AppendToBody_WithStringAndNullEncoding_ThrowsException()
        {
            var response = new BasicHttpResponse();

            Assert.That(
                () => response.AppendToBody(string.Empty, null),
                Throws.Exception.TypeOf <ArgumentNullException>().With.Property("ParamName").EqualTo("encoding"));
        }
        /// <summary>
        ///     Asserts that the <see cref="Stump"/> will drop the connection immediately.
        /// </summary>
        /// <param name="response">The <see cref="BasicHttpResponse"/> returned which should drop the connection.</param>
        /// <returns>The calling <see cref="BasicHttpResponse"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="response"/> is <c>null</c>.</exception>
        public static BasicHttpResponse ByDroppingTheConnection(this BasicHttpResponse response)
        {
            response = response ?? throw new ArgumentNullException(nameof(response));

            response.TerminateConnection = true;

            return(response);
        }
        public void AppendToBody_WithStringAndEncoding_UpdatesBody()
        {
            var expected = "ABCD";
            var expectedLength = expected.Length * 2;

            var response = new BasicHttpResponse();
            response.AppendToBody(expected, Encoding.Unicode);
            Assert.AreEqual(expectedLength, response.BodyLength);
        }
        /// <summary>
        /// Specifies the body returned as part of the HTTP response.
        /// </summary>
        /// <param name="response">The <see cref="BasicHttpResponse" /> that returns in response to an HTTP request.</param>
        /// <param name="body">The value returned as body of the HTTP response.</param>
        /// <returns>The calling <see cref="BasicHttpResponse"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="response"/> is <c>null</c>.</exception>
        public static BasicHttpResponse WithBody(this BasicHttpResponse response, string body)
        {
            response = response ?? throw new ArgumentNullException(nameof(response));

            response.ClearBody();
            response.AppendToBody(body);

            return(response);
        }
Beispiel #12
0
        public void AppendToBody_WithString_UpdatesBody()
        {
            var expected = "ABCD";

            var response = new BasicHttpResponse();

            response.AppendToBody(expected);
            Assert.AreEqual(expected.Length, response.BodyLength);
        }
Beispiel #13
0
        /// <summary>
        ///     Asserts that the <see cref="StumpResponseFactory"/> will respond with a <see cref="BasicHttpResponse"/>.
        /// </summary>
        /// <param name="responseFactory">The <see cref="StumpResponseFactory"/> to which the new <see cref="BasicHttpResponse"/> is added.</param>
        /// <returns>A <see cref="BasicHttpResponse"/> created for the <paramref name="responseFactory"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="responseFactory"/> is <c>null</c>.</exception>
        public static BasicHttpResponse Responds(this StumpResponseFactory responseFactory)
        {
            responseFactory = responseFactory ?? throw new ArgumentNullException(nameof(responseFactory));

            var response = new BasicHttpResponse();

            responseFactory.Add(response);

            return(response);
        }
Beispiel #14
0
        public void GetBody_WithNullEncoding_ThrowsException()
        {
            var response = new BasicHttpResponse();

            response.AppendToBody("ABCD");

            Assert.That(
                () => response.GetBodyAsString(null),
                Throws.Exception.TypeOf <ArgumentNullException>().With.Property("ParamName").EqualTo("encoding"));
        }
        public void AppendToBody_WithBytes_UpdatesBody()
        {
            var newBytes = new byte[5]
            {
                1, 2, 3, 4, 5
            };

            var response = new BasicHttpResponse();
            response.AppendToBody(newBytes);
            Assert.AreEqual(5, response.BodyLength);
        }
Beispiel #16
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);
        }
        public void AppendToBody_WithAdditionalString_AppendsToEnd()
        {
            var newString = "ABCDE";

            var expectedString = "ABCDEABCDE";

            var response = new BasicHttpResponse();
            response.AppendToBody(newString);
            response.AppendToBody(newString);
            Assert.AreEqual(10, response.BodyLength);
            CollectionAssert.AreEqual(expectedString, response.GetBodyAsString());
        }
Beispiel #18
0
        public void AppendToBody_WithBytes_UpdatesBody()
        {
            var newBytes = new byte[5]
            {
                1, 2, 3, 4, 5
            };

            var response = new BasicHttpResponse();

            response.AppendToBody(newBytes);
            Assert.AreEqual(5, response.BodyLength);
        }
        private void PopulateFactoryWithMultipleResponses(StumpResponseFactory factory, int count)
        {
            for (var i = 1; i <= count; i++)
            {
                var response = new BasicHttpResponse()
                {
                    StatusCode = i
                };

                factory.Add(response);
            }
        }
Beispiel #20
0
        public void AppendToBody_WithAdditionalString_AppendsToEnd()
        {
            var newString = "ABCDE";

            var expectedString = "ABCDEABCDE";

            var response = new BasicHttpResponse();

            response.AppendToBody(newString);
            response.AppendToBody(newString);
            Assert.AreEqual(10, response.BodyLength);
            CollectionAssert.AreEqual(expectedString, response.GetBodyAsString());
        }
        /// <summary>
        ///     Asserts that the <see cref="T:Stumps.Stump"/> will drop the connection immediately.
        /// </summary>
        /// <param name="stump">The <see cref="T:Stumps.Stump"/> intercepting incomming HTTP requests.</param>
        /// <returns>The calling <see cref="T:Stumps.Stump"/>.</returns>
        /// <exception cref="System.ArgumentNullException"><paramref name="stump"/> is <c>null</c>.</exception>
        public static Stump DropsConnection(this Stump stump)
        {
            if (stump == null)
            {
                throw new ArgumentNullException("stump");
            }

            stump.TerminateConnection = true;

            var response = new BasicHttpResponse();
            stump.Response = response;

            return stump;
        }
        /// <summary>
        ///     Specifies the body returned as part of the HTTP response.
        /// </summary>
        /// <param name="response">The <see cref="BasicHttpResponse"/> that returns in response to an HTTP request.</param>
        /// <param name="path">The path to the file that contains the HTTP response.</param>
        /// <returns>The calling <see cref="BasicHttpResponse"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="response"/> is <c>null</c>.</exception>
        public static BasicHttpResponse WithFile(this BasicHttpResponse response, string path)
        {
            response = response ?? throw new ArgumentNullException(nameof(response));

            var buffer = File.ReadAllBytes(path);

            response.ClearBody();
            response.AppendToBody(buffer);

            var mimeType = MimeMapping.GetMimeMapping(path);

            response.Headers["Content-Type"] = mimeType;

            return(response);
        }
Beispiel #23
0
        public void ClearBody_WhenCalled_RemovedAllBytes()
        {
            var newBytes = new byte[5]
            {
                1, 2, 3, 4, 5
            };

            var response = new BasicHttpResponse();

            response.AppendToBody(newBytes);
            response.ClearBody();
            Assert.AreEqual(0, response.BodyLength);
            var bodyBytes = response.GetBody();

            Assert.IsNotNull(bodyBytes);
            Assert.AreEqual(0, bodyBytes.Length);
        }
Beispiel #24
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();
        }
        public void AppendToBody_WithAdditionalBytes_AppendsToEnd()
        {
            var newBytes = new byte[5]
            {
                1, 2, 3, 4, 5
            };

            var expected = new byte[10]
            {
                1, 2, 3, 4, 5, 1, 2, 3, 4, 5
            };

            var response = new BasicHttpResponse();
            response.AppendToBody(newBytes);
            response.AppendToBody(newBytes);
            Assert.AreEqual(10, response.BodyLength);
            CollectionAssert.AreEqual(expected, response.GetBody());
        }
Beispiel #26
0
        public void AppendToBody_WithAdditionalBytes_AppendsToEnd()
        {
            var newBytes = new byte[5]
            {
                1, 2, 3, 4, 5
            };

            var expected = new byte[10]
            {
                1, 2, 3, 4, 5, 1, 2, 3, 4, 5
            };

            var response = new BasicHttpResponse();

            response.AppendToBody(newBytes);
            response.AppendToBody(newBytes);
            Assert.AreEqual(10, response.BodyLength);
            CollectionAssert.AreEqual(expected, response.GetBody());
        }
Beispiel #27
0
        public void AppendToBody_WithNullBytes_DoesNotTrowError()
        {
            var response = new BasicHttpResponse();

            Assert.DoesNotThrow(() => response.AppendToBody((byte[])null));
        }
        public void GetBody_WithNullEncoding_ThrowsException()
        {
            var response = new BasicHttpResponse();
            response.AppendToBody("ABCD");

            Assert.That(
                () => response.GetBodyAsString(null),
                Throws.Exception.TypeOf<ArgumentNullException>().With.Property("ParamName").EqualTo("encoding"));
        }
 public void Constructor_Default_SetsTo200Ok()
 {
     var response = new BasicHttpResponse();
     Assert.AreEqual(200, response.StatusCode);
     Assert.AreEqual("OK", response.StatusDescription);
 }
 public void Constructor_Default_InitializesHeader()
 {
     var response = new BasicHttpResponse();
     Assert.IsNotNull(response.Headers);
 }
 public void Constructor_Default_EmptyBody()
 {
     var response = new BasicHttpResponse();
     Assert.AreEqual(0, response.BodyLength);
 }
        public void ClearBody_WhenCalled_RemovedAllBytes()
        {
            var newBytes = new byte[5]
            {
                1, 2, 3, 4, 5
            };

            var response = new BasicHttpResponse();
            response.AppendToBody(newBytes);
            response.ClearBody();
            Assert.AreEqual(0, response.BodyLength);
            var bodyBytes = response.GetBody();
            Assert.IsNotNull(bodyBytes);
            Assert.AreEqual(0, bodyBytes.Length);
        }
Beispiel #33
0
        public void AppendToBody_WithNullString_DoesNotTrowError()
        {
            var response = new BasicHttpResponse();

            Assert.DoesNotThrow(() => response.AppendToBody((string)null));
        }
Beispiel #34
0
        public void Constructor_Default_InitializesHeader()
        {
            var response = new BasicHttpResponse();

            Assert.IsNotNull(response.Headers);
        }
 public void AppendToBody_WithNullBytes_DoesNotTrowError()
 {
     var response = new BasicHttpResponse();
     Assert.DoesNotThrow(() => response.AppendToBody((byte[])null));
 }
Beispiel #36
0
 public void Respose_GetSet_ReturnsResponse()
 {
     var response = new BasicHttpResponse();
     var stump = new Stump("ABC");
     stump.Response = response;
     Assert.AreEqual(response, stump.Response);
 }
 public void AppendToBody_WithNullString_DoesNotTrowError()
 {
     var response = new BasicHttpResponse();
     Assert.DoesNotThrow(() => response.AppendToBody((string)null));
 }
        /// <summary>
        ///     Asserts that the <see cref="T:Stumps.Stump"/> will respond with a <see cref="T:Stumps.BasicHttpResponse"/>.
        /// </summary>
        /// <param name="stump">The <see cref="T:Stumps.Stump"/> intercepting incomming HTTP requests.</param>
        /// <returns>A <see cref="T:Stumps.BasicHttpResponse"/> created for the <paramref name="stump"/>.</returns>
        /// <exception cref="System.ArgumentNullException"><paramref name="stump"/> is <c>null</c>.</exception>
        public static BasicHttpResponse Responds(this Stump stump)
        {
            if (stump == null)
            {
                throw new ArgumentNullException("stump");
            }

            var response = new BasicHttpResponse();
            stump.Response = response;
            return response;
        }
        public void AppendToBody_WithStringAndNullEncoding_ThrowsException()
        {
            var response = new BasicHttpResponse();

            Assert.That(
                () => response.AppendToBody(string.Empty, null),
                Throws.Exception.TypeOf<ArgumentNullException>().With.Property("ParamName").EqualTo("encoding"));
        }
Beispiel #40
0
        public void Constructor_Default_EmptyBody()
        {
            var response = new BasicHttpResponse();

            Assert.AreEqual(0, response.BodyLength);
        }