diff options
author | Patrick J Cherry <patrick@bytemark.co.uk> | 2012-07-05 13:51:45 +0100 |
---|---|---|
committer | Patrick J Cherry <patrick@bytemark.co.uk> | 2012-07-05 13:51:45 +0100 |
commit | 4bc9c1005cdbb14d5d310ba7674e068b2098e19c (patch) | |
tree | 58288878dc9cc99b6efd9c2867449f9508590f6f /test | |
parent | f1ddcb07a58413a87d3643c8a23e9374e6197a1a (diff) |
Added HTTP api client tests.
Diffstat (limited to 'test')
-rw-r--r-- | test/tc_mauve_generic_http_api_client.rb | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/test/tc_mauve_generic_http_api_client.rb b/test/tc_mauve_generic_http_api_client.rb new file mode 100644 index 0000000..d8accfa --- /dev/null +++ b/test/tc_mauve_generic_http_api_client.rb @@ -0,0 +1,108 @@ +$:.unshift "../lib" + +require 'th_mauve' +require 'mauve/calendar_interface' +require 'mauve/configuration_builder' +require 'mauve/configuration' +require 'webmock' + +class TcMauveGenericApiClient < Mauve::UnitTest + + include WebMock::API + include Mauve + include Mauve::GenericHttpApiClient + + def setup + WebMock.disable_net_connect! + super + end + + def teardown + WebMock.reset! + WebMock.allow_net_connect! + super + end + + def test_do_get + url = "http://localhost/" + + # + # This sets up two redirects, followed by the answer (below) + # + 2.times do |x| + next_url = url + "#{x}/" + stub_request(:get, url). + to_return(:status => 301, :body => nil, :headers => {:location => next_url}) + url = next_url + end + + # + # And finally the answer. + # + stub_request(:get, url). + to_return(:status => 200, :body => "OK!", :headers => {}) + + # + # Now do_get should return "OK!" when the maximum number of redirects is set to two. + # + result = nil + assert_nothing_raised{ result = do_get("http://localhost/", 2) } + assert_equal("OK!",result) + + # + # do_get should return nil when the maximum number of redirects is set to two. + # + assert_nothing_raised{ result = do_get( "http://localhost/", 1) } + assert_nil(result) + + # + # Pop the warning about the redirect off the end of the log. + # + logger_pop + end + + def test_do_get_with_cache + url = "http://localhost/" + + # + # This stubs the request to give out the time + # + stub_request(:get, url). + to_return( lambda{ {:status => 200, :body => YAML.dump(Time.now), :headers => {}} } ) + + # + # This reponse should not be cached, the cache-until paramter is "now" + # + assert_equal(YAML.dump(Time.now), do_get_with_cache( url, Time.now)) + + # + # Since the last request wasn't cached, the next one should give back + # "now", and should be cached for the next 10 seconds. + # + Timecop.freeze(Time.now + 5) + assert_equal(YAML.dump(Time.now), do_get_with_cache( url, Time.now + 10)) + + # + # This should have been cached from the last query. + # + Timecop.freeze(Time.now + 5) + assert_equal(YAML.dump(Time.now - 5), do_get_with_cache( url, Time.now + 10)) + + # + # Finally, this should now have expired from the cache. + # + Timecop.freeze(Time.now + 5) + assert_equal(YAML.dump(Time.now), do_get_with_cache( url, Time.now + 10)) + + Timecop.freeze(Time.now + 50) + cache = clean_cache + assert(cache.empty?) + end + + +end + + + + + |