Sha256: 2b50767439ec23645b6251c35c05a4e923012057fdd0b47bfbcc1a2b18a4f7d8
Contents?: true
Size: 1.75 KB
Versions: 1
Compression:
Stored size: 1.75 KB
Contents
require File.expand_path(File.dirname(__FILE__)+'/test_helper') describe 'TextcaptchaApi' do describe 'with a valid xml response' do before(:each) do body = "<captcha><question>1+1?</question><answer>1</answer><answer>2</answer><answer>3</answer></captcha>" stub_request(:get, "http://textcaptcha.com/api/abc").to_return(:body => body) end it 'should fetch and parse an answer from the service' do result = ActsAsTextcaptcha::TextcaptchaApi.fetch('abc') result[0].must_equal '1+1?' result[1].must_equal ['1', '2', '3'] end it 'should allow http options to be set' do result = ActsAsTextcaptcha::TextcaptchaApi.fetch('abc', { :http_read_timeout => 30, :http_open_timeout => 5 }) result.length.must_equal 2 end end it 'should return nil when Net::HTTP errors occur' do [ SocketError, Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, Errno::EHOSTUNREACH, EOFError, Errno::ECONNREFUSED, Errno::ETIMEDOUT, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError, URI::InvalidURIError ].each do |error| stub_request(:get, "http://textcaptcha.com/api/xyz").to_raise(error) ActsAsTextcaptcha::TextcaptchaApi.fetch('xyz').must_equal nil end end it 'should return nil when body cannot be parsed as XML' do stub_request(:get, "http://textcaptcha.com/api/jibber").to_return(:body => 'here be gibberish') ActsAsTextcaptcha::TextcaptchaApi.fetch('jibber').must_equal nil end it 'should return nil when body is empty' do stub_request(:get, "http://textcaptcha.com/api/empty").to_return(:body => '') ActsAsTextcaptcha::TextcaptchaApi.fetch('empty').must_equal nil end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
acts_as_textcaptcha-4.1.3 | test/textcaptcha_api_test.rb |