public Task Invoke(IDictionary<string, object> environment)
        {
            if (!environment.Get<string>(OwinConstants.RequestMethodKey).EqualsIgnoreCase("GET"))
            {
                return _inner(environment);
            }

            var originalOutput = environment.Get<Stream>(OwinConstants.ResponseBodyKey);
            var recordedStream = new MemoryStream();
            environment.Set(OwinConstants.ResponseBodyKey, recordedStream);

            return _inner(environment).ContinueWith(t => {
                recordedStream.Position = 0;
                environment[OwinConstants.ResponseBodyKey] = originalOutput;

                var response = new OwinHttpResponse(environment);

                if (IsGetHtmlRequest(environment) && response.StatusCode < 500)
                {
                    injectContent(environment, recordedStream, response);
                }
                else
                {
                    response.StreamContents(recordedStream);
                }
            });
        }
示例#2
0
        public Task Invoke(IDictionary<string, object> environment)
        {
            var writer = new OwinHttpResponse(environment);
            writer.AppendHeader("Slow-Moving", "Laser");

            return _inner(environment);
        }
        public Task Invoke(IDictionary<string, object> environment)
        {
            var writer = new OwinHttpResponse(environment);
            writer.AppendHeader("James", "Bond");

            return _inner(environment);
        }
        public void beforeEach()
        {
            environment = new Dictionary<string, object>();
            environment.Add(OwinConstants.ResponseBodyKey, new MemoryStream());

            response = new OwinHttpResponse(environment);
        }
示例#5
0
        public void beforeEach()
        {
            environment = new Dictionary <string, object>();
            environment.Add(OwinConstants.ResponseBodyKey, new MemoryStream());

            response = new OwinHttpResponse(environment);
        }
示例#6
0
        public Task Invoke(IDictionary<string, object> environment)
        {
            var path = environment[OwinConstants.RequestPathKey].As<string>().TrimStart('/');

            if (path == "topics.js")
            {
                return Task.Factory.StartNew(() =>
                {
                    var response = new OwinHttpResponse(environment);
                    response.WriteContentType("text/javascript");
                    new AssetSettings().Headers.Each((key, func) => response.AppendHeader(key, func()));
                    response.Write(_topicJS);

                    response.Flush();
                });
            }

            var topic = _project.FindTopicByUrl(path);
            if (topic == null)
            {
                return _inner(environment);
            }

            return Task.Factory.StartNew(() =>
            {
                var response = new OwinHttpResponse(environment);
                response.WriteContentType("text/html");

                var html = GenerateHtml(topic);

                response.Write(html);
                response.Flush();
            });
        }
        public Task Invoke(IDictionary <string, object> environment)
        {
            if (!environment.Get <string>(OwinConstants.RequestMethodKey).EqualsIgnoreCase("GET"))
            {
                return(_inner(environment));
            }

            var originalOutput = environment.Get <Stream>(OwinConstants.ResponseBodyKey);
            var recordedStream = new MemoryStream();

            environment.Set(OwinConstants.ResponseBodyKey, recordedStream);

            return(_inner(environment).ContinueWith(t => {
                recordedStream.Position = 0;
                environment[OwinConstants.ResponseBodyKey] = originalOutput;

                var response = new OwinHttpResponse(environment);

                if (IsGetHtmlRequest(environment) && response.StatusCode < 500)
                {
                    injectContent(environment, recordedStream, response);
                }
                else
                {
                    response.StreamContents(recordedStream);
                }
            }));
        }
示例#8
0
        public void use_anonymous_wrapping_Func_AppFunc_AppFunc_as_middleware()
        {
            Func <AppFunc, AppFunc> middleware = inner =>
            {
                return(dict =>
                {
                    var response = new OwinHttpResponse(dict);

                    response.Write("1-");
                    response.Flush();
                    return inner(dict).ContinueWith(t =>
                    {
                        response.Write("-2");
                        response.Flush();
                    });
                });
            };

            using (var server = serverFor(x => x.AddMiddleware(middleware)))
            {
                server.Scenario(_ =>
                {
                    _.Get.Action <MiddleWareInterceptedEndpoint>(x => x.get_middleware_result());
                    _.ContentShouldBe("1-I'm okay-2");
                });
            }
        }
        public void SetUp()
        {
            theRequest  = new OwinHttpRequest();
            theResponse = new OwinHttpResponse(theRequest.Environment);


            theMiddleware = new StaticFileMiddleware(null, theFiles, new AssetSettings());
        }
示例#10
0
        public Task Invoke(IDictionary <string, object> environment)
        {
            var writer = new OwinHttpResponse(environment);

            writer.AppendHeader("Slow-Moving", "Laser");

            return(_inner(environment));
        }
示例#11
0
        public Task Invoke(IDictionary <string, object> environment)
        {
            var writer = new OwinHttpResponse(environment);

            writer.AppendHeader("James", "Bond");

            return(_inner(environment));
        }
示例#12
0
 private void write500(IDictionary<string, object> environment, Exception exception)
 {
     using (var writer = new OwinHttpResponse(environment))
     {
         writer.WriteResponseCode(HttpStatusCode.InternalServerError);
         writer.Write("FubuMVC has detected an exception\r\n");
         writer.Write(exception.ToString());
     }
 }
示例#13
0
        public static OwinHttpResponse GetByInput(object input)
        {
            OwinHttpResponse response = null;

            Scenario(x => {
                x.Get.Input(input);

                response = x.Response;
            });

            return(response);
        }
示例#14
0
        public static OwinHttpResponse GetByAction <T>(Expression <Action <T> > expression)
        {
            OwinHttpResponse response = null;

            Scenario(x =>
            {
                x.Get.Action(expression);

                response = x.Response;
            });

            return(response);
        }
        private void injectContent(IDictionary<string, object> environment, MemoryStream recordedStream, OwinHttpResponse response)
        {
            var html = recordedStream.ReadAllText();
            var builder = new StringBuilder(html);
            var position = html.IndexOf("</head>", 0, StringComparison.OrdinalIgnoreCase);

            if (position >= 0)
            {
                builder.Insert(position, _options.Content(environment));
            }

            response.Write(builder.ToString());
            response.Flush();
        }
示例#16
0
        private void validateStatusCode(OwinHttpResponse response)
        {
            var httpStatusCode = response.StatusCode;

            if (httpStatusCode != _expectedStatusCode)
            {
                _assertion.Add("Expected status code {0} ({1}), but was {2}", _expectedStatusCode,
                               _expectedStatusReason, httpStatusCode);

                if (httpStatusCode >= 500)
                {
                    _assertion.Body = response.Body.ReadAsText();
                }
            }
        }
        public void write_a_header_that_allows_multiple_values()
        {
            var settings = new OwinHeaderSettings();
            var environment = new Dictionary<string, object>
            {
                {OwinConstants.HeaderSettings, settings}
            };
            var response = new OwinHttpResponse(environment);

            response.AppendHeader(HttpGeneralHeaders.Allow, "application/json");
            response.AppendHeader(HttpGeneralHeaders.Allow, "text/json");

            var headers = environment.Get<IDictionary<string, string[]>>(OwinConstants.ResponseHeadersKey);
            headers[HttpGeneralHeaders.Allow].ShouldHaveTheSameElementsAs("application/json", "text/json");
        }
示例#18
0
        public void write_a_header_that_allows_multiple_values()
        {
            var settings    = new OwinHeaderSettings();
            var environment = new Dictionary <string, object>
            {
                { OwinConstants.HeaderSettings, settings }
            };
            var response = new OwinHttpResponse(environment);

            response.AppendHeader(HttpGeneralHeaders.Allow, "application/json");
            response.AppendHeader(HttpGeneralHeaders.Allow, "text/json");

            var headers = environment.Get <IDictionary <string, string[]> >(OwinConstants.ResponseHeadersKey);

            headers[HttpGeneralHeaders.Allow].ShouldHaveTheSameElementsAs("application/json", "text/json");
        }
        public void write_a_header_that_does_not_allow_multiple_values()
        {
            var settings = new OwinHeaderSettings();
            var environment = new Dictionary<string, object>
            {
                {OwinConstants.HeaderSettings, settings}
            };
            var response = new OwinHttpResponse(environment);

            settings.DoNotAllowMultipleValues(HttpRequestHeaders.ContentLength);

            response.AppendHeader(HttpRequestHeaders.ContentLength, "1234");
            response.AppendHeader(HttpRequestHeaders.ContentLength, "1234");

            var headers = environment.Get<IDictionary<string, string[]>>(OwinConstants.ResponseHeadersKey);
            headers[HttpRequestHeaders.ContentLength].ShouldHaveTheSameElementsAs("1234");
        }
示例#20
0
            public string GenerateTextForCurrentThreadCulture()
            {
                var request = OwinHttpRequest.ForTesting();

                request.ContentType(MimeType.HttpFormMimetype);
                request.Accepts(MimeType.Html.Value);


                var services = new OwinServiceArguments(new RouteData(), request.Environment);
                var invoker  = new BehaviorInvoker(_parent._services, _chain);

                invoker.Invoke(services);

                var response = new OwinHttpResponse(request.Environment);

                return(response.Body.ReadAsText());
            }
示例#21
0
        public void write_a_header_that_does_not_allow_multiple_values()
        {
            var settings    = new OwinHeaderSettings();
            var environment = new Dictionary <string, object>
            {
                { OwinConstants.HeaderSettings, settings }
            };
            var response = new OwinHttpResponse(environment);

            settings.DoNotAllowMultipleValues(HttpRequestHeaders.ContentLength);

            response.AppendHeader(HttpRequestHeaders.ContentLength, "1234");
            response.AppendHeader(HttpRequestHeaders.ContentLength, "1234");

            var headers = environment.Get <IDictionary <string, string[]> >(OwinConstants.ResponseHeadersKey);

            headers[HttpRequestHeaders.ContentLength].ShouldHaveTheSameElementsAs("1234");
        }
示例#22
0
        private void write500(IDictionary<string, object> environment, Exception exception)
        {
            using (var writer = new OwinHttpResponse(environment))
            {
                writer.WriteResponseCode(HttpStatusCode.InternalServerError);
                var document = new HtmlDocument
                {
                    Title = "Exception!"
                };

                document.Add("h1").Text("FubuMVC has detected an exception");
                document.Add("hr");
                document.Add("pre").Id("error").Text(exception.ToString());

                writer.WriteContentType(MimeType.Html.Value);
                writer.Write(document.ToString());
            }
        }
示例#23
0
        public Task Invoke(IDictionary <string, object> environment)
        {
            var path = environment[OwinConstants.RequestPathKey].As <string>().TrimStart('/');

            if (path == "topics.js")
            {
                return(Task.Factory.StartNew(() =>
                {
                    var response = new OwinHttpResponse(environment);
                    response.WriteContentType("text/javascript");

                    response.Write(_topicJS);



                    response.Flush();
                }));
            }

            var topic = _project.FindTopicByUrl(path);

            if (topic == null)
            {
                return(_inner(environment));
            }

            return(Task.Factory.StartNew(() =>
            {
                var response = new OwinHttpResponse(environment);
                response.WriteContentType("text/html");

                response.AppendHeader(HttpGeneralHeaders.CacheControl, "no-cache, no-store, must-revalidate");
                response.AppendHeader(HttpResponseHeaders.Pragma, "no-cache");
                response.AppendHeader(HttpResponseHeaders.Expires, "0");


                var html = GenerateHtml(topic);


                response.Write(html);
                response.Flush();
            }));
        }
示例#24
0
        public Task Invoke(IDictionary<string, object> environment)
        {
            var path = environment[OwinConstants.RequestPathKey].As<string>().TrimStart('/');

            if (path == "topics.js")
            {
                return Task.Factory.StartNew(() =>
                {
                    var response = new OwinHttpResponse(environment);
                    response.WriteContentType("text/javascript");

                    response.Write(_topicJS);


                   
                    response.Flush();
                });
            }

            var topic = _project.FindTopicByUrl(path);
            if (topic == null)
            {
                return _inner(environment);
            }

            return Task.Factory.StartNew(() =>
            {
                var response = new OwinHttpResponse(environment);
                response.WriteContentType("text/html");

                response.AppendHeader(HttpGeneralHeaders.CacheControl, "no-cache, no-store, must-revalidate");
                response.AppendHeader(HttpResponseHeaders.Pragma, "no-cache");
                response.AppendHeader(HttpResponseHeaders.Expires, "0");


                var html = GenerateHtml(topic);


                response.Write(html);
                response.Flush();
            });

        }
示例#25
0
        public HttpRequestSummary(ChainExecutionLog log) : this()
        {
            var request  = new OwinHttpRequest(log.Request);
            var response = new OwinHttpResponse(log.Request);

            id          = log.Id.ToString();
            time        = log.Time.ToHttpDateString();
            url         = request.RawUrl();
            method      = request.HttpMethod();
            status      = response.StatusCode;
            description = response.StatusDescription;
            if (status == 302)
            {
                // TODO -- write a helper for location
                description = response.HeaderValueFor(HttpResponseHeaders.Location).SingleOrDefault();
            }

            contentType = response.ContentType();
            duration    = log.ExecutionTime;
        }
示例#26
0
        public HttpRequestSummary(ChainExecutionLog log) : this()
        {
            var request = new OwinHttpRequest(log.Request);
            var response = new OwinHttpResponse(log.Request);

            id = log.Id.ToString();
            time = log.Time.ToHttpDateString();
            url = request.RawUrl();
            method = request.HttpMethod();
            status = response.StatusCode;
            description = response.StatusDescription;
            if (status == 302)
            {
                // TODO -- write a helper for location
                description = response.HeaderValueFor(HttpResponseHeaders.Location).SingleOrDefault();
            }

            contentType = response.ContentType();
            duration = log.ExecutionTime;
        }
示例#27
0
        public void use_anonymous_wrapping_Func_AppFunc_AppFunc_as_middleware()
        {
            Func <AppFunc, AppFunc> middleware = inner => {
                return(dict => {
                    var response = new OwinHttpResponse(dict);

                    response.Write("1-");
                    response.Flush();
                    return inner(dict).ContinueWith(t => {
                        response.Write("-2");
                        response.Flush();
                    });
                });
            };

            using (var server = serverFor(x =>
            {
                x.AddMiddleware(middleware);
            }))
            {
                server.Endpoints.Get <MiddleWareInterceptedEndpoint>(x => x.get_middleware_result())
                .ReadAsText().ShouldContain("1-I'm okay-2");
            }
        }
示例#28
0
        public Task Invoke(IDictionary <string, object> environment)
        {
            var path = environment[OwinConstants.RequestPathKey].As <string>().TrimStart('/');

            if (path == "topics.js")
            {
                return(Task.Factory.StartNew(() =>
                {
                    var response = new OwinHttpResponse(environment);
                    response.WriteContentType("text/javascript");
                    new AssetSettings().Headers.Each((key, func) => response.AppendHeader(key, func()));
                    response.Write(_topicJS);

                    response.Flush();
                }));
            }

            var topic = _project.FindTopicByUrl(path);

            if (topic == null)
            {
                return(_inner(environment));
            }

            return(Task.Factory.StartNew(() =>
            {
                var response = new OwinHttpResponse(environment);
                response.WriteContentType("text/html");

                var html = GenerateHtml(topic);


                response.Write(html);
                response.Flush();
            }));
        }
        public void use_anonymous_wrapping_Func_AppFunc_AppFunc_as_middleware()
        {
            Func<AppFunc, AppFunc> middleware = inner => {
                return dict => {
                    var response = new OwinHttpResponse(dict);

                    response.Write("1-");
                    response.Flush();
                    return inner(dict).ContinueWith(t => {
                        response.Write("-2");
                        response.Flush();
                    });
                };
            };

            using (var server = serverFor(x =>
            {
                x.AddMiddleware(middleware);
            }))
            {
                server.Endpoints.Get<MiddleWareInterceptedEndpoint>(x => x.get_middleware_result())
                    .ReadAsText().ShouldContain("1-I'm okay-2");
            }
        }
        public void SetUp()
        {
            theRequest = new OwinHttpRequest();
            theResponse = new OwinHttpResponse(theRequest.Environment);

            theMiddleware = new StaticFileMiddleware(null, theFiles, new AssetSettings());
        }
        private void injectContent(IDictionary <string, object> environment, MemoryStream recordedStream, OwinHttpResponse response)
        {
            var html     = recordedStream.ReadAllText();
            var builder  = new StringBuilder(html);
            var position = html.IndexOf("</head>", 0, StringComparison.OrdinalIgnoreCase);

            if (position >= 0)
            {
                builder.Insert(position, _options.Content(environment));
            }

            response.Write(builder.ToString());
            response.Flush();
        }
        public void use_anonymous_wrapping_Func_AppFunc_AppFunc_as_middleware()
        {
            Func<AppFunc, AppFunc> middleware = inner =>
            {
                return dict =>
                {
                    var response = new OwinHttpResponse(dict);

                    response.Write("1-");
                    response.Flush();
                    return inner(dict).ContinueWith(t =>
                    {
                        response.Write("-2");
                        response.Flush();
                    });
                };
            };

            using (var server = serverFor(x => x.AddMiddleware(middleware)))
            {
                server.Scenario(_ =>
                {
                    _.Get.Action<MiddleWareInterceptedEndpoint>(x => x.get_middleware_result());
                    _.ContentShouldBe("1-I'm okay-2");
                });
            }
        }