예제 #1
0
        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!");
        }
예제 #2
0
 public AkismetSpamService(string apiKey, Blog blog, AkismetClient akismetClient, BlogUrlHelper urlHelper)
 {
     _blog = blog;
     _akismet = akismetClient ?? new AkismetClient(apiKey, urlHelper.BlogUrl().ToFullyQualifiedUrl(blog));
     IWebProxy proxy = HttpHelper.GetProxy();
     if (proxy != null)
     {
         _akismet.Proxy = proxy;
     }
     _urlHelper = urlHelper ?? new BlogUrlHelper(null, null);
 }
예제 #3
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();
        }
예제 #4
0
        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!");
        }
예제 #5
0
        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));
        }
예제 #6
0
        public void SubmitHamTest(string urlEnding, bool isHam)
        {
            string userAgent = GetExpectedUserAgent();
            var checkUrl = new Uri("http://myapikey.rest.akismet.com/1.1/" + urlEnding);
            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(string.Empty);

            var client = new AkismetClient("myapikey", new Uri("http://haacked.com/"), httpClient.Object);
            if(isHam)
            {
                client.SubmitHam(comment.Object);
            }
            else
            {
                client.SubmitSpam(comment.Object);
            }
        }
예제 #7
0
 public void ConstructorSetsApiKeyAndUrl()
 {
     var client = new AkismetClient("fake-key", new Uri("http://haacked.com/"), new HttpClient());
     Assert.AreEqual(new Uri("http://haacked.com/"), client.BlogUrl);
     Assert.AreEqual("fake-key", client.ApiKey);
     UnitTestHelper.AssertSimpleProperties(client, "ApiKey");
 }
예제 #8
0
        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));
        }
예제 #9
0
        public void CanVerifyApiKeyIsWrong()
        {
            //act
            string userAgent = GetExpectedUserAgent();
            var verifyUrl = new Uri("http://rest.akismet.com/1.1/verify-key");
            string parameters = "key=" + HttpUtility.UrlEncode("wrong-key") + "&blog=" +
                                HttpUtility.UrlEncode("http://haacked.com/");
            var httpClient = new Mock<HttpClient>();
            httpClient.Setup(hc => hc.PostRequest(verifyUrl, userAgent, 5000, parameters, null)).Returns("invalid");
            var client = new AkismetClient("wrong-key", new Uri("http://haacked.com/"), httpClient.Object);

            //act
            bool isVerified = client.VerifyApiKey();

            //assert
            Assert.IsFalse(isVerified, "If the request returns 'invalid' then we should return false!");
        }
예제 #10
0
 public AkismetSpamBlockingService(string apiKey, string blogUrl)
 {
     akismetClient = new AkismetClient(apiKey, new Uri(blogUrl));
 }
예제 #11
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();
        }
예제 #12
0
        public void SubmitHamTest(string urlEnding, bool isHam)
        {
            string userAgent = GetExpectedUserAgent();
            Uri checkUrl = new Uri("http://myapikey.rest.akismet.com/1.1/" + urlEnding);
            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(string.Empty);
            mocks.ReplayAll();

            AkismetClient client = new AkismetClient("myapikey", new Uri("http://haacked.com/"), httpClient);
            if (isHam)
                client.SubmitHam(comment);
            else
                client.SubmitSpam(comment);

            mocks.VerifyAll();
        }
예제 #13
0
 public void CheckCommentThrowsArgumentNullException()
 {
     AkismetClient client = new AkismetClient("fake-key", new Uri("http://haacked.com/"), null);
     client.CheckCommentForSpam(null);
 }
예제 #14
0
        public void CanVerifyApiKeyIsWrong()
        {
            string userAgent = GetExpectedUserAgent();
            Uri verifyUrl = new Uri("http://rest.akismet.com/1.1/verify-key");
            string parameters = "key=" + HttpUtility.UrlEncode("wrong-key") + "&blog=" + HttpUtility.UrlEncode("http://haacked.com/");

            MockRepository mocks = new MockRepository();
            HttpClient httpClient = (HttpClient)mocks.CreateMock(typeof(HttpClient));
            Expect.Call(httpClient.PostRequest(verifyUrl, userAgent, 5000, parameters)).Return("invalid");
            mocks.ReplayAll();

            AkismetClient client = new AkismetClient("wrong-key", new Uri("http://haacked.com/"), httpClient);
            Assert.IsFalse(client.VerifyApiKey(), "If the request returns 'invalid' then we should return false!");

            mocks.VerifyAll();
        }