예제 #1
0
        void SendToTarget(Uri targetUrl, LinkbackSendParameters parameters)
        {
            HttpWebRequestAbstraction request = HttpWebRequestAbstraction.Create(targetUrl);

            SetupRequest(request, parameters);

            using (HttpWebResponseAbstraction response = request.GetResponse())
            {
                if (!response.IsHttpStatusCode2XX)
                {
                    throw new LinkbackSendException(String.Format(CultureInfo.InvariantCulture, "Http error while sending {0} for {1}", Name, targetUrl));
                }

                Stream receiveStream = response.GetResponseStream();

                if (response.ContentEncoding.ToUpperInvariant().Contains("GZIP"))
                {
                    receiveStream = new GZipStream(receiveStream, CompressionMode.Decompress);
                }
                else if (response.ContentEncoding.ToUpperInvariant().Contains("DEFLATE"))
                {
                    receiveStream = new DeflateStream(receiveStream, CompressionMode.Decompress);
                }

                StreamReader streamReader = new StreamReader(receiveStream, Encoding.UTF8);

                ParseResponse(targetUrl, streamReader.ReadToEnd());
            }
        }
예제 #2
0
        public ISendResult Send(Uri sendUrl, LinkbackSendParameters parameters)
        {
            HandleExceptionsOnSend(
                () => {
                Uri targetUrl = DiscoveryTargetUrl(sendUrl, parameters);

                SendToTarget(targetUrl, parameters);
            });

            return(this as ISendResult);
        }
예제 #3
0
        protected override Uri DiscoveryTargetUrl(Uri sendUrl, LinkbackSendParameters parameters)
        {
            HttpWebRequestAbstraction request = HttpWebRequestAbstraction.Create(sendUrl);

            using (HttpWebResponseAbstraction response = request.GetResponse())
            {
                if (response.IsHttpStatusCode2XX)
                {
                    return(FindTargetUrl(sendUrl, response));
                }
            }

            throw new LinkbackSendException(String.Format(CultureInfo.InvariantCulture, "Http error while discovering {0} url for {1}", Name, sendUrl));
        }
예제 #4
0
        public void Discovery_Both_Header_And_Link_Not_Found_Then_LinkbackSendException_Saved()
        {
            // Setup

            var webRequest1 = new Mock<IHttpWebRequestImplementation>(MockBehavior.Strict);
            var webResponse1 = new Mock<IHttpWebResponseImplementation>(MockBehavior.Strict);

            webRequest1.Setup(x => x.Create(new Uri("http://target/1", UriKind.Absolute))).Returns(webRequest1.Object);
            webRequest1.Setup(x => x.GetResponse()).Returns(webResponse1.Object);

            webResponse1.Setup(x => x.Close());
            webResponse1.SetupGet(x => x.IsHttpStatusCode2XX).Returns(true);
            webResponse1.SetupGet(x => x.ContentEncoding).Returns(String.Empty);
            webResponse1.SetupGet(x => x.Headers).Returns(new System.Net.WebHeaderCollection { });
            string response_content = "...";
            webResponse1.Setup(x => x.GetResponseStream()).Returns(new MemoryStream(new UTF8Encoding().GetBytes(response_content)));

            // Test

            var pingback = new Pingback(webRequest1.Object);

            var url = new Uri("http://target/1");

            var parameters = new LinkbackSendParameters
            {
                SourceUrl = new Uri("http://source/1"),
                TargetUrl = new Uri("http://target/1")
            };

            var result = pingback.Send(url, parameters);

            // Verify

            webRequest1.VerifyAll();
            webResponse1.VerifyAll();

            Assert.False(result.Success);
            Assert.Equal(0, result.Code);
            Assert.True(String.IsNullOrEmpty(result.Message));
            Assert.IsType<LinkbackSendException>(result.SendException);
            Assert.Equal("Pingback url discovering failed for http://target/1", result.SendException.Message);
        }
예제 #5
0
        Uri FindTargetUrl(Uri sendUrl, LinkbackSendParameters parameters, HttpWebResponseAbstraction response)
        {
            Stream receiveStream = response.GetResponseStream();

            if (response.ContentEncoding.ToUpperInvariant().Contains("GZIP"))
            {
                receiveStream = new GZipStream(receiveStream, CompressionMode.Decompress);
            }
            else if (response.ContentEncoding.ToUpperInvariant().Contains("DEFLATE"))
            {
                receiveStream = new DeflateStream(receiveStream, CompressionMode.Decompress);
            }

            StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
            string       content    = readStream.ReadToEnd();

            var matches = _regex_rdf.Matches(content);

            if (matches.Count > 0)
            {
                foreach (Match match in matches)
                {
                    if (String.Compare(match.Groups[1].ToString().Trim(), sendUrl.ToString(), StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        string trackback_ping_url = match.Groups[2].ToString().Trim();

                        return(new Uri(trackback_ping_url, UriKind.Absolute));
                    }
                }
            }

            if (parameters.AutoDiscovery == null)
            {
                return(sendUrl);
            }

            throw new LinkbackSendException(String.Format(CultureInfo.InvariantCulture, "RDF not found while discovering {0} url for {1}", Name, sendUrl));
        }
예제 #6
0
 protected abstract void SetupRequest(HttpWebRequestAbstraction request, LinkbackSendParameters parameters);
예제 #7
0
        void SendToTarget(Uri targetUrl, LinkbackSendParameters parameters)
        {
            HttpWebRequestAbstraction request = HttpWebRequestAbstraction.Create(targetUrl);

            SetupRequest(request, parameters);

            using (HttpWebResponseAbstraction response = request.GetResponse())
            {
                if (!response.IsHttpStatusCode2XX)
                {
                    throw new LinkbackSendException(String.Format(CultureInfo.InvariantCulture, "Http error while sending {0} for {1}", Name, targetUrl));
                }

                Stream receiveStream = response.GetResponseStream();

                if (response.ContentEncoding.ToUpperInvariant().Contains("GZIP"))
                    receiveStream = new GZipStream(receiveStream, CompressionMode.Decompress);
                else if (response.ContentEncoding.ToUpperInvariant().Contains("DEFLATE"))
                    receiveStream = new DeflateStream(receiveStream, CompressionMode.Decompress);

                StreamReader streamReader = new StreamReader(receiveStream, Encoding.UTF8);

                ParseResponse(targetUrl, streamReader.ReadToEnd());
            }
        }
예제 #8
0
 protected override void SetupRequest(HttpWebRequestAbstraction request, LinkbackSendParameters parameters)
 {
     parameters.SetupRequestForPingback(request);
 }
예제 #9
0
 protected abstract void SetupRequest(HttpWebRequestAbstraction request, LinkbackSendParameters parameters);
예제 #10
0
        public void Autodiscovery_Fail_Then_Try_Direct()
        {
            // Setup

            var webRequest1 = new Mock<IHttpWebRequestImplementation>(MockBehavior.Strict);
            var webResponse1 = new Mock<IHttpWebResponseImplementation>(MockBehavior.Strict);

            //webRequest1.Setup(x => x.Create(new Uri("http://target/post/1/trackback"))).Returns(webRequest1.Object);
            bool firstRequest = true;
            webRequest1.Setup(x => x.GetResponse()).Returns(webResponse1.Object);
            webResponse1.SetupGet(x => x.IsHttpStatusCode2XX).Returns(false);
            webResponse1.Setup(x => x.Close());

            var webRequest2 = new Mock<IHttpWebRequestImplementation>(MockBehavior.Strict);
            var webResponse2 = new Mock<IHttpWebResponseImplementation>(MockBehavior.Strict);

            webRequest1.Setup(x => x.Create(new Uri("http://target/post/1/trackback"))).Returns(
                () => {
                    if (firstRequest) {
                        firstRequest = false;
                        return webRequest1.Object;
                    }
                    else {
                        return webRequest2.Object;
                    }
                });
            webRequest2.SetupSet(x => x.Method = "POST");
            webRequest2.SetupSet(x => x.ContentType = "application/x-www-form-urlencoded");
            webRequest2.SetupSet(x => x.ContentLength = 82);
            var requestStream = new MemoryStream();
            webRequest2.Setup(x => x.GetRequestStream()).Returns(requestStream);
            webRequest2.Setup(x => x.GetResponse()).Returns(webResponse2.Object);

            webResponse2.Setup(x => x.Close());
            webResponse2.SetupGet(x => x.IsHttpStatusCode2XX).Returns(true);
            webResponse2.SetupGet(x => x.ContentEncoding).Returns(String.Empty);
            webResponse2.Setup(x => x.GetResponseStream()).Returns(new MemoryStream(new UTF8Encoding().GetBytes("<?xml version=\"1.0\" encoding=\"utf-8\"?><response><error>0</error></response>")));

            // Test

            var trackback = new Trackback(webRequest1.Object);

            var url = new Uri("http://target/post/1/trackback");

            var parameters = new LinkbackSendParameters
            {
                Title = "Source Title",
                Excerpt = "ABC",
                Url = new Uri("http://source/1"),
                BlogName = "Test Blog"
            };

            var result = trackback.Send(url, parameters);

            // Verify

            webRequest1.VerifyAll();
            webResponse1.VerifyAll();
            webRequest2.VerifyAll();
            webResponse2.VerifyAll();

            Assert.Equal("url=http%3A%2F%2Fsource%2F1&title=Source%20Title&excerpt=ABC&blog_name=Test%20Blog", new UTF8Encoding().GetString(requestStream.ToArray()));

            Assert.True(result.Success);
            Assert.Equal(0, result.Code);
            Assert.True(String.IsNullOrEmpty(result.Message));
        }
예제 #11
0
        public void ProtocolViolationException_Saved()
        {
            // Setup

            var webRequest1 = new Mock<IHttpWebRequestImplementation>(MockBehavior.Strict);
            var webResponse1 = new Mock<IHttpWebResponseImplementation>(MockBehavior.Strict);

            webRequest1.Setup(x => x.Create(new Uri("http://target/post/1"))).Returns(webRequest1.Object);
            webRequest1.Setup(x => x.GetResponse()).Throws(new System.Net.ProtocolViolationException("ProtocolViolationException"));

            // Test

            var trackback = new Trackback(webRequest1.Object);

            var url = new Uri("http://target/post/1");

            var parameters = new LinkbackSendParameters
            {
                Title = "Source Title",
                Excerpt = "ABC",
                Url = new Uri("http://source/1"),
                BlogName = "Test Blog"
            };

            var result = trackback.Send(url, parameters);

            // Verify

            webRequest1.VerifyAll();
            webResponse1.VerifyAll();

            Assert.False(result.Success);
            Assert.Equal(0, result.Code);
            Assert.True(String.IsNullOrEmpty(result.Message));
            Assert.IsType<System.Net.ProtocolViolationException>(result.SendException);
        }
예제 #12
0
        public void Send_Response_Is_GZipped()
        {
            // Setup

            var webRequest1 = new Mock<IHttpWebRequestImplementation>(MockBehavior.Strict);
            var webResponse1 = new Mock<IHttpWebResponseImplementation>(MockBehavior.Strict);

            webRequest1.Setup(x => x.Create(new Uri("http://target/1", UriKind.Absolute))).Returns(webRequest1.Object);
            webRequest1.Setup(x => x.GetResponse()).Returns(webResponse1.Object);

            webResponse1.Setup(x => x.Close());
            webResponse1.SetupGet(x => x.IsHttpStatusCode2XX).Returns(true);
            webResponse1.SetupGet(x => x.Headers).Returns(new System.Net.WebHeaderCollection { });
            webResponse1.SetupGet(x => x.ContentEncoding).Returns("gzip");
            string response_content = @"
            ...
            <p>
            <link rel=""pingback"" href=""http://target/pingback"">
            </p>
            ...";
            var response_content_bytes = new UTF8Encoding().GetBytes(response_content);
            var memoryStream = new MemoryStream();
            var gzippedStream = new GZipStream(memoryStream, CompressionMode.Compress, true);
            gzippedStream.Write(response_content_bytes, 0, response_content_bytes.Length);
            gzippedStream.Close();
            memoryStream.Position = 0;

            webResponse1.Setup(x => x.GetResponseStream()).Returns(memoryStream);

            var webRequest2 = new Mock<IHttpWebRequestImplementation>(MockBehavior.Strict);
            var webResponse2 = new Mock<IHttpWebResponseImplementation>(MockBehavior.Strict);

            webRequest1.Setup(x => x.Create(new Uri("http://target/pingback", UriKind.Absolute))).Returns(webRequest2.Object);
            webRequest2.SetupSet(x => x.Method = "POST");
            webRequest2.SetupSet(x => x.ContentType = "text/xml");
            webRequest2.SetupSet(x => x.ContentLength = 225);
            var requestStream = new MemoryStream();
            webRequest2.Setup(x => x.GetRequestStream()).Returns(requestStream);
            webRequest2.Setup(x => x.GetResponse()).Returns(webResponse2.Object);

            webResponse2.Setup(x => x.Close());
            webResponse2.SetupGet(x => x.IsHttpStatusCode2XX).Returns(true);
            webResponse2.SetupGet(x => x.ContentEncoding).Returns("gzip");

            string responseContent = "<?xml version=\"1.0\"?><methodResponse><params><param><value><string>ok</string></value></param></params></methodResponse>";
            var responseContentBytes = new UTF8Encoding().GetBytes(responseContent);
            var responseMemoryStream = new MemoryStream();
            var gzippedResponseStream = new GZipStream(responseMemoryStream, CompressionMode.Compress, true);
            gzippedResponseStream.Write(responseContentBytes, 0, responseContentBytes.Length);
            gzippedResponseStream.Close();
            responseMemoryStream.Position = 0;

            webResponse2.Setup(x => x.GetResponseStream()).Returns(responseMemoryStream);

            // Test

            var pingback = new Pingback(webRequest1.Object);

            var url = new Uri("http://target/1");

            var parameters = new LinkbackSendParameters
            {
                SourceUrl = new Uri("http://source/1"),
                TargetUrl = new Uri("http://target/1")
            };

            var result = pingback.Send(url, parameters);

            // Verify

            webRequest1.VerifyAll();
            webResponse1.VerifyAll();
            webRequest2.VerifyAll();
            webResponse2.VerifyAll();

            Assert.True(result.Success);
            Assert.Equal(0, result.Code);
            Assert.True(String.IsNullOrEmpty(result.Message));
        }
예제 #13
0
        public void Autodiscovery_RDF_Not_Found_But_Autodiscovery_Is_True_Then_LinkbackSendException_Saved()
        {
            // Setup

            var webRequest1 = new Mock<IHttpWebRequestImplementation>(MockBehavior.Strict);
            var webResponse1 = new Mock<IHttpWebResponseImplementation>(MockBehavior.Strict);

            webRequest1.Setup(x => x.Create(new Uri("http://target/post/1"))).Returns(webRequest1.Object);
            webRequest1.Setup(x => x.GetResponse()).Returns(webResponse1.Object);

            webResponse1.Setup(x => x.Close());
            webResponse1.SetupGet(x => x.IsHttpStatusCode2XX).Returns(true);
            webResponse1.SetupGet(x => x.ContentEncoding).Returns(String.Empty);
            string rdf = "... there are no any RDFs ... ";
            webResponse1.Setup(x => x.GetResponseStream()).Returns(new MemoryStream(new UTF8Encoding().GetBytes(rdf)));

            // Test

            var trackback = new Trackback(webRequest1.Object);

            var url = new Uri("http://target/post/1");

            var parameters = new LinkbackSendParameters
            {
                Title = "Source Title",
                Excerpt = "ABC",
                Url = new Uri("http://source/1"),
                BlogName = "Test Blog",
                AutoDiscovery = true
            };

            var result = trackback.Send(url, parameters);

            // Verify

            webRequest1.VerifyAll();
            webResponse1.VerifyAll();

            Assert.False(result.Success);
            Assert.Equal(0, result.Code);
            Assert.True(String.IsNullOrEmpty(result.Message));
            Assert.IsType<LinkbackSendException>(result.SendException);
            Assert.Equal("RDF not found while discovering Trackback url for http://target/post/1", result.SendException.Message);
        }
예제 #14
0
        protected override Uri DiscoveryTargetUrl(Uri sendUrl, LinkbackSendParameters parameters)
        {
            HttpWebRequestAbstraction request = HttpWebRequestAbstraction.Create(sendUrl);
            using (HttpWebResponseAbstraction response = request.GetResponse())
            {
                if (response.IsHttpStatusCode2XX)
                {
                    return FindTargetUrl(sendUrl, response);
                }
            }

            throw new LinkbackSendException(String.Format(CultureInfo.InvariantCulture, "Http error while discovering {0} url for {1}", Name, sendUrl));
        }
예제 #15
0
        public void LinkbackSendParameters_SetupRequestForPingback_TargetUrl_Null_Throws_InvalidOperationException()
        {
            var parameters = new LinkbackSendParameters();

            parameters.SourceUrl = new Uri("http://localhost");
            parameters.TargetUrl = null;

            var requestImplementation = new Mock<IHttpWebRequestImplementation>();
            var request = new HttpWebRequestAbstraction(requestImplementation.Object);

            Assert.Throws<InvalidOperationException>(() =>
            {
                parameters.SetupRequestForPingback(request);
            });
        }
예제 #16
0
        public void Invalid_Response_LinkbackSendException_Saved()
        {
            // Setup

            var webRequest1 = new Mock<IHttpWebRequestImplementation>(MockBehavior.Strict);
            var webResponse1 = new Mock<IHttpWebResponseImplementation>(MockBehavior.Strict);

            webRequest1.Setup(x => x.Create(new Uri("http://target/1", UriKind.Absolute))).Returns(webRequest1.Object);
            webRequest1.Setup(x => x.GetResponse()).Returns(webResponse1.Object);

            webResponse1.Setup(x => x.Close());
            webResponse1.SetupGet(x => x.IsHttpStatusCode2XX).Returns(true);
            webResponse1.SetupGet(x => x.Headers).Returns(
                new System.Net.WebHeaderCollection {
                    {"X-Pingback", "http://target/pingback"}
                });

            var webRequest2 = new Mock<IHttpWebRequestImplementation>(MockBehavior.Strict);
            var webResponse2 = new Mock<IHttpWebResponseImplementation>(MockBehavior.Strict);

            webRequest1.Setup(x => x.Create(new Uri("http://target/pingback", UriKind.Absolute))).Returns(webRequest2.Object);
            webRequest2.SetupSet(x => x.Method = "POST");
            webRequest2.SetupSet(x => x.ContentType = "text/xml");
            webRequest2.SetupSet(x => x.ContentLength = 225);
            var requestStream = new MemoryStream();
            webRequest2.Setup(x => x.GetRequestStream()).Returns(requestStream);
            webRequest2.Setup(x => x.GetResponse()).Returns(webResponse2.Object);

            webResponse2.Setup(x => x.Close());
            webResponse2.SetupGet(x => x.IsHttpStatusCode2XX).Returns(true);
            webResponse2.SetupGet(x => x.ContentEncoding).Returns(String.Empty);
            webResponse2.Setup(x => x.GetResponseStream()).Returns(new MemoryStream(new UTF8Encoding().GetBytes("... invalid response ...")));

            // Test

            var pingback = new Pingback(webRequest1.Object);

            var url = new Uri("http://target/1");

            var parameters = new LinkbackSendParameters
            {
                SourceUrl = new Uri("http://source/1"),
                TargetUrl = new Uri("http://target/1")
            };

            var result = pingback.Send(url, parameters);

            // Verify

            webRequest1.VerifyAll();
            webResponse1.VerifyAll();
            webRequest2.VerifyAll();
            webResponse2.VerifyAll();

            Assert.False(result.Success);
            Assert.Equal(0, result.Code);
            Assert.True(String.IsNullOrEmpty(result.Message));
            Assert.IsType<LinkbackSendException>(result.SendException);
            Assert.Equal("Invalid response received from http://target/pingback", result.SendException.Message);
        }
예제 #17
0
        public void Discovery_Http_Error_Then_LinkbackSendException_Saved()
        {
            // Setup

            var webRequest1 = new Mock<IHttpWebRequestImplementation>(MockBehavior.Strict);
            var webResponse1 = new Mock<IHttpWebResponseImplementation>(MockBehavior.Strict);

            webRequest1.Setup(x => x.Create(new Uri("http://target/1", UriKind.Absolute))).Returns(webRequest1.Object);
            webRequest1.Setup(x => x.GetResponse()).Returns(webResponse1.Object);

            webResponse1.Setup(x => x.Close());
            webResponse1.SetupGet(x => x.IsHttpStatusCode2XX).Returns(false);

            // Test

            var pingback = new Pingback(webRequest1.Object);

            var url = new Uri("http://target/1");

            var parameters = new LinkbackSendParameters
            {
                SourceUrl = new Uri("http://source/1"),
                TargetUrl = new Uri("http://target/1")
            };

            var result = pingback.Send(url, parameters);

            // Verify

            webRequest1.VerifyAll();
            webResponse1.VerifyAll();

            Assert.False(result.Success);
            Assert.Equal(0, result.Code);
            Assert.True(String.IsNullOrEmpty(result.Message));
            Assert.IsType<LinkbackSendException>(result.SendException);
            Assert.Equal("Http error while discovering Pingback url for http://target/1", result.SendException.Message);
        }
예제 #18
0
 protected override void SetupRequest(HttpWebRequestAbstraction request, LinkbackSendParameters parameters)
 {
     parameters.SetupRequestForPingback(request);
 }
예제 #19
0
        public void Autodiscovery_Http_Error_But_Autodiscovery_Is_True_Then_LinkbackSendException_Saved()
        {
            // Setup

            var webRequest1 = new Mock<IHttpWebRequestImplementation>(MockBehavior.Strict);
            var webResponse1 = new Mock<IHttpWebResponseImplementation>(MockBehavior.Strict);

            webRequest1.Setup(x => x.Create(new Uri("http://target/post/1"))).Returns(webRequest1.Object);
            webRequest1.Setup(x => x.GetResponse()).Returns(webResponse1.Object);

            webResponse1.SetupGet(x => x.IsHttpStatusCode2XX).Returns(false);
            webResponse1.Setup(x => x.Close());

            // Test

            var trackback = new Trackback(webRequest1.Object);

            var url = new Uri("http://target/post/1");

            var parameters = new LinkbackSendParameters
            {
                Title = "Source Title",
                Excerpt = "ABC",
                Url = new Uri("http://source/1"),
                BlogName = "Test Blog",
                AutoDiscovery = true
            };

            var result = trackback.Send(url, parameters);

            // Verify

            webRequest1.VerifyAll();
            webResponse1.VerifyAll();

            Assert.False(result.Success);
            Assert.Equal(0, result.Code);
            Assert.True(String.IsNullOrEmpty(result.Message));
            Assert.IsType<LinkbackSendException>(result.SendException);
            Assert.Equal("Http error while discovering Trackback url for http://target/post/1", result.SendException.Message);
        }
예제 #20
0
        public void Unknown_Exceptions_Are_Not_Handled()
        {
            // Setup

            var webRequest1 = new Mock<IHttpWebRequestImplementation>(MockBehavior.Strict);
            var webResponse1 = new Mock<IHttpWebResponseImplementation>(MockBehavior.Strict);

            webRequest1.Setup(x => x.Create(new Uri("http://target/1", UriKind.Absolute))).Returns(webRequest1.Object);
            webRequest1.Setup(x => x.GetResponse()).Throws(new InvalidOperationException());

            // Test

            var pingback = new Pingback(webRequest1.Object);

            var url = new Uri("http://target/1");

            var parameters = new LinkbackSendParameters
            {
                SourceUrl = new Uri("http://source/1"),
                TargetUrl = new Uri("http://target/1")
            };

            InvalidOperationException exception = null;

            try
            {
                pingback.Send(url, parameters);
            }
            catch (InvalidOperationException ex)
            {
                exception = ex;
            }

            // Verify

            Assert.NotNull(exception);
        }
예제 #21
0
        public void Unknown_Exceptions_Are_Not_Handled()
        {
            // Setup

            var webRequest1 = new Mock<IHttpWebRequestImplementation>(MockBehavior.Strict);
            var webResponse1 = new Mock<IHttpWebResponseImplementation>(MockBehavior.Strict);

            webRequest1.Setup(x => x.Create(new Uri("http://target/post/1"))).Returns(webRequest1.Object);
            webRequest1.Setup(x => x.GetResponse()).Throws(new InvalidOperationException());

            // Test

            var trackback = new Trackback(webRequest1.Object);

            var url = new Uri("http://target/post/1");

            var parameters = new LinkbackSendParameters
            {
                Title = "Source Title",
                Excerpt = "ABC",
                Url = new Uri("http://source/1"),
                BlogName = "Test Blog"
            };

            InvalidOperationException exception = null;

            try
            {
                trackback.Send(url, parameters);
            }
            catch (InvalidOperationException ex)
            {
                exception = ex;
            }

            // Verify

            Assert.NotNull(exception);
        }
예제 #22
0
        public void WebException_Saved()
        {
            // Setup

            var webRequest1 = new Mock<IHttpWebRequestImplementation>(MockBehavior.Strict);
            var webResponse1 = new Mock<IHttpWebResponseImplementation>(MockBehavior.Strict);

            webRequest1.Setup(x => x.Create(new Uri("http://target/1", UriKind.Absolute))).Returns(webRequest1.Object);
            webRequest1.Setup(x => x.GetResponse()).Throws(new System.Net.WebException("WebException"));

            // Test

            var pingback = new Pingback(webRequest1.Object);

            var url = new Uri("http://target/1");

            var parameters = new LinkbackSendParameters
            {
                SourceUrl = new Uri("http://source/1"),
                TargetUrl = new Uri("http://target/1")
            };

            var result = pingback.Send(url, parameters);

            // Verify

            webRequest1.VerifyAll();
            webResponse1.VerifyAll();

            Assert.False(result.Success);
            Assert.Equal(0, result.Code);
            Assert.True(String.IsNullOrEmpty(result.Message));
            Assert.IsType<System.Net.WebException>(result.SendException);
        }
예제 #23
0
        public void Excerpt_Not_Specified()
        {
            // Setup

            var webRequest1 = new Mock<IHttpWebRequestImplementation>(MockBehavior.Strict);
            var webResponse1 = new Mock<IHttpWebResponseImplementation>(MockBehavior.Strict);

            webRequest1.Setup(x => x.Create(new Uri("http://target/post/1"))).Returns(webRequest1.Object);
            webRequest1.Setup(x => x.GetResponse()).Returns(webResponse1.Object);

            webResponse1.SetupGet(x => x.IsHttpStatusCode2XX).Returns(true);
            webResponse1.SetupGet(x => x.ContentEncoding).Returns(String.Empty);
            string rdf = String.Format(@"
            ...
            <p>
            <rdf:RDF xmlns:rdf=""http://www.w3.org/1999/02/22-rdf-syntax-ns#""
             xmlns:dc=""http://purl.org/dc/elements/1.1/""
             xmlns:trackback=""http://madskills.com/public/xml/rss/module/trackback/"">
            <rdf:Description rdf:about=""{0}""
                     dc:identifier=""{0}""
                     dc:title=""{1}""
                     trackback:ping=""{2}"" />
            </rdf:RDF>
            </p>
            ...", "http://target/post/1", "1", "http://target/post/1/trackback");
            webResponse1.Setup(x => x.GetResponseStream()).Returns(new MemoryStream(new UTF8Encoding().GetBytes(rdf)));
            webResponse1.Setup(x => x.Close());

            var webRequest2 = new Mock<IHttpWebRequestImplementation>(MockBehavior.Strict);
            var webResponse2 = new Mock<IHttpWebResponseImplementation>(MockBehavior.Strict);

            webRequest1.Setup(x => x.Create(new Uri("http://target/post/1/trackback"))).Returns(webRequest2.Object);
            webRequest2.SetupSet(x => x.Method = "POST");
            webRequest2.SetupSet(x => x.ContentType = "application/x-www-form-urlencoded");
            webRequest2.SetupSet(x => x.ContentLength = 70);
            var requestStream = new MemoryStream();
            webRequest2.Setup(x => x.GetRequestStream()).Returns(requestStream);
            webRequest2.Setup(x => x.GetResponse()).Returns(webResponse2.Object);

            webResponse2.Setup(x => x.Close());
            webResponse2.SetupGet(x => x.IsHttpStatusCode2XX).Returns(true);
            webResponse2.SetupGet(x => x.ContentEncoding).Returns(String.Empty);
            webResponse2.Setup(x => x.GetResponseStream()).Returns(new MemoryStream(new UTF8Encoding().GetBytes("<?xml version=\"1.0\" encoding=\"utf-8\"?><response><error>0</error></response>")));

            // Test

            var trackback = new Trackback(webRequest1.Object);

            var url = new Uri("http://target/post/1");

            var parameters = new LinkbackSendParameters
            {
                Title = "Source Title",
                Url = new Uri("http://source/1"),
                BlogName = "Test Blog"
            };

            var result = trackback.Send(url, parameters);

            // Verify

            webRequest1.VerifyAll();
            webResponse1.VerifyAll();
            webRequest2.VerifyAll();
            webResponse2.VerifyAll();

            Assert.Equal("url=http%3A%2F%2Fsource%2F1&title=Source%20Title&blog_name=Test%20Blog", new UTF8Encoding().GetString(requestStream.ToArray()));

            Assert.True(result.Success);
            Assert.Equal(0, result.Code);
            Assert.True(String.IsNullOrEmpty(result.Message));
        }
예제 #24
0
        public ActionResult Send(ILinkback linkback, int id, string url, bool? autodiscovery)
        {
            var post = _db.Post.First(x => x.Id == id);

            string source_url = Url.AbsoluteRouteUrl("Post", new { id });

            var parameters = new LinkbackSendParameters
            {
                // Trackback
                Title = post.Title,
                Excerpt = post.Content,
                Url = new Uri(source_url),
                BlogName = "Linkback.NET Demo",
                AutoDiscovery = autodiscovery,

                // Linkback
                SourceUrl = new Uri(source_url),
                TargetUrl = new Uri(url)
            };

            var result = linkback.Send(new Uri(url), parameters);

            TempData["Linkback-Send-Result"] = result.Success
                ? String.Format("{0} for {1} sent", linkback.Name, url)
                : String.Format("Error: {0}({1})", result.Message, result.Code);

            return RedirectToRoute(String.Format("{0}-Send", linkback.Name), new { id });
        }
예제 #25
0
        public void LinkbackSendParameters_SetupRequestForTrackback_Url_Null_Throws_InvalidOperationException()
        {
            var parameters = new LinkbackSendParameters();

            var requestImplementation = new Mock<IHttpWebRequestImplementation>();
            var request = new HttpWebRequestAbstraction(requestImplementation.Object);

            Assert.Throws<InvalidOperationException>(() =>
            {
                parameters.SetupRequestForTrackback(request);
            });
        }
예제 #26
0
        public ISendResult Send(Uri sendUrl, LinkbackSendParameters parameters)
        {
            HandleExceptionsOnSend(
                () => {
                    Uri targetUrl = DiscoveryTargetUrl(sendUrl, parameters);

                    SendToTarget(targetUrl, parameters);
                });

            return this as ISendResult;
        }
예제 #27
0
        public void Response_Error()
        {
            // Setup

            var webRequest1 = new Mock<IHttpWebRequestImplementation>(MockBehavior.Strict);
            var webResponse1 = new Mock<IHttpWebResponseImplementation>(MockBehavior.Strict);

            webRequest1.Setup(x => x.Create(new Uri("http://target/post/1"))).Returns(webRequest1.Object);
            webRequest1.Setup(x => x.GetResponse()).Returns(webResponse1.Object);

            webResponse1.SetupGet(x => x.IsHttpStatusCode2XX).Returns(true);
            webResponse1.SetupGet(x => x.ContentEncoding).Returns(String.Empty);
            string rdf = String.Format("... <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:trackback=\"http://madskills.com/public/xml/rss/module/trackback/\"><rdf:Description rdf:about=\"{0}\" dc:identifier=\"{0}\" dc:title=\"{1}\" trackback:ping=\"{2}\" /></rdf:RDF> ...", "http://target/post/1", "1", "http://target/post/1/trackback");
            webResponse1.Setup(x => x.GetResponseStream()).Returns(new MemoryStream(new UTF8Encoding().GetBytes(rdf)));
            webResponse1.Setup(x => x.Close());

            var webRequest2 = new Mock<IHttpWebRequestImplementation>(MockBehavior.Strict);
            var webResponse2 = new Mock<IHttpWebResponseImplementation>(MockBehavior.Strict);

            webRequest1.Setup(x => x.Create(new Uri("http://target/post/1/trackback"))).Returns(webRequest2.Object);
            webRequest2.SetupSet(x => x.Method = "POST");
            webRequest2.SetupSet(x => x.ContentType = "application/x-www-form-urlencoded");
            webRequest2.SetupSet(x => x.ContentLength = 82);
            var requestStream = new MemoryStream();
            webRequest2.Setup(x => x.GetRequestStream()).Returns(requestStream);
            webRequest2.Setup(x => x.GetResponse()).Returns(webResponse2.Object);

            webResponse2.Setup(x => x.Close());
            webResponse2.SetupGet(x => x.IsHttpStatusCode2XX).Returns(true);
            webResponse2.SetupGet(x => x.ContentEncoding).Returns(String.Empty);
            webResponse2.Setup(x => x.GetResponseStream()).Returns(new MemoryStream(new UTF8Encoding().GetBytes("<?xml version=\"1.0\" encoding=\"utf-8\"?><response><error>1</error><message>Error</message></response>")));

            // Test

            var trackback = new Trackback(webRequest1.Object);

            var url = new Uri("http://target/post/1");

            var parameters = new LinkbackSendParameters
            {
                Title = "Source Title",
                Excerpt = "ABC",
                Url = new Uri("http://source/1"),
                BlogName = "Test Blog"
            };

            var result = trackback.Send(url, parameters);

            // Verify

            webRequest1.VerifyAll();
            webResponse1.VerifyAll();
            webRequest2.VerifyAll();
            webResponse2.VerifyAll();

            Assert.True(result.Success);
            Assert.Equal(1, result.Code);
            Assert.Equal("Error", result.Message);
        }
예제 #28
0
 protected abstract Uri DiscoveryTargetUrl(Uri sendUrl, LinkbackSendParameters parameters);
예제 #29
0
        public void Discovery_Header()
        {
            // Setup

            var webRequest1 = new Mock<IHttpWebRequestImplementation>(MockBehavior.Strict);
            var webResponse1 = new Mock<IHttpWebResponseImplementation>(MockBehavior.Strict);

            webRequest1.Setup(x => x.Create(new Uri("http://target/1", UriKind.Absolute))).Returns(webRequest1.Object);
            webRequest1.Setup(x => x.GetResponse()).Returns(webResponse1.Object);

            webResponse1.Setup(x => x.Close());
            webResponse1.SetupGet(x => x.IsHttpStatusCode2XX).Returns(true);
            webResponse1.SetupGet(x => x.Headers).Returns(
                new System.Net.WebHeaderCollection {
                    {"X-Pingback", "http://target/pingback"}
                });

            var webRequest2 = new Mock<IHttpWebRequestImplementation>(MockBehavior.Strict);
            var webResponse2 = new Mock<IHttpWebResponseImplementation>(MockBehavior.Strict);

            webRequest1.Setup(x => x.Create(new Uri("http://target/pingback", UriKind.Absolute))).Returns(webRequest2.Object);
            webRequest2.SetupSet(x => x.Method = "POST");
            webRequest2.SetupSet(x => x.ContentType = "text/xml");
            webRequest2.SetupSet(x => x.ContentLength = 225);
            var requestStream = new MemoryStream();
            webRequest2.Setup(x => x.GetRequestStream()).Returns(requestStream);
            webRequest2.Setup(x => x.GetResponse()).Returns(webResponse2.Object);

            webResponse2.Setup(x => x.Close());
            webResponse2.SetupGet(x => x.IsHttpStatusCode2XX).Returns(true);
            webResponse2.SetupGet(x => x.ContentEncoding).Returns(String.Empty);
            webResponse2.Setup(x => x.GetResponseStream()).Returns(new MemoryStream(new UTF8Encoding().GetBytes("<?xml version=\"1.0\"?><methodResponse><params><param><value><string>ok</string></value></param></params></methodResponse>")));

            // Test

            var pingback = new Pingback(webRequest1.Object);

            var url = new Uri("http://target/1");

            var parameters = new LinkbackSendParameters
            {
                SourceUrl = new Uri("http://source/1"),
                TargetUrl = new Uri("http://target/1")
            };

            var result = pingback.Send(url, parameters);

            // Verify

            webRequest1.VerifyAll();
            webResponse1.VerifyAll();
            webRequest2.VerifyAll();
            webResponse2.VerifyAll();

            Assert.True(result.Success);
            Assert.Equal(0, result.Code);
            Assert.True(String.IsNullOrEmpty(result.Message));
        }
예제 #30
0
 protected abstract Uri DiscoveryTargetUrl(Uri sendUrl, LinkbackSendParameters parameters);