コード例 #1
0
        public void ShouldReplaceResponse()
        {
            string responseBody = new StringBuilder()
                .AppendLine("<html>")
                .AppendLine("<head><title>Fry Rocks!</title></head>")
                .AppendLine("<body><h1>Fry Proxy</h1></body>")
                .AppendLine("</html>")
                .ToString();

            HttpProxyServer.Proxy.OnRequestReceived = context =>
            {
                if (!context.RequestHeader.Host.Contains("wikipedia.org"))
                {
                    return;
                }

                context.StopProcessing();

                var responseStream = new MemoryStream(Encoding.ASCII.GetBytes(responseBody), false);
                var responseHeader = new HttpResponseHeader(200, "OK", "1.1");

                responseHeader.EntityHeaders.ContentType = "text/html";
                responseHeader.EntityHeaders.ContentEncoding = "us-ascii";
                responseHeader.EntityHeaders.ContentLength = responseStream.Length;

                new HttpResponseWriter(context.ClientStream).Write(
                    responseHeader,
                    responseStream,
                    responseStream.Length
                );
            };

            WebDriver.Navigate().GoToUrl("http://www.wikipedia.org");

            Assert.That(WebDriver.Title, Contains.Substring("Fry"));
        }
コード例 #2
0
 private static Boolean IsRedirect(HttpResponseHeader header)
 {
     return header != null
         && (header.StatusCode >= 300 || header.StatusCode < 400)
         && !String.IsNullOrEmpty(header.Location);
 }