HTTP Accept header helper. Will set appropriate value for this header. Available options for format are: :json, :html or :xml By default :xml is used
# File tests/common.rb, line 79 def accept(format=:xml) case format when :json then 'application/json;q=0.9' when :html then 'text/html;q=0.9' when :xml then 'application/xml;q=0.9' else 'application/xml;q=0.9' end end
# File tests/common.rb, line 229 def add_created_instance(id) $created_instances ||= [] $created_instances << id end
Authentication helper for Basic HTTP authentication To change default user credentials stored in ENV you need to set opts = { :user => '...', :password => '...'}
# File tests/common.rb, line 67 def authenticate(opts={}) credentials = opts[:credentials] || { :user => ENV['API_USER'], :password => ENV['API_PASSWORD']} return { 'HTTP_AUTHORIZATION' => "Basic " + Base64.encode64("#{credentials[:user]}:#{credentials[:password]}") } end
# File tests/common.rb, line 247 def check_blob_basics(blob, cloud) (blob/'blob').length.should == 1 (blob/'blob').attribute("id").text.should_not == nil (blob/'blob').attribute("href").text.should_not==nil (blob/'blob/bucket').text.should_not == nil (blob/'blob/content_length').text.should_not == nil (blob/'blob/content_type').text.should_not == nil (blob/'blob').attribute("id").text.should == "#{@@created_blob_name}#{cloud}" (blob/'blob/bucket').text.should == "#{@@created_bucket_name}#{cloud}" (blob/'blob/content_length').text.to_i.should == File.size(@@created_blob_local_file) end
# File tests/common.rb, line 259 def check_blob_metadata(blob, metadata_hash) meta_from_blob = {} #extract metadata from nokogiri blob xml (0.. (((blob/'blob/user_metadata').first).elements.size - 1) ).each do |i| meta_from_blob[(((blob/'blob/user_metadata').first).elements[i].attribute("key").value)] = (((blob/'blob/user_metadata').first).elements[i].children[1].text) end #remove any 'x-goog-meta-' prefixes (problem for google blobs and vcr...) meta_from_blob.gsub_keys(/x-.*-meta-/, "") meta_from_blob.eql?(metadata_hash).should == true end
# File tests/common.rb, line 239 def check_bucket_basics(bucket, cloud) (bucket/'bucket/name').first.text.should == "#{@@created_bucket_name}#{cloud}" (bucket/'bucket').attribute("id").text.should == "#{@@created_bucket_name}#{cloud}" (bucket/'bucket').length.should > 0 (bucket/'bucket/name').first.text.should_not == nil (bucket/'bucket').attribute("href").text.should_not == nil end
# File tests/common.rb, line 152 def delete_url(uri, params={}, opts={}) header 'Accept', accept(opts[:format] || :xml) if DeltacloudTestCommon::recording? VCR.use_cassette("delete-"+Digest::SHA1.hexdigest("#{uri}-#{params}")) do delete(uri, params || {}, authenticate(opts)) end else delete(uri, params || {}, authenticate(opts)) if last_response.status.to_s =~ /5(\d{2})/ puts "============= [ ERROR ] ================" puts last_response.body puts "============= [ RESPONSE ] ================" puts last_response.errors end end end
# File tests/common.rb, line 116 def get_auth_url(uri, params={}, opts={}) opts.merge!(:auth => true) get_url(uri, params, opts) if last_response.status.to_s =~ /5(\d{2})/ puts "============= [ ERROR ] ================" puts last_response.body puts "============= [ RESPONSE ] ================" puts last_response.errors puts "========================================" end end
This helper will execute GET operation on given URI. You can set additional parameters using params Hash, which will be passed to request. You can change format used for communication using opts = :xml | :html :json You can turn on recording (you need to configure it first in setup.rb) using opts (true/false) You can force authentication using opts parameter or use 'get_auth_url' which will do it for you ;-)
# File tests/common.rb, line 97 def get_url(uri, params={}, opts={}) header 'Accept', accept(opts[:format] || :xml) if DeltacloudTestCommon::recording? VCR.use_cassette("get-" + Digest::SHA1.hexdigest("#{uri}-#{params}}")) do get(uri, params || {}, opts[:auth] ? authenticate(opts) : {}) end else get(uri, params || {}, opts[:auth] ? authenticate(opts) : {}) if last_response.status.to_s =~ /5(\d{2})/ puts "============= [ ERROR ] ================" puts last_response.body puts "============= [ RESPONSE ] ================" puts last_response.errors puts "========================================" end end last_response.status.should_not == 401 end
# File tests/common.rb, line 169 def head_url(uri, params={}, opts={}) header 'Accept', accept(opts[:format] || :xml) if DeltacloudTestCommon::recording? VCR.use_cassette("head-"+Digest::SHA1.hexdigest("#{uri}-#{params}")) do head(uri, params || {}, authenticate(opts)) end else head(uri, params || {}, authenticate(opts)) if last_response.status.to_s =~ /5(\d{2})/ puts "============= [ ERROR ] ================" puts last_response.inspect puts "========================================" end end end
This helper will automatically convert output from method above to Nokogiri XML object
# File tests/common.rb, line 204 def last_xml_response Nokogiri::XML(last_response.body) #if last_response.status.to_s =~ /2(\d+)/ end
# File tests/common.rb, line 128 def post_url(uri, params={}, opts={}) header 'Accept', accept(opts[:format] || :xml) if DeltacloudTestCommon::recording? if opts['vcr_cassette'] VCR.use_cassette(opts['vcr_cassette']) do post(uri, params || {}, authenticate(opts)) end else VCR.use_cassette("post-" + Digest::SHA1.hexdigest("#{uri}-#{params}")) do post(uri, params || {}, authenticate(opts)) end end else post(uri, params || {}, authenticate(opts)) if last_response.status.to_s =~ /5(\d{2})/ puts "============= [ ERROR ] ================" puts last_response.body puts "============= [ RESPONSE ] ================" puts last_response.errors puts "========================================" end end end
# File tests/common.rb, line 185 def put_url(uri, params={}, opts={}) header 'Accept', accept(opts[:format] || :xml) if DeltacloudTestCommon::recording? VCR.use_cassette("put-"+Digest::SHA1.hexdigest("#{uri}-#{params}-#{authenticate(opts)}")) do put(uri, params || {}, authenticate(opts)) end else put(uri, params || {}, authenticate(opts)) if last_response.status.to_s =~ /5(\d{2})/ puts "============= [ ERROR ] ================" puts last_response.body puts "============= [ RESPONSE ] ================" puts last_response.errors end end end
# File tests/common.rb, line 58 def record! @use_recording = true end
# File tests/common.rb, line 54 def recording? @use_recording end
Check if given URI require authentication
# File tests/common.rb, line 209 def require_authentication?(uri) # We need to make sure we don't have both API_USER and API_PASSWORD # set in the environment; otherwise LazyAuth will use those instead # of asking for credentials api_user = ENV.delete("API_USER") get uri, {} ENV["API_USER"] = api_user last_response.status == 401 end
hash ordering is unpredictable - sort the params hash so we get same vcr cassette name each time
# File tests/common.rb, line 273 def stable_vcr_cassette_name(method, uri, params) digest = Digest::SHA1.hexdigest("#{uri}-#{params.sort_by {|k,v| k.to_s}}") return "#{method}-#{digest}" end
Generated with the Darkfish Rdoc Generator 2.