Best Inspec_ruby code snippet using Supermarket.get
connection_spec.rb
Source:connection_spec.rb
1require "spec_helper"2describe Berkshelf::APIClient::Connection do3 let(:instance) { described_class.new("http://supermarket.getchef.com") }4 describe "#universe" do5 before do6 body_response = %q{{"ruby":{"1.2.3":{"endpoint_priority":0,"platforms":{},"dependencies":{"build-essential":">= 1.2.2"},"location_type":"supermarket","location_path":"https://supermarket.getchef.com/"},"2.0.0":{"endpoint_priority":0,"platforms":{},"dependencies":{"build-essential":">= 1.2.2"},"location_type":"supermarket","location_path":"https://supermarket.getchef.com/"}},"elixir":{"1.0.0":{"endpoint_priority":0,"platforms":{"CentOS":"= 6.0.0"},"dependencies":{},"location_type":"supermarket","location_path":"https://supermarket.getchef.com/"}}}}7 stub_request(:get, "http://supermarket.getchef.com/universe")8 .to_return(status: 200, body: body_response, headers: { "Content-Type" => "application/json; charset=utf-8" })9 end10 subject { instance.universe }11 it "returns an array of APIClient::RemoteCookbook" do12 expect(subject).to be_a(Array)13 subject.each do |remote|14 expect(remote).to be_a(Berkshelf::APIClient::RemoteCookbook)15 end16 end17 it "contains a item for each dependency" do18 expect(subject.size).to eq(3)19 expect(subject[0].name).to eql("ruby")20 expect(subject[0].version).to eql("1.2.3")21 expect(subject[1].name).to eql("ruby")22 expect(subject[1].version).to eql("2.0.0")23 expect(subject[2].name).to eql("elixir")24 expect(subject[2].version).to eql("1.0.0")25 end26 it "has the dependencies for each" do27 expect(subject[0].dependencies).to include("build-essential" => ">= 1.2.2")28 expect(subject[1].dependencies).to include("build-essential" => ">= 1.2.2")29 expect(subject[2].dependencies).to be_empty30 end31 it "has the platforms for each" do32 expect(subject[0].platforms).to be_empty33 expect(subject[1].platforms).to be_empty34 expect(subject[2].platforms).to include("CentOS" => "= 6.0.0")35 end36 it "has a location_path for each" do37 subject.each do |remote|38 expect(remote.location_path).to_not be_nil39 end40 end41 it "has a location_type for each" do42 subject.each do |remote|43 expect(remote.location_type).to_not be_nil44 end45 end46 end47 describe "disabling ssl validation on requests" do48 let(:instance) { described_class.new("https://supermarket.getchef.com", ssl: { verify: false }) }49 before do50 body_response = %q{{"ruby":{"1.2.3":{"endpoint_priority":0,"platforms":{},"dependencies":{"build-essential":">= 1.2.2"},"location_type":"supermarket","location_path":"https://supermarket.getchef.com/"},"2.0.0":{"endpoint_priority":0,"platforms":{},"dependencies":{"build-essential":">= 1.2.2"},"location_type":"supermarket","location_path":"https://supermarket.getchef.com/"}},"elixir":{"1.0.0":{"endpoint_priority":0,"platforms":{"CentOS":"= 6.0.0"},"dependencies":{},"location_type":"supermarket","location_path":"https://supermarket.getchef.com/"}}}}51 stub_request(:get, "https://supermarket.getchef.com/universe")52 .to_return(status: 200, body: body_response, headers: { "Content-Type" => "application/json; charset=utf-8" })53 end54 subject { instance.universe }55 it "correctly disables SSL validation" do56 expect_any_instance_of(Net::HTTP).to receive(:use_ssl=).with(true).and_call_original57 expect_any_instance_of(Net::HTTP).to receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE).and_call_original58 expect(subject).to be_a(Array)59 end60 end61 describe "enabling ssl validation on requests" do62 let(:instance) { described_class.new("https://supermarket.getchef.com", ssl: { verify: true }) }63 before do64 body_response = %q{{"ruby":{"1.2.3":{"endpoint_priority":0,"platforms":{},"dependencies":{"build-essential":">= 1.2.2"},"location_type":"supermarket","location_path":"https://supermarket.getchef.com/"},"2.0.0":{"endpoint_priority":0,"platforms":{},"dependencies":{"build-essential":">= 1.2.2"},"location_type":"supermarket","location_path":"https://supermarket.getchef.com/"}},"elixir":{"1.0.0":{"endpoint_priority":0,"platforms":{"CentOS":"= 6.0.0"},"dependencies":{},"location_type":"supermarket","location_path":"https://supermarket.getchef.com/"}}}}65 stub_request(:get, "https://supermarket.getchef.com/universe")66 .to_return(status: 200, body: body_response, headers: { "Content-Type" => "application/json; charset=utf-8" })67 end68 subject { instance.universe }69 it "correctly disables SSL validation" do70 expect_any_instance_of(Net::HTTP).to receive(:use_ssl=).with(true).and_call_original71 expect_any_instance_of(Net::HTTP).to receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_PEER).and_call_original72 expect(subject).to be_a(Array)73 end74 end75 describe "enabling ssl validation by default" do76 let(:instance) { described_class.new("https://supermarket.getchef.com") }77 before do78 body_response = %q{{"ruby":{"1.2.3":{"endpoint_priority":0,"platforms":{},"dependencies":{"build-essential":">= 1.2.2"},"location_type":"supermarket","location_path":"https://supermarket.getchef.com/"},"2.0.0":{"endpoint_priority":0,"platforms":{},"dependencies":{"build-essential":">= 1.2.2"},"location_type":"supermarket","location_path":"https://supermarket.getchef.com/"}},"elixir":{"1.0.0":{"endpoint_priority":0,"platforms":{"CentOS":"= 6.0.0"},"dependencies":{},"location_type":"supermarket","location_path":"https://supermarket.getchef.com/"}}}}79 stub_request(:get, "https://supermarket.getchef.com/universe")80 .to_return(status: 200, body: body_response, headers: { "Content-Type" => "application/json; charset=utf-8" })81 end82 subject { instance.universe }83 it "correctly disables SSL validation" do84 expect_any_instance_of(Net::HTTP).to receive(:use_ssl=).with(true).and_call_original85 expect_any_instance_of(Net::HTTP).to receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_PEER).and_call_original86 expect(subject).to be_a(Array)87 end88 end89 describe "non-200s" do90 before do91 Chef::Config[:http_retry_delay] = 0.00192 Chef::Config[:http_retry_count] = 093 end94 subject { instance.universe }95 it "follows 301 redirects correctly" do96 stub_request(:get, "http://supermarket.getchef.com/universe").to_return(:status => 301, :headers => { "Location" => "http://arglebargle.com/universe" })97 body_response = %q{{"ruby":{"1.2.3":{"endpoint_priority":0,"platforms":{},"dependencies":{"build-essential":">= 1.2.2"},"location_type":"supermarket","location_path":"https://supermarket.getchef.com/"},"2.0.0":{"endpoint_priority":0,"platforms":{},"dependencies":{"build-essential":">= 1.2.2"},"location_type":"supermarket","location_path":"https://supermarket.getchef.com/"}},"elixir":{"1.0.0":{"endpoint_priority":0,"platforms":{"CentOS":"= 6.0.0"},"dependencies":{},"location_type":"supermarket","location_path":"https://supermarket.getchef.com/"}}}}98 stub_request(:get, "http://arglebargle.com/universe")99 .to_return(status: 200, body: body_response, headers: { "Content-Type" => "application/json; charset=utf-8" })100 expect(subject.size).to eq(3)101 end102 it "raises Berkshelf::APIClient::ServiceUnavailable for 500s" do103 stub_request(:get, "http://supermarket.getchef.com/universe").to_return(:status => [500, "Internal Server Error"])104 expect { subject }.to raise_error(Berkshelf::APIClient::ServiceUnavailable)105 end106 it "raises Berkshelf::APIClient::ServiceNotFound for 404s" do107 stub_request(:get, "http://supermarket.getchef.com/universe").to_return(:status => [404, "Not Found"])108 expect { subject }.to raise_error(Berkshelf::APIClient::ServiceNotFound)109 end110 it "raises Net::HTTPBadRequest for 400s" do111 stub_request(:get, "http://supermarket.getchef.com/universe").to_return(:status => [400, "Bad Request"])112 expect { subject }.to raise_error(Berkshelf::APIClient::BadResponse)113 end114 it "raises Berkshelf::APIClient::TimeoutError for timeouts" do115 stub_request(:get, "http://supermarket.getchef.com/universe").to_timeout116 expect { subject }.to raise_error(Berkshelf::APIClient::TimeoutError)117 end118 it "raises Berkshelf::APIClient::TimeoutError for timeouts" do119 stub_request(:get, "http://supermarket.getchef.com/universe").to_raise(Errno::ECONNREFUSED)120 expect { subject }.to raise_error(Berkshelf::APIClient::ServiceUnavailable)121 end122 end123end...
api_test.rb
Source:api_test.rb
...25 end26 let(:profile_name) { 'supermarket://test_owner/test_name' }27 describe '#profiles' do28 it 'returns the profile list' do29 stub_request(:get, "#{supermarket_url}/api/v1/tools-search?items=100&type=compliance_profile").30 to_return(:status => 200, :body => profile_search_response_body.to_json)31 test_profile = default_url?(supermarket_url) ? subject.profiles.first : subject.profiles(supermarket_url).first32 test_profile.must_equal(profile_search_response_body['items'].first.merge({'slug' => 'test_name'}))33 end34 end35 describe '#profile_name' do36 it 'returns the profile name and owner from a supermarket://owner/name path' do37 tool_owner, tool_name = subject.profile_name('supermarket://test_tool_owner/test_tool_name')38 tool_owner.must_equal('test_tool_owner')39 tool_name.must_equal('test_tool_name')40 end41 end42 describe '#info' do43 let(:profile_list_response_body) do44 {45 'name' => 'test_name',46 'slug' => 'test_slug',47 'type' => 'test_type',48 'source_url' => supermarket_url,49 'description' => 'test_description',50 'instructions' => 'test_instructions',51 'owner' => 'test_owner'52 }53 end54 it 'returns profile info' do55 stub_request(:get, "#{supermarket_url}/api/v1/tools/test_name").56 to_return(:status => 200, :body => profile_list_response_body.to_json)57 profile_info = default_url?(supermarket_url) ? subject.info('test_owner/test_name') : subject.info('test_owner/test_name', supermarket_url)58 profile_info.must_equal(profile_list_response_body)59 end60 end61 describe '#same?' do62 let(:tool_url) { "#{supermarket_url}/api/v1/tools/test_name" }63 it 'is the same on a match' do64 supermarket_tool = {'tool_owner' => 'test_owner', 'tool' => tool_url}65 same = default_url?(supermarket_url) ? subject.same?(profile_name, supermarket_tool) : subject.same?(profile_name, supermarket_tool, supermarket_url)66 same.must_equal(true)67 end68 it 'is not the same on a mismatched owner' do69 supermarket_tool = {'tool_owner' => 'wrong_owner', 'tool' => tool_url}70 same = default_url?(supermarket_url) ? subject.same?(profile_name, supermarket_tool) : subject.same?(profile_name, supermarket_tool, supermarket_url)71 same.must_equal(false)72 end73 it 'is not the same on a mismatched supermarket tool' do74 supermarket_tool = {'tool_owner' => 'test_owner', 'tool' => 'garbage'}75 same = default_url?(supermarket_url) ? subject.same?(profile_name, supermarket_tool) : subject.same?(profile_name, supermarket_tool, supermarket_url)76 same.must_equal(false)77 end78 end79 describe '#find' do80 let(:empty_profile_search_response_body) do81 {start: 0, total: 0, items: []}82 end83 it 'returns nil if profiles are empty' do84 stub_request(:get, "#{supermarket_url}/api/v1/tools-search?items=100&type=compliance_profile").85 to_return(:status => 200, :body => empty_profile_search_response_body.to_json)86 search = default_url?(supermarket_url) ? subject.find(profile_name) : subject.find(profile_name, supermarket_url)87 search.must_be_nil88 end89 it 'returns nil if profile not found' do90 stub_request(:get, "#{supermarket_url}/api/v1/tools-search?items=100&type=compliance_profile").91 to_return(:status => 200, :body => profile_search_response_body.to_json)92 profile_name_cant_find = 'supermarket://cant_find/not_found'93 search = default_url?(supermarket_url) ? subject.find(profile_name_cant_find) : subject.find(profile_name_cant_find, supermarket_url)94 search.must_be_nil95 end96 it 'returns profile if it is found' do97 stub_request(:get, "#{supermarket_url}/api/v1/tools-search?items=100&type=compliance_profile").98 to_return(:status => 200, :body => profile_search_response_body.to_json)99 profile = default_url?(supermarket_url) ? subject.find(profile_name) : subject.find(profile_name, supermarket_url)100 profile.must_equal(profile_search_response_body['items'].first.merge({'slug' => 'test_name'}))101 end102 end103 end104 end105end...
api.rb
Source:api.rb
...9 SUPERMARKET_URL = 'https://supermarket.chef.io'.freeze10 # displays a list of profiles11 def self.profiles(supermarket_url = SUPERMARKET_URL)12 url = "#{supermarket_url}/api/v1/tools-search"13 _success, data = get(url, { type: 'compliance_profile', items: 100 })14 if !data.nil?15 profiles = JSON.parse(data)16 profiles['items'].map { |x|17 m = %r{^#{supermarket_url}/api/v1/tools/(?<slug>[\w-]+)(/)?$}.match(x['tool'])18 x['slug'] = m[:slug]19 x20 }21 else22 []23 end24 end25 def self.profile_name(profile)26 # We use Addressable::URI here because URI has a bug in Ruby 2.1.x where it doesn't allow underscore in host27 uri = Addressable::URI.parse profile28 [uri.host, uri.path[1..-1]]29 rescue30 nil31 end32 # displays profile infos33 def self.info(profile, supermarket_url = SUPERMARKET_URL)34 _tool_owner, tool_name = profile_name("supermarket://#{profile}")35 return if tool_name.nil? || tool_name.empty?36 url = "#{supermarket_url}/api/v1/tools/#{tool_name}"37 _success, data = get(url, {})38 JSON.parse(data) if !data.nil?39 rescue JSON::ParserError40 nil41 end42 # compares a profile with the supermarket tool info43 def self.same?(profile, supermarket_tool, supermarket_url = SUPERMARKET_URL)44 tool_owner, tool_name = profile_name(profile)45 tool = "#{supermarket_url}/api/v1/tools/#{tool_name}"46 supermarket_tool['tool_owner'] == tool_owner && supermarket_tool['tool'] == tool47 end48 def self.find(profile, supermarket_url = SUPERMARKET_URL)49 profiles = Supermarket::API.profiles(supermarket_url)50 if !profiles.empty?51 index = profiles.index { |t| same?(profile, t, supermarket_url) }52 # return profile or nil53 profiles[index] if !index.nil? && index >= 054 end55 end56 # verifies that a profile exists57 def self.exist?(profile, supermarket_url = SUPERMARKET_URL)58 !find(profile, supermarket_url).nil?59 end60 def self.get(url, params)61 uri = URI.parse(url)62 uri.query = URI.encode_www_form(params)63 req = Net::HTTP::Get.new(uri)64 send_request(uri, req)65 end66 def self.send_request(uri, req)67 # send request68 res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') {|http|69 http.request(req)70 }71 [res.is_a?(Net::HTTPSuccess), res.body]72 end73 end74end...
get
Using AI Code Generation
1s.set('Milk')2s.set('Bread')3 def set(item)4t.set('Milk')5t.set('Bread')6 def set(item)
get
Using AI Code Generation
1sm.set('abc')2sm.set('abc')3sm.set('abc')4sm.set('abc')5sm.set('abc')6sm.set('abc')7sm.set('abc')8sm.set('abc')9sm.set('abc')10sm.set('abc')11sm.set('abc')12sm.set('abc')
get
Using AI Code Generation
1puts supermarket.get(1)2supermarket.set(1, 'Milk')3supermarket.add('Milk')4supermarket.remove(1)5supermarket.item_at(1)6supermarket.add_to_cart(1)7supermarket.remove_from_cart(1)
get
Using AI Code Generation
1sm.set('abc')2sm.set('abc')3sm.set('abc')4sm.set('abc')5sm.set('abc')6sm.set('abc')7sm.set('abc')8sm.set('abc')9sm.set('abc')10sm.set('abc')11sm.set('abc')12sm.set('abc')
get
Using AI Code Generation
1puts supermarket.get(1)2supermarket.set(1, 'Milk')3supermarket.add('Milk')4supermarket.remove(1)5supermarket.item_at(1)6supermarket.add_to_cart(1)
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!