Validating feeds in functional tests
In the past I usually tested the feeds a Rails application generated by writing a functional test that checked the HTTP status code and matched certain strings in the feed using a regular expression. If that checked out I hand-tested the feed using the online feedvalidator. Needless to say, this became very cumbersome after a few times, especially for one of our projects that generates a whole list of different feeds depending on the state of the account. Time to add validation to the functional tests.
Unfortunately the feedvalidator is (not yet) written in Ruby, so we have to do system calls to a python script to validate. I installed the feedvalidator in the script directory, which looked like the correct place to put this. A protected method on the testcase makes sure we can easily test feeds.
def validate_feed(content)
validate = File.dirname(__FILE__) + '/../../script/feedvalidator/validate.py'
path = Pathname.new(File.dirname(__FILE__) + '/../tmp')
Tempfile.open('feed', path.cleanpath) do |tmpfile|
tmpfile.write(content)
tmpfile.flush
result = `#{validate} #{tmpfile.path} A`
unless result =~ /No errors or warnings/
raise "Feed did not validate: #{result}"
end
end
end
So now the testcases looks like this:
def test_atom
get :atom
validate_feed @response.body
assert_equal 'application/atom+xml', @response.headers['Content-Type']
end
Note that this validates the feeds on level ‘A’ which only tests for MUST directives. For completeness you should probably still test either with the online validator or temporarily set the validation to ‘AA’ or even ‘AAA’.
Rumor has it that if you validate your feeds at ‘AAA’ level Mark Pilgrim will come to your house and crown you Prins of the Feeds. Lucky lucky!
Comments
Add your comment
In order to fight spam on this blog, posting comments from a browser without javascript is currently not supported.
Subscribe
Bob Aman about 8 hours later: (delete | show email)
That's pretty cool... I might have to do something like this for FeedTools. Every now and then I contemplate porting the feedvalidator to Ruby, but realistically, I know I'll never have the time to do it. ¶
Phil Markham 9 days later: (delete)
The feed validator is the best piece of software Mark Pilgrim has worked on since he gave us the MBDF virus.
http://www.rbs2.com/dsb.htm ¶
Thijs van der Vossen 10 days later: (delete)
Mark did _what_? ;-) ¶
Edgar Gonzalez 98 days later: (delete)
You can try this: http://feedvalidator.rubyforge.org/
FeedValidator is an interface to the W3C Feed Validation online service, based on its SOAP 1.2 support.
FeedValidator add a new assertion (assert_valid_feed) which you can use in Rails applications. This assertion implements a cache to improve the performance of the tests and to not abuse of the W3C Feed Validation online service. ¶