CheckCommentForSpam() public méthode

Checks the comment and returns true if it is spam, otherwise false.
public CheckCommentForSpam ( IComment comment ) : bool
comment IComment
Résultat bool
Exemple #1
0
        public void CanCheckCommentForSpam()
        {
            string userAgent = GetExpectedUserAgent();
            Uri checkUrl = new Uri("http://myapikey.rest.akismet.com/1.1/comment-check");
            string parameters = "blog=" + HttpUtility.UrlEncode("http://haacked.com/")
                                + "&user_ip=10.0.0.1"
                                + "&user_agent=" + HttpUtility.UrlEncode("Mozilla (My Silly Browser)")
                                + "&referer=" + HttpUtility.UrlEncode("http://example.com/none-of-your-business/")
                                + "&permalink=" + HttpUtility.UrlEncode("http://example.com/i-am-right-you-are-wrong/")
                                + "&comment_type=comment"
                                + "&comment_author=Your+Mama"
                                + "&comment_author_email=" + HttpUtility.UrlEncode("*****@*****.**")
                                + "&comment_author_url=" + HttpUtility.UrlEncode("http://mysite.example.com/foo/")
                                + "&comment_content=" + HttpUtility.UrlEncode("This is my rifle. There are many like it, but this one is MINE.");

            MockRepository mocks = new MockRepository();
            HttpClient httpClient = (HttpClient)mocks.CreateMock(typeof(HttpClient));
            IComment comment = (IComment)mocks.CreateMock(typeof(IComment));

            SetupCallsAnComment(comment
                                , "Your Mama"
                                , "*****@*****.**"
                                , IPAddress.Parse("10.0.0.1")
                                , "Mozilla (My Silly Browser)"
                                , "http://example.com/none-of-your-business/"
                                , new Uri("http://example.com/i-am-right-you-are-wrong/")
                                , "comment"
                                , new Uri("http://mysite.example.com/foo/")
                                , "This is my rifle. There are many like it, but this one is MINE."
                                , null);

            Expect.Call(httpClient.PostRequest(checkUrl, userAgent, 5000, parameters)).Return("true");
            mocks.ReplayAll();

            AkismetClient client = new AkismetClient("myapikey", new Uri("http://haacked.com/"), httpClient);
            Assert.IsTrue(client.CheckCommentForSpam(comment), "If the request returns 'false' then we should return false!");

            mocks.VerifyAll();
        }
        public void CanCheckCommentWithArbitraryServerParams()
        {
            string userAgent = GetExpectedUserAgent();
            var checkUrl = new Uri("http://myapikey.rest.akismet.com/1.1/comment-check");
            string parameters = "blog=" + HttpUtility.UrlEncode("http://haacked.com/")
                                + "&user_ip=192.168.200.201"
                                + "&user_agent=" + HttpUtility.UrlEncode("Mozilla (My Silly Browser)")
                                + "&Making=" + HttpUtility.UrlEncode("This-Stuff")
                                + "&Up=" + HttpUtility.UrlEncode("As I-Go-Along");

            var httpClient = new Mock<HttpClient>();
            var comment = new Mock<IComment>();

            var extendedProps = new NameValueCollection();

            extendedProps.Add("Making", "This-Stuff");
            extendedProps.Add("Up", "As I-Go-Along");

            //We'll try a mix of nulls and empty strings.
            SetupCallsAnComment(comment
                                , string.Empty
                                , string.Empty
                                , IPAddress.Parse("192.168.200.201")
                                , "Mozilla (My Silly Browser)"
                                , null
                                , null
                                , null
                                , null
                                , string.Empty
                                , extendedProps);

            httpClient.Setup(hc => hc.PostRequest(checkUrl, userAgent, 5000, parameters)).Returns("false");

            var client = new AkismetClient("myapikey", new Uri("http://haacked.com/"), httpClient.Object);
            Assert.IsFalse(client.CheckCommentForSpam(comment.Object),
                           "If the request returns 'false' then we should return false!");
        }
        public void CanCheckCommentForSpamWithoutOptionalParams()
        {
            string userAgent = GetExpectedUserAgent();
            var checkUrl = new Uri("http://myapikey.rest.akismet.com/1.1/comment-check");
            string parameters = "blog=" + HttpUtility.UrlEncode("http://haacked.com/")
                                + "&user_ip=192.168.200.201"
                                + "&user_agent=" + HttpUtility.UrlEncode("Mozilla (My Silly Browser)");

            var httpClient = new Mock<HttpClient>();
            var comment = new Mock<IComment>();

            //We'll try a mix of nulls and empty strings.
            SetupCallsAnComment(comment
                                , string.Empty
                                , string.Empty
                                , IPAddress.Parse("192.168.200.201")
                                , "Mozilla (My Silly Browser)"
                                , null
                                , null
                                , null
                                , null
                                , string.Empty
                                , null);

            httpClient.Setup(hc => hc.PostRequest(checkUrl, userAgent, 5000, parameters)).Returns("true");

            var client = new AkismetClient("myapikey", new Uri("http://haacked.com/"), httpClient.Object);
            Assert.IsTrue(client.CheckCommentForSpam(comment.Object),
                          "If the request returns 'false' then we should return false!");
        }
        public void ThrowsInvalidResponseWhenApiKeyInvalid()
        {
            // arrange
            string userAgent = GetExpectedUserAgent();
            var checkUrl = new Uri("http://myapikey.rest.akismet.com/1.1/comment-check");
            string parameters = "blog=" + HttpUtility.UrlEncode("http://haacked.com/")
                                + "&user_ip=192.168.200.201"
                                + "&user_agent=" + HttpUtility.UrlEncode("Mozilla (My Silly Browser)");

            var httpClient = new Mock<HttpClient>();
            var comment = new Mock<IComment>();

            //We'll try a mix of nulls and empty strings.
            SetupCallsAnComment(comment
                                , string.Empty
                                , string.Empty
                                , IPAddress.Parse("192.168.200.201")
                                , "Mozilla (My Silly Browser)"
                                , null
                                , null
                                , null
                                , null
                                , string.Empty
                                , null);

            httpClient.Setup(hc => hc.PostRequest(checkUrl, userAgent, 5000, parameters)).Returns("invalid");
            var client = new AkismetClient("myapikey", new Uri("http://haacked.com/"), httpClient.Object);

            // act, assert
            UnitTestHelper.AssertThrows<InvalidResponseException>(() => client.CheckCommentForSpam(comment.Object));
        }
        public void CheckCommentForSpam_WithNullComment_ThrowsArgumentNullException()
        {
            // arrange
            var client = new AkismetClient("fake-key", new Uri("http://haacked.com/"), new HttpClient());

            // act, assert
            UnitTestHelper.AssertThrowsArgumentNullException(() => client.CheckCommentForSpam(null));
        }
Exemple #6
0
        public void CanCheckCommentForSpamWithoutOptionalParams()
        {
            string userAgent = GetExpectedUserAgent();
            Uri checkUrl = new Uri("http://myapikey.rest.akismet.com/1.1/comment-check");
            string parameters = "blog=" + HttpUtility.UrlEncode("http://haacked.com/")
                                + "&user_ip=192.168.200.201"
                                + "&user_agent=" + HttpUtility.UrlEncode("Mozilla (My Silly Browser)");

            MockRepository mocks = new MockRepository();
            HttpClient httpClient = (HttpClient)mocks.CreateMock(typeof(HttpClient));
            IComment comment = (IComment)mocks.CreateMock(typeof(IComment));

            //We'll try a mix of nulls and empty strings.
            SetupCallsAnComment(comment
                                , string.Empty
                                , string.Empty
                                , IPAddress.Parse("192.168.200.201")
                                , "Mozilla (My Silly Browser)"
                                , null
                                , null
                                , null
                                , null
                                , string.Empty
                                , null);

            Expect.Call(httpClient.PostRequest(checkUrl, userAgent, 5000, parameters)).Return("true");
            mocks.ReplayAll();

            AkismetClient client = new AkismetClient("myapikey", new Uri("http://haacked.com/"), httpClient);
            Assert.IsTrue(client.CheckCommentForSpam(comment), "If the request returns 'false' then we should return false!");

            mocks.VerifyAll();
        }
Exemple #7
0
 public void CheckCommentThrowsArgumentNullException()
 {
     AkismetClient client = new AkismetClient("fake-key", new Uri("http://haacked.com/"), null);
     client.CheckCommentForSpam(null);
 }