AppendToBody() public method

Appends a byte array to the body of the HTTP response.
public AppendToBody ( byte buffer ) : void
buffer byte The bytes to append to the body of the response.
return void
        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());
        }
Example #2
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());
        }
Example #3
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>
        /// 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);
        }
        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);
        }
        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());
        }
Example #7
0
        public void AppendToBody_WithString_UpdatesBody()
        {
            var expected = "ABCD";

            var response = new BasicHttpResponse();

            response.AppendToBody(expected);
            Assert.AreEqual(expected.Length, response.BodyLength);
        }
Example #8
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());
        }
Example #9
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);
        }
Example #11
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);
        }
        /// <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);
        }
Example #13
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);
        }
Example #14
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_WithStringAndNullEncoding_ThrowsException()
        {
            var response = new BasicHttpResponse();

            Assert.That(
                () => response.AppendToBody(string.Empty, null),
                Throws.Exception.TypeOf<ArgumentNullException>().With.Property("ParamName").EqualTo("encoding"));
        }
 public void AppendToBody_WithNullString_DoesNotTrowError()
 {
     var response = new BasicHttpResponse();
     Assert.DoesNotThrow(() => response.AppendToBody((string)null));
 }
 public void AppendToBody_WithNullBytes_DoesNotTrowError()
 {
     var response = new BasicHttpResponse();
     Assert.DoesNotThrow(() => response.AppendToBody((byte[])null));
 }
Example #18
0
        public void AppendToBody_WithNullBytes_DoesNotTrowError()
        {
            var response = new BasicHttpResponse();

            Assert.DoesNotThrow(() => response.AppendToBody((byte[])null));
        }
Example #19
0
        public void AppendToBody_WithNullString_DoesNotTrowError()
        {
            var response = new BasicHttpResponse();

            Assert.DoesNotThrow(() => response.AppendToBody((string)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 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);
        }