Exemplo n.º 1
0
        public void BlogName_Encoded()
        {
            // Setup

            var request = new Mock<HttpRequestBase>(MockBehavior.Strict);

            request.SetupGet(x => x.Form).Returns(
                new NameValueCollection {
                    {"title", "Source Title"},
                    {"excerpt", "ABC"},
                    {"url", "http://source/1"},
                    {"blog_name", "Test <script /> Blog"}
                });

            var webRequest = new Mock<IHttpWebRequestImplementation>(MockBehavior.Strict);
            var webResponse = new Mock<IHttpWebResponseImplementation>(MockBehavior.Strict);

            webRequest.Setup(x => x.Create(new Uri("http://source/1"))).Returns(webRequest.Object);
            webRequest.Setup(x => x.GetResponse()).Returns(webResponse.Object);

            webResponse.Setup(x => x.Close());
            webResponse.SetupGet(x => x.IsHttpStatusCode2XX).Returns(true);
            string source = "... <a href=\"http://taRget/post/1\">post1</a> ...";
            webResponse.Setup(x => x.GetResponseStream()).Returns(new MemoryStream(new UTF8Encoding().GetBytes(source)));

            // Test

            var trackback = new Trackback(webRequest.Object);

            var result = trackback.Receive(request.Object, new Uri("http://target/post/1"));

            // Verify

            webRequest.VerifyAll();
            webResponse.VerifyAll();
            request.VerifyAll();

            Assert.True(result.Valid);
            Assert.Equal("Test &lt;script /&gt; Blog", result.BlogName);
        }
Exemplo n.º 2
0
        public void TargetUrl_Http()
        {
            // Setup

            var request = new Mock <HttpRequestBase>(MockBehavior.Strict);

            request.SetupGet(x => x.Form).Returns(
                new NameValueCollection {
                { "title", "Source Title" },
                { "excerpt", "ABC" },
                { "url", "http://source/1" },
                { "blog_name", "Test Blog" }
            });

            var webRequest  = new Mock <IHttpWebRequestImplementation>(MockBehavior.Strict);
            var webResponse = new Mock <IHttpWebResponseImplementation>(MockBehavior.Strict);

            webRequest.Setup(x => x.Create(new Uri("http://source/1"))).Returns(webRequest.Object);
            webRequest.Setup(x => x.GetResponse()).Returns(webResponse.Object);

            webResponse.Setup(x => x.Close());
            webResponse.SetupGet(x => x.IsHttpStatusCode2XX).Returns(true);
            string source = "... <a href=\"http://taRget/post/1\">post1</a> ...";

            webResponse.Setup(x => x.GetResponseStream()).Returns(new MemoryStream(new UTF8Encoding().GetBytes(source)));

            // Test

            var trackback = new Trackback(webRequest.Object);

            var result = trackback.Receive(request.Object, new Uri("http://target/post/1"));

            // Verify

            webRequest.VerifyAll();
            webResponse.VerifyAll();
            request.VerifyAll();

            Assert.True(result.Valid);
        }
Exemplo n.º 3
0
        public void Source_Discovery_Http_Error_LinkbackReceiveException_Saved()
        {
            // Setup

            var request = new Mock <HttpRequestBase>(MockBehavior.Strict);

            request.SetupGet(x => x.Form).Returns(
                new NameValueCollection {
                { "title", "Source Title" },
                { "excerpt", "ABC" },
                { "url", "http://source/post/1" },
                { "blog_name", "Test Blog" }
            });

            var webRequest  = new Mock <IHttpWebRequestImplementation>(MockBehavior.Strict);
            var webResponse = new Mock <IHttpWebResponseImplementation>(MockBehavior.Strict);

            webRequest.Setup(x => x.Create(new Uri("http://source/post/1"))).Returns(webRequest.Object);
            webRequest.Setup(x => x.GetResponse()).Returns(webResponse.Object);

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

            // Test

            var trackback = new Trackback(webRequest.Object);

            var result = trackback.Receive(request.Object, new Uri("http://target/post/1"));

            // Verify

            webRequest.VerifyAll();
            webResponse.VerifyAll();
            request.VerifyAll();

            Assert.False(result.Valid);
            Assert.IsType <LinkbackReceiveException>(result.ReceiveException);
            Assert.Equal("Http error while discovering Trackback source at http://source/post/1", result.ReceiveException.Message);
        }
Exemplo n.º 4
0
        public void WebException_Saved()
        {
            // Setup

            var request = new Mock<HttpRequestBase>(MockBehavior.Strict);

            request.SetupGet(x => x.Form).Returns(
                new NameValueCollection {
                    {"title", "Source Title"},
                    {"excerpt", "ABC"},
                    {"url", "http://source/post/1"},
                    {"blog_name", "Test Blog"}
                });

            var webRequest = new Mock<IHttpWebRequestImplementation>(MockBehavior.Strict);

            webRequest.Setup(x => x.Create(new Uri("http://source/post/1"))).Returns(webRequest.Object);
            webRequest.Setup(x => x.GetResponse()).Throws(new System.Net.WebException("WebException"));

            // Test

            var trackback = new Trackback(webRequest.Object);

            var result = trackback.Receive(request.Object, new Uri("http://target/post/1"));

            // Verify

            webRequest.VerifyAll();
            request.VerifyAll();

            Assert.False(result.Valid);
            Assert.IsType<System.Net.WebException>(result.ReceiveException);
            Assert.Equal("WebException", result.ReceiveException.Message);
        }
Exemplo n.º 5
0
        public void Url_Not_Specified_LinkbackReceiveException_Saved()
        {
            // Setup

            var request = new Mock<HttpRequestBase>(MockBehavior.Strict);

            request.SetupGet(x => x.Form).Returns(
                new NameValueCollection {
                    {"title", "Source Title"},
                    {"excerpt", "ABC"},
                    {"blog_name", "Test Blog"}
                });

            var webRequest = new Mock<IHttpWebRequestImplementation>(MockBehavior.Strict);

            // Test

            var trackback = new Trackback(webRequest.Object);

            var result = trackback.Receive(request.Object, new Uri("http://target/post/1"));

            // Verify

            webRequest.VerifyAll();
            request.VerifyAll();

            Assert.False(result.Valid);
            Assert.IsType<LinkbackReceiveException>(result.ReceiveException);
            Assert.Equal("Url parameter for Trackback not specified", result.ReceiveException.Message);
        }
Exemplo n.º 6
0
        public void Uri_Not_Http_Or_Https_UriFormatException_Saved()
        {
            // Setup

            var request = new Mock<HttpRequestBase>(MockBehavior.Strict);

            request.SetupGet(x => x.Form).Returns(
                new NameValueCollection {
                    {"title", "Source Title"},
                    {"excerpt", "ABC"},
                    {"url", "ftp://source/1"},
                    {"blog_name", "Test Blog"}
                });

            var webRequest = new Mock<IHttpWebRequestImplementation>(MockBehavior.Strict);

            // Test

            var trackback = new Trackback(webRequest.Object);

            var result = trackback.Receive(request.Object, new Uri("http://target/post/1"));

            // Verify

            webRequest.VerifyAll();
            request.VerifyAll();

            Assert.False(result.Valid);
            Assert.IsType<UriFormatException>(result.ReceiveException);
            Assert.Equal("Url scheme must be http or https", result.ReceiveException.Message);
        }
Exemplo n.º 7
0
        public void Uri_Invalid_UriFormatException_Saved()
        {
            // Setup

            var request = new Mock<HttpRequestBase>(MockBehavior.Strict);

            request.SetupGet(x => x.Form).Returns(
                new NameValueCollection {
                    {"title", "Source Title"},
                    {"excerpt", "ABC"},
                    {"url", "~this~is~not~an~url*xxx://"},
                    {"blog_name", "Test Blog"}
                });

            var webRequest = new Mock<IHttpWebRequestImplementation>(MockBehavior.Strict);

            // Test

            var trackback = new Trackback(webRequest.Object);

            var result = trackback.Receive(request.Object, new Uri("http://target/post/1"));

            // Verify

            webRequest.VerifyAll();
            request.VerifyAll();

            Assert.False(result.Valid);
            Assert.IsType<UriFormatException>(result.ReceiveException);
        }
Exemplo n.º 8
0
        public void TargetUrl_Null_ArgumentNullException()
        {
            // Setup

            var request = new Mock<HttpRequestBase>(MockBehavior.Strict);

            var webRequest = new Mock<IHttpWebRequestImplementation>(MockBehavior.Strict);

            // Test

            var trackback = new Trackback(webRequest.Object);

            ArgumentNullException ex = null;

            try
            {
                trackback.Receive(request.Object, null);
            }
            catch (ArgumentNullException _ex)
            {
                ex = _ex;
            }

            // Verify

            webRequest.VerifyAll();
            request.VerifyAll();

            Assert.NotNull(ex);
            Assert.Equal("targetUrl", ex.ParamName);
        }
Exemplo n.º 9
0
        public void Source_Discovery_Http_Error_LinkbackReceiveException_Saved()
        {
            // Setup

            var request = new Mock<HttpRequestBase>(MockBehavior.Strict);

            request.SetupGet(x => x.Form).Returns(
                new NameValueCollection {
                    {"title", "Source Title"},
                    {"excerpt", "ABC"},
                    {"url", "http://source/post/1"},
                    {"blog_name", "Test Blog"}
                });

            var webRequest = new Mock<IHttpWebRequestImplementation>(MockBehavior.Strict);
            var webResponse = new Mock<IHttpWebResponseImplementation>(MockBehavior.Strict);

            webRequest.Setup(x => x.Create(new Uri("http://source/post/1"))).Returns(webRequest.Object);
            webRequest.Setup(x => x.GetResponse()).Returns(webResponse.Object);

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

            // Test

            var trackback = new Trackback(webRequest.Object);

            var result = trackback.Receive(request.Object, new Uri("http://target/post/1"));

            // Verify

            webRequest.VerifyAll();
            webResponse.VerifyAll();
            request.VerifyAll();

            Assert.False(result.Valid);
            Assert.IsType<LinkbackReceiveException>(result.ReceiveException);
            Assert.Equal("Http error while discovering Trackback source at http://source/post/1", result.ReceiveException.Message);
        }
Exemplo n.º 10
0
        public void Discovery_Source_Has_No_Link()
        {
            // Setup

            var request = new Mock<HttpRequestBase>(MockBehavior.Strict);

            request.SetupGet(x => x.Form).Returns(
                new NameValueCollection
                {
                    {"title", "Source Title"},
                    {"excerpt", "ABC"},
                    {"url", "http://source/1"},
                    {"blog_name", "Test Blog"}
                });

            var webRequest = new Mock<IHttpWebRequestImplementation>(MockBehavior.Strict);
            var webResponse = new Mock<IHttpWebResponseImplementation>(MockBehavior.Strict);

            webRequest.Setup(x => x.Create(new Uri("http://source/1"))).Returns(webRequest.Object);
            webRequest.Setup(x => x.GetResponse()).Returns(webResponse.Object);

            webResponse.Setup(x => x.Close());
            webResponse.SetupGet(x => x.IsHttpStatusCode2XX).Returns(true);
            string source = "... http://target/any ...";
            webResponse.Setup(x => x.GetResponseStream()).Returns(new MemoryStream(new UTF8Encoding().GetBytes(source)));

            // Test

            var trackback = new Trackback(webRequest.Object);

            var result = trackback.Receive(request.Object, new Uri("http://target/post/1"));

            // Verify

            webRequest.VerifyAll();
            webResponse.VerifyAll();
            request.VerifyAll();

            Assert.False(result.Valid);
            Assert.Equal("Source Title", result.Title);
            Assert.Equal("ABC", result.Excerpt);
            Assert.Equal("http://source/1", result.Url.ToString().ToLowerInvariant());
            Assert.Equal("Test Blog", result.BlogName);
        }