Permalink
Browse files

Refactored extra_headers class methods a bit, mostly permitting to

push a header or an array of headers to it.

Also added tests for spam? and ham? with a request object.
  • Loading branch information...
1 parent 75caeb5 commit a7ff55043317eaf249faf2224c3707e55f02dfbb @ysbaddaden committed Oct 24, 2010
Showing with 112 additions and 39 deletions.
  1. +35 −12 lib/akismet.rb
  2. +77 −27 test/akismet_test.rb
View
@@ -3,7 +3,7 @@
# Akismet compatible library for checking spams.
#
# Before calling any method, you must configure a blog (your website
-# homepage) and your Akismet or TypepadAntispam API key.
+# homepage) and your Akismet or Typepad Antispam API key.
#
# Akismet.key = '123456789'
# Akismet.blog = 'http://example.com'
@@ -13,7 +13,7 @@ class Akismet
class MissingKey < StandardError
end
- VERSION = '0.9.3'.freeze
+ VERSION = '1.0.0.rc'.freeze
API_VERSION = '1.1'.freeze
class << self
@@ -53,21 +53,36 @@ def logger
@@logger
end
- # Configure an array of extra HTTP headers to pass to Akismet from
- # the request.
+ # Configure an Array of extra HTTP headers to pass to the Akismet server
+ # to extract from the request object.
#
- # Example:
+ # Defaults to:
#
- # Akismet.extra_headers = [
+ # [
# 'HTTP_REMOTE_ADDR',
# 'HTTP_CLIENT_IP',
- # 'HTTP_X_FORWARDED_FOR'
+ # 'HTTP_X_FORWARDED_FOR',
+ # 'HTTP_CONNECTION'
# ]
+ #
+ # Examples:
+ #
+ # # replaces the actual list:
+ # Akismet.extra_headers = ['HTTP_REMOTE_ADDR']
+ #
+ # # appends a header to the list:
+ # Akismet.extra_headers << 'HTTP_ACCEPT_CHARSET'
+ #
+ # # appends multiple headers to the list:
+ # Akismet.extra_headers << ['HTTP_ACCEPT_CHARSET', 'HTTP_ACCEPT_LANGUAGE']
+ #
def extra_headers=(headers)
@@extra_headers = headers
end
def extra_headers
+ @@extra_headers.flatten!
+ @@extra_headers.uniq!
@@extra_headers
end
@@ -87,13 +102,18 @@ def valid_key?(key)
# - <tt>:comment_content</tt>
#
# Those are also required, but will be extracted from the
- # <tt>ActionDispatch::Request</tt> object if available:
+ # +request+ object if available:
#
# - <tt>:user_ip</tt>
# - <tt>:user_agent</tt>
- # - <tt>:referer</tt>
+ # - <tt>:referrer</tt> (check spelling!)
+ #
+ # Plus more relevant HTTP headers from extra_headers.
#
- # Plus more relevant HTTP header from extra_headers.
+ # Note that request is supposed to be an instance of
+ # ActionDispatch::Request or ActionController::Request. If not, the object
+ # must respond to +remote_ip+ (IP as string) and +headers+
+ # (an array of HTTP headers).
#
def spam?(attributes, request = nil)
call('comment-check', attributes, request) == "true"
@@ -140,7 +160,7 @@ def initialize(command, attributes, request = nil)
end
def call
- self.class.logger.debug { " AKISMET #{@command}" } if self.class.logger
+ self.class.logger.debug { " AKISMET #{@command} #{post_attributes}" } if self.class.logger
http = Net::HTTP.new(http_host, 80)
http.post(http_path, post_attributes, http_headers).body
@@ -157,7 +177,10 @@ def attributes
@attributes[:user_ip] = @request.remote_ip
@attributes[:user_agent] = @request.headers["HTTP_USER_AGENT"]
@attributes[:referrer] = @request.headers["HTTP_REFERER"]
- extra_headers.each { |h| @attributes[h] = @request.headers[h] }
+
+ self.class.extra_headers.each do |h|
+ @attributes[h] = @request.headers[h]
+ end
end
end
View
@@ -1,8 +1,13 @@
require 'test/unit'
require 'logger'
+
+require 'rubygems'
+gem 'actionpack'
+gem 'rack'
+require 'action_dispatch'
+
require File.expand_path("../../lib/akismet.rb", __FILE__)
-# TODO: Test with an ActionDispatch::TestRequest object.
class AkismetTest < Test::Unit::TestCase
def setup
Akismet.host = 'api.antispam.typepad.com'
@@ -13,27 +18,8 @@ def setup
Akismet.logger.level = Logger::DEBUG
end
- def valid_attributes
- {
- :user_ip => '127.0.0.1',
- :user_agent => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10.5; en-US; rv:1.9.0.3) Gecko/2008092414 Firefox/3.0.3',
- :referrer => 'http://www.example.com/posts',
- :permalink => 'http://www.example.com/posts/1',
- :comment_author => 'Julien Portalier',
- :comment_author_url => 'http://ysbaddaden.wordpress.com/',
- :comment_author_email => 'julien@example.com',
- :comment_content => 'this is a normal comment',
- }
- end
-
- def invalid_attributes
- invalid = valid_attributes.dup
- invalid[:comment_author] = 'viagra-test-123'
- invalid
- end
-
def test_valid_key
- assert Akismet.valid_key?('123456789')
+ assert Akismet.valid_key?(Akismet.key)
end
# def test_invalid_key
@@ -42,38 +28,102 @@ def test_valid_key
def test_should_not_fail_with_no_logger
Akismet.logger = nil
- assert Akismet.valid_key?('123456789')
+ assert Akismet.valid_key?(Akismet.key)
end
def test_should_raise_missing_key
Akismet.key = nil
- assert_raise(Akismet::MissingKey) { Akismet.spam?(valid_attributes) }
+ assert_raise(Akismet::MissingKey) { Akismet.spam?(full_spam_attributes) }
end
def test_should_not_raise_missing_key_for_valid_key
Akismet.key = nil
assert_nothing_raised { Akismet.valid_key?('123456789') }
end
+ def test_extra_headers
+ Akismet.extra_headers = ['HTTP_REMOTE_ADDR']
+ assert_equal ['HTTP_REMOTE_ADDR'], Akismet.extra_headers
+
+ Akismet.extra_headers << 'HTTP_CLIENT_IP'
+ assert_equal ['HTTP_REMOTE_ADDR', 'HTTP_CLIENT_IP'], Akismet.extra_headers
+
+ Akismet.extra_headers << ['HTTP_X_FORWARDED_FOR', 'HTTP_CONNECTION']
+ assert_equal ['HTTP_REMOTE_ADDR', 'HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_CONNECTION'],
+ Akismet.extra_headers
+ end
+
+ def test_spam_with_actiondispatch_request
+ assert Akismet.spam?(spam_attributes, actiondispatch_request)
+ end
+
+ def test_ham_with_actiondispatch_request
+ assert !Akismet.spam?(ham_attributes, actiondispatch_request)
+ end
+
def test_spam
- assert Akismet.spam?(invalid_attributes)
+ assert Akismet.spam?(full_spam_attributes)
end
def test_not_spam
- assert !Akismet.spam?(valid_attributes)
+ assert !Akismet.spam?(full_ham_attributes)
end
def test_ham
- assert Akismet.ham?(valid_attributes)
+ assert Akismet.ham?(full_ham_attributes)
end
def test_not_ham
- assert !Akismet.ham?(invalid_attributes)
+ assert !Akismet.ham?(full_spam_attributes)
end
def test_submit_spam
+ assert Akismet.submit_spam(spam_attributes)
end
def test_submit_ham
+ assert Akismet.submit_ham(ham_attributes)
end
+
+ protected
+ def ham_attributes
+ {
+ :permalink => 'http://www.example.com/posts/1',
+ :comment_author => 'Julien Portalier',
+ :comment_author_url => 'http://ysbaddaden.wordpress.com/',
+ :comment_author_email => 'julien@example.com',
+ :comment_content => 'this is a normal comment'
+ }
+ end
+
+ def spam_attributes
+ ham_attributes.merge(:comment_author => 'viagra-test-123')
+ end
+
+ def additional_attributes
+ {
+ :user_ip => '127.0.0.1',
+ :user_agent => 'Mozilla/5.0 (X11; U; Linux i686; fr-FR) AppleWebKit/534.7 (KHTML, like Gecko) Ubuntu/10.04 Chromium/7.0.517.41 Chrome/7.0.517.41 Safari/534.7',
+ :referrer => 'http://www.example.com/posts'
+ }
+ end
+
+ def full_spam_attributes
+ spam_attributes.merge(additional_attributes)
+ end
+
+ def full_ham_attributes
+ ham_attributes.merge(additional_attributes)
+ end
+
+ def actiondispatch_request
+ request = ActionDispatch::TestRequest.new(
+ 'HTTP_REFERER' => 'http://www.example.com/posts/1',
+ 'HTTP_REMOTE_ADDR' => '127.0.0.1',
+ 'HTTP_USER_AGENT' => 'Mozilla/5.0 (X11; U; Linux i686; fr-FR) AppleWebKit/534.7 (KHTML, like Gecko) Ubuntu/10.04 Chromium/7.0.517.41 Chrome/7.0.517.41 Safari/534.7',
+ 'HTTP_CONNECTION' => 'close'
+ )
+ request.remote_addr = '127.0.0.1'
+ request
+ end
end

0 comments on commit a7ff550

Please sign in to comment.