Ejemplo n.º 1
0
        public dynamic[] CallInternal(IDictionary <string, dynamic> environment)
        {
            Request = new Request(environment);

            string method      = environment["REQUEST_METHOD"].ToString();
            string pathInfo    = environment["PATH_INFO"].ToString();
            string queryString = environment["QUERY_STRING"].ToString();

            var requestLine         = RequestLine.Create(method, pathInfo, queryString);
            var requestLineStripped = requestLine.ToString().StripQueryString();

            string actionKey = requestLineStripped;
            IDictionary <string, dynamic> routeParameters = new Dictionary <string, dynamic>();

            if (!_actionMap.ContainsKey(actionKey))
            {
                foreach (var key in _actionMap.Keys)
                {
                    var parser = new PatternParser(key);
                    if (parser.IsMatch(requestLineStripped))
                    {
                        actionKey       = key;
                        routeParameters = parser.Match(requestLineStripped);
                        break;
                    }
                }
            }

            if (!_actionMap.ContainsKey(actionKey))
            {
                return(new dynamic[] { 404, new Hash {
                                           { "Content-Type", "text/plain" }
                                       }, "Not found: " + requestLine.GetUri().StripQueryString() });
            }

            var methodInfo = _actionMap[actionKey];

            var dataDictionary = new Dictionary <string, dynamic>();

            if (routeParameters.Count > 0)
            {
                dataDictionary = dataDictionary.Concat(routeParameters).ToDictionary(pair => pair.Key,
                                                                                     pair => pair.Value);
            }

            var requestParams = Request.Params.ToDictionary(kvp => kvp.Key, kvp => (dynamic)kvp.Value);

            Params = requestParams.Concat(dataDictionary)
                     .ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

            var actionResponse = methodInfo.Invoke(this, null);

            if (actionResponse is string || actionResponse is IIterable)
            {
                return(new dynamic[] { 200, new Hash {
                                           { "Content-Type", "text/html" }
                                       }, actionResponse });
            }

            if (actionResponse is dynamic[])
            {
                return(actionResponse as dynamic[]);
            }

            return(new dynamic[] { 500, new Hash {
                                       { "Content-Type", "text/plain" }
                                   }, "Internal server error." });
        }
Ejemplo n.º 2
0
        public void It_Ensure_The_Query_String_Starts_With_A_Question_Mark()
        {
            var requestLine = RequestLine.Create("GET", "/animals/birds", "beak=long&feathers=white");

            Assert.AreEqual("GET /animals/birds?beak=long&feathers=white", requestLine.ToString());
        }
Ejemplo n.º 3
0
 public void It_Requires_A_Method()
 {
     Assert.Throws <ArgumentNullException>(() => RequestLine.Create(null, "/birds"));
 }
Ejemplo n.º 4
0
        public void It_Ensures_Path_Info_Starts_With_A_Slash()
        {
            var requestLine = RequestLine.Create("GET", "birds/pigeons");

            Assert.AreEqual("GET /birds/pigeons", requestLine.ToString());
        }