Пример #1
0
        public void Should_Be_Nestable()
        {
            var map = new UrlMap(new Dictionary <string, object> {
                { "/foo",
                  new UrlMap(new Dictionary <string, object> {
                        { "/bar",
                          new UrlMap(new Dictionary <string, object> {
                                { "/quux",
                                  DetachedApplication.Create(env =>
                                                             new dynamic[] { 200, new Hash
                                                                             {
                                                                                 { "Content-Type", "text/plain" },
                                                                                 { "X-Position", "/foo/bar/quux" },
                                                                                 { "X-PathInfo", env["PATH_INFO"] },
                                                                                 { "X-ScriptName", env["SCRIPT_NAME"] }
                                                                             }, string.Empty }) }
                            }) }
                    }) }
            });

            var res = new MockRequest(map).Get("/foo/bar");

            Assert.AreEqual(404, res.Status);

            res = new MockRequest(map).Get("/foo/bar/quux");
            Assert.AreEqual(200, res.Status);
            Assert.AreEqual("/foo/bar/quux", res["X-Position"]);
            Assert.AreEqual(string.Empty, res["X-PathInfo"]);
            Assert.AreEqual("/foo/bar/quux", res["X-ScriptName"]);
        }
Пример #2
0
        public void Should_Set_Content_Type_To_Chosen_Default_If_None_Is_Set()
        {
            var app = DetachedApplication.Create(env => new dynamic[] { 200, new Hash(), "Hello, World" });

            var response = new ContentType(app, "application/octet-stream").Call(new Dictionary <string, dynamic>());
            var headers  = response[1];

            Assert.AreEqual("application/octet-stream", headers["Content-Type"]);
        }
Пример #3
0
        public void Should_Not_Set_Content_Length_When_Transfer_Encoding_Is_Chunked()
        {
            var app =
                DetachedApplication.Create(
                    env => new dynamic[] { 200, new Hash {
                                               { "Transfer-Encoding", "chunked" }
                                           }, string.Empty });

            var response = new ContentLength(app).Call(new Dictionary <string, dynamic>());

            Assert.IsFalse(response[1].ContainsKey("Content-Length"));
        }
Пример #4
0
        public void Should_Not_Set_Content_Length_On_304_Responses()
        {
            var app =
                DetachedApplication.Create(
                    env => new dynamic[] { 304, new Hash {
                                               { "Content-Type", "text/plain" }
                                           }, string.Empty });

            var response = new ContentLength(app).Call(new Dictionary <string, dynamic>());

            Assert.IsFalse(response[1].ContainsKey("Content-Length"));
        }
Пример #5
0
 private static KeyValuePair <string, object> GetMapForRootRouteTest(string uri, string xPosition)
 {
     return(new KeyValuePair <string, object>(uri,
                                              DetachedApplication.Create(env =>
                                                                         new dynamic[] { 200, new Hash
                                                                                         {
                                                                                             { "Content-Type", "text/plain" },
                                                                                             { "X-Position", xPosition },
                                                                                             { "X-PathInfo", env["PATH_INFO"] },
                                                                                             { "X-ScriptName", env["SCRIPT_NAME"] }
                                                                                         }, string.Empty })));
 }
Пример #6
0
        public void Should_Not_Change_Content_Type_If_It_Is_Already_Set()
        {
            var app =
                DetachedApplication.Create(
                    env => new dynamic[] { 200, new Hash {
                                               { "Content-Type", "foo/bar" }
                                           }, "Hello, World" });

            var response = new ContentType(app).Call(new Dictionary <string, dynamic>());
            var headers  = response[1];

            Assert.AreEqual("foo/bar", headers["Content-Type"]);
        }
Пример #7
0
 private static KeyValuePair <string, object> GetMapForDispatchTest(string uri, string xPosition)
 {
     return(new KeyValuePair <string, object>(uri,
                                              DetachedApplication.Create(env =>
                                                                         new dynamic[] { 200, new Hash
                                                                                         {
                                                                                             { "Content-Type", "text/plain" },
                                                                                             { "X-Position", xPosition },
                                                                                             { "X-Host", env.ContainsKey("HTTP_HOST")
                                                 ? env["HTTP_HOST"]
                                                 : env["SERVER_NAME"] }
                                                                                         }, string.Empty })));
 }
Пример #8
0
        public void Should_Not_Change_Content_Length_If_It_Is_Already_Set()
        {
            var app =
                DetachedApplication.Create(
                    env =>
                    new dynamic[]
                    { 200, new Hash {
                          { "Content-Type", "text/plain" }, { "Content-Length", "1" }
                      }, "Hello, World!" });

            var response = new ContentLength(app).Call(new Dictionary <string, dynamic>());

            Assert.AreEqual("1", response[1]["Content-Length"]);
        }
Пример #9
0
        public void Should_Set_Content_Length_On_Array_Bodies_If_None_Is_Set()
        {
            var app =
                DetachedApplication.Create(
                    env =>
                    new dynamic[]
                    { 200, new Hash {
                          { "Content-Type", "text/plain" }
                      }, "Hello, World!" });

            var response = new ContentLength(app).Call(new Dictionary <string, dynamic>());

            Assert.AreEqual("13", response[1]["Content-Length"]);
        }
Пример #10
0
        public void Should_Catch_Exceptions()
        {
            dynamic res = null;

            var req = new MockRequest(new ShowExceptions(DetachedApplication.Create(
                                                             env => { throw new InvalidOperationException(); })));

            Assert.DoesNotThrow(delegate { res = req.Get("/"); });

            Assert.AreEqual(500, res.Status);

            Assert.IsTrue(res.Body.ToString().Contains("InvalidOperationException"));
            Assert.IsTrue(res.Body.ToString().Contains("ShowExceptions"));
        }
Пример #11
0
        public void Should_Detect_Content_Type_Case_Insensitive()
        {
            var app =
                DetachedApplication.Create(
                    env => new dynamic[] { 200, new Hash {
                                               { "CONTENT-Type", "foo/bar" }
                                           }, "Hello, World" });

            var response = new ContentType(app).Call(new Dictionary <string, dynamic>());
            var headers  = (Hash)response[1];

            var keyValuePair = headers.ToArray().Where(pair => pair.Key.ToLower() == "content-type").Single();

            Assert.AreEqual(new KeyValuePair <string, dynamic>("CONTENT-Type", "foo/bar"), keyValuePair);
        }
Пример #12
0
        public void Should_Not_Set_Content_Length_On_Variable_Length_Bodies()
        {
            Func <string> body         = () => "Hello World!";
            var           iterableBody = new IterableAdapter(new Proc(body), lambda => lambda.Call());

            var app =
                DetachedApplication.Create(
                    env => new dynamic[] { 200, new Hash {
                                               { "Content-Type", "text/plain" }
                                           }, iterableBody });

            var response = new ContentLength(app).Call(new Dictionary <string, dynamic>());

            Assert.IsFalse(response[1].ContainsKey("Content-Length"));
        }
Пример #13
0
        private static object GetLambdaLobster()
        {
            return(DetachedApplication.Create(env =>
            {
                var lobster = string.Empty;
                string href;

                if (env.ContainsKey("QUERY_STRING") &&
                    env["QUERY_STRING"].ToString().Contains("flip"))
                {
                    var lobsterArray = LobsterString.Split('\n').Select(x => new string(x.Reverse().ToArray())).ToArray();

                    lobster = string.Join("\n", lobsterArray);
                    href = "?";
                }
                else
                {
                    lobster = LobsterString;
                    href = "?flip";
                }

                var content = "<title>Lobstericious!</title><pre>" +
                              lobster + "</pre><a href=\"" + href +
                              "\">flip!</a>";

                return new dynamic[]
                {
                    200,
                    new Hash {
                        { "Content-Length", content.Length.ToString() },
                        { "Content-Type", "text/html" }
                    },
                    content
                };
            }));
        }
Пример #14
0
        public void Should_Dispatch_Paths_Correctly()
        {
            var app = DetachedApplication.Create
                          (env =>
                          new dynamic[] { 200,
                                          new Hash
                                          {
                                              { "X-ScriptName", env["SCRIPT_NAME"] },
                                              { "X-PathInfo", env["PATH_INFO"] },
                                              { "Content-Type", "text/plain" }
                                          },
                                          string.Empty });

            var map = new UrlMap(new Dictionary <string, object>
            {
                { "http://foo.org/bar", app },
                { "/foo", app },
                { "/foo/bar", app }
            });

            var res = new MockRequest(map).Get("/");

            Assert.AreEqual(404, res.Status);

            res = new MockRequest(map).Get("/qux");
            Assert.AreEqual(404, res.Status);

            res = new MockRequest(map).Get("/foo");
            Assert.AreEqual(200, res.Status);
            Assert.AreEqual("/foo", res["X-ScriptName"]);
            Assert.AreEqual(string.Empty, res["X-PathInfo"]);

            res = new MockRequest(map).Get("/foo/");
            Assert.AreEqual(200, res.Status);
            Assert.AreEqual("/foo", res["X-ScriptName"]);
            Assert.AreEqual("/", res["X-PathInfo"]);

            res = new MockRequest(map).Get("/foo/bar");
            Assert.AreEqual(200, res.Status);
            Assert.AreEqual("/foo/bar", res["X-ScriptName"]);
            Assert.AreEqual(string.Empty, res["X-PathInfo"]);

            res = new MockRequest(map).Get("/foo/bar/");
            Assert.AreEqual(200, res.Status);
            Assert.AreEqual("/foo/bar", res["X-ScriptName"]);
            Assert.AreEqual("/", res["X-PathInfo"]);

            res = new MockRequest(map).Get("/foo///bar//quux");
            Assert.AreEqual(200, res.Status);
            Assert.AreEqual("/foo/bar", res["X-ScriptName"]);
            Assert.AreEqual("//quux", res["X-PathInfo"]);

            res = new MockRequest(map).Get("/foo/quux", new Dictionary <string, object> {
                { "SCRIPT_NAME", "/bleh" }
            });
            Assert.AreEqual(200, res.Status);
            Assert.AreEqual("/bleh/foo", res["X-ScriptName"]);
            Assert.AreEqual("/quux", res["X-PathInfo"]);

            res = new MockRequest(map).Get("/bar", new Dictionary <string, object> {
                { "HTTP_HOST", "foo.org" }
            });
            Assert.AreEqual(200, res.Status);
            Assert.AreEqual("/bar", res["X-ScriptName"]);
            Assert.AreEqual(string.Empty, res["X-PathInfo"]);

            res = new MockRequest(map).Get("/bar/", new Dictionary <string, object> {
                { "HTTP_HOST", "foo.org" }
            });
            Assert.AreEqual(200, res.Status);
            Assert.AreEqual("/bar", res["X-ScriptName"]);
            Assert.AreEqual("/", res["X-PathInfo"]);
        }