Best Vcr_ruby code snippet using Faraday.initialize
connection_test.rb
Source:connection_test.rb
...12 ENV[key] = old_value13 end14 end15 end16 def test_initialize_parses_host_out_of_given_url17 conn = Faraday::Connection.new "http://sushi.com"18 assert_equal 'sushi.com', conn.host19 end20 def test_initialize_inherits_default_port_out_of_given_url21 conn = Faraday::Connection.new "http://sushi.com"22 assert_equal 80, conn.port23 end24 def test_initialize_parses_scheme_out_of_given_url25 conn = Faraday::Connection.new "http://sushi.com"26 assert_equal 'http', conn.scheme27 end28 def test_initialize_parses_port_out_of_given_url29 conn = Faraday::Connection.new "http://sushi.com:815"30 assert_equal 815, conn.port31 end32 def test_initialize_parses_nil_path_prefix_out_of_given_url33 conn = Faraday::Connection.new "http://sushi.com"34 assert_equal '/', conn.path_prefix35 end36 def test_initialize_parses_path_prefix_out_of_given_url37 conn = Faraday::Connection.new "http://sushi.com/fish"38 assert_equal '/fish', conn.path_prefix39 end40 def test_initialize_parses_path_prefix_out_of_given_url_option41 conn = Faraday::Connection.new :url => "http://sushi.com/fish"42 assert_equal '/fish', conn.path_prefix43 end44 def test_initialize_stores_default_params_from_options45 conn = Faraday::Connection.new :params => {:a => 1}46 assert_equal({'a' => 1}, conn.params)47 end48 def test_initialize_stores_default_params_from_uri49 conn = Faraday::Connection.new "http://sushi.com/fish?a=1"50 assert_equal({'a' => '1'}, conn.params)51 end52 def test_initialize_stores_default_params_from_uri_and_options53 conn = Faraday::Connection.new "http://sushi.com/fish?a=1&b=2", :params => {'a' => 3}54 assert_equal({'a' => 3, 'b' => '2'}, conn.params)55 end56 def test_initialize_stores_default_headers_from_options57 conn = Faraday::Connection.new :headers => {:user_agent => 'Faraday'}58 assert_equal 'Faraday', conn.headers['User-agent']59 end60 def test_basic_auth_sets_header61 conn = Faraday::Connection.new62 assert_nil conn.headers['Authorization']63 conn.basic_auth 'Aladdin', 'open sesame'64 assert auth = conn.headers['Authorization']65 assert_equal 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==', auth66 end67 def test_auto_parses_basic_auth_from_url_and_unescapes68 conn = Faraday::Connection.new :url => "http://foo%40bar.com:pass%20word@sushi.com/fish"69 assert auth = conn.headers['Authorization']70 assert_equal Faraday::Request::BasicAuthentication.header("foo@bar.com", "pass word"), auth71 end72 def test_token_auth_sets_header73 conn = Faraday::Connection.new74 assert_nil conn.headers['Authorization']75 conn.token_auth 'abcdef', :nonce => 'abc'76 assert auth = conn.headers['Authorization']77 assert_match(/^Token /, auth)78 assert_match(/token="abcdef"/, auth)79 assert_match(/nonce="abc"/, auth)80 end81 def test_build_exclusive_url_uses_connection_host_as_default_uri_host82 conn = Faraday::Connection.new83 conn.host = 'sushi.com'84 uri = conn.build_exclusive_url("/sake.html")85 assert_equal 'sushi.com', uri.host86 end87 def test_build_exclusive_url_overrides_connection_port_for_absolute_urls88 conn = Faraday::Connection.new89 conn.port = 2390 uri = conn.build_exclusive_url("http://sushi.com")91 assert_equal 80, uri.port92 end93 def test_build_exclusive_url_uses_connection_scheme_as_default_uri_scheme94 conn = Faraday::Connection.new 'http://sushi.com'95 uri = conn.build_exclusive_url("/sake.html")96 assert_equal 'http', uri.scheme97 end98 def test_build_exclusive_url_uses_connection_path_prefix_to_customize_path99 conn = Faraday::Connection.new100 conn.path_prefix = '/fish'101 uri = conn.build_exclusive_url("sake.html")102 assert_equal '/fish/sake.html', uri.path103 end104 def test_build_exclusive_url_uses_root_connection_path_prefix_to_customize_path105 conn = Faraday::Connection.new106 conn.path_prefix = '/'107 uri = conn.build_exclusive_url("sake.html")108 assert_equal '/sake.html', uri.path109 end110 def test_build_exclusive_url_forces_connection_path_prefix_to_be_absolute111 conn = Faraday::Connection.new112 conn.path_prefix = 'fish'113 uri = conn.build_exclusive_url("sake.html")114 assert_equal '/fish/sake.html', uri.path115 end116 def test_build_exclusive_url_ignores_connection_path_prefix_trailing_slash117 conn = Faraday::Connection.new118 conn.path_prefix = '/fish/'119 uri = conn.build_exclusive_url("sake.html")120 assert_equal '/fish/sake.html', uri.path121 end122 def test_build_exclusive_url_allows_absolute_uri_to_ignore_connection_path_prefix123 conn = Faraday::Connection.new124 conn.path_prefix = '/fish'125 uri = conn.build_exclusive_url("/sake.html")126 assert_equal '/sake.html', uri.path127 end128 def test_build_exclusive_url_parses_url_params_into_path129 conn = Faraday::Connection.new130 uri = conn.build_exclusive_url("http://sushi.com/sake.html")131 assert_equal '/sake.html', uri.path132 end133 def test_build_exclusive_url_doesnt_add_ending_slash_given_nil_url134 conn = Faraday::Connection.new135 conn.url_prefix = "http://sushi.com/nigiri"136 uri = conn.build_exclusive_url137 assert_equal "/nigiri", uri.path138 end139 def test_build_exclusive_url_doesnt_add_ending_slash_given_empty_url140 conn = Faraday::Connection.new141 conn.url_prefix = "http://sushi.com/nigiri"142 uri = conn.build_exclusive_url('')143 assert_equal "/nigiri", uri.path144 end145 def test_build_exclusive_url_doesnt_use_connection_params146 conn = Faraday::Connection.new "http://sushi.com/nigiri"147 conn.params = {:a => 1}148 assert_equal "http://sushi.com/nigiri", conn.build_exclusive_url.to_s149 end150 def test_build_exclusive_url_uses_argument_params151 conn = Faraday::Connection.new "http://sushi.com/nigiri"152 conn.params = {:a => 1}153 params = Faraday::Utils::ParamsHash.new154 params[:a] = 2155 url = conn.build_exclusive_url(nil, params)156 assert_equal "http://sushi.com/nigiri?a=2", url.to_s157 end158 def test_build_url_uses_params159 conn = Faraday::Connection.new "http://sushi.com/nigiri"160 conn.params = {:a => 1, :b => 1}161 assert_equal "http://sushi.com/nigiri?a=1&b=1", conn.build_url.to_s162 end163 def test_build_url_merges_params164 conn = Faraday::Connection.new "http://sushi.com/nigiri"165 conn.params = {:a => 1, :b => 1}166 url = conn.build_url(nil, :b => 2, :c => 3)167 assert_equal "http://sushi.com/nigiri?a=1&b=2&c=3", url.to_s168 end169 def test_env_url_parses_url_params_into_query170 uri = env_url("http://sushi.com/sake.html", 'a[b]' => '1 + 2')171 assert_equal "a%5Bb%5D=1+%2B+2", uri.query172 end173 def test_env_url_escapes_per_spec174 uri = env_url('http:/', 'a' => '1+2 foo~bar.-baz')175 assert_equal "a=1%2B2+foo~bar.-baz", uri.query176 end177 def test_env_url_bracketizes_nested_params_in_query178 url = env_url nil, 'a' => {'b' => 'c'}179 assert_equal "a%5Bb%5D=c", url.query180 end181 def test_env_url_bracketizes_repeated_params_in_query182 uri = env_url("http://sushi.com/sake.html", 'a' => [1, 2])183 assert_equal "a%5B%5D=1&a%5B%5D=2", uri.query184 end185 def test_env_url_without_braketizing_repeated_params_in_query186 uri = env_url 'http://sushi.com', 'a' => [1, 2] do |conn|187 conn.options.params_encoder = Faraday::FlatParamsEncoder188 end189 assert_equal "a=1&a=2", uri.query190 end191 def test_build_exclusive_url_parses_url192 conn = Faraday::Connection.new193 uri = conn.build_exclusive_url("http://sushi.com/sake.html")194 assert_equal "http", uri.scheme195 assert_equal "sushi.com", uri.host196 assert_equal '/sake.html', uri.path197 end198 def test_build_exclusive_url_parses_url_and_changes_scheme199 conn = Faraday::Connection.new :url => "http://sushi.com/sushi"200 conn.scheme = 'https'201 uri = conn.build_exclusive_url("sake.html")202 assert_equal 'https://sushi.com/sushi/sake.html', uri.to_s203 end204 def test_build_exclusive_url_joins_url_to_base_with_ending_slash205 conn = Faraday::Connection.new :url => "http://sushi.com/sushi/"206 uri = conn.build_exclusive_url("sake.html")207 assert_equal 'http://sushi.com/sushi/sake.html', uri.to_s208 end209 def test_build_exclusive_url_used_default_base_with_ending_slash210 conn = Faraday::Connection.new :url => "http://sushi.com/sushi/"211 uri = conn.build_exclusive_url212 assert_equal 'http://sushi.com/sushi/', uri.to_s213 end214 def test_build_exclusive_url_overrides_base215 conn = Faraday::Connection.new :url => "http://sushi.com/sushi/"216 uri = conn.build_exclusive_url('/sake/')217 assert_equal 'http://sushi.com/sake/', uri.to_s218 end219 def test_build_exclusive_url_handles_uri_instances220 conn = Faraday::Connection.new221 uri = conn.build_exclusive_url(URI('/sake.html'))222 assert_equal '/sake.html', uri.path223 end224 def test_proxy_accepts_string225 with_env 'http_proxy', "http://duncan.proxy.com:80" do226 conn = Faraday::Connection.new227 conn.proxy 'http://proxy.com'228 assert_equal 'proxy.com', conn.proxy.host229 end230 end231 def test_proxy_accepts_uri232 with_env 'http_proxy', "http://duncan.proxy.com:80" do233 conn = Faraday::Connection.new234 conn.proxy URI.parse('http://proxy.com')235 assert_equal 'proxy.com', conn.proxy.host236 end237 end238 def test_proxy_accepts_hash_with_string_uri239 with_env 'http_proxy', "http://duncan.proxy.com:80" do240 conn = Faraday::Connection.new241 conn.proxy :uri => 'http://proxy.com', :user => 'rick'242 assert_equal 'proxy.com', conn.proxy.host243 assert_equal 'rick', conn.proxy.user244 end245 end246 def test_proxy_accepts_hash247 with_env 'http_proxy', "http://duncan.proxy.com:80" do248 conn = Faraday::Connection.new249 conn.proxy :uri => URI.parse('http://proxy.com'), :user => 'rick'250 assert_equal 'proxy.com', conn.proxy.host251 assert_equal 'rick', conn.proxy.user252 end253 end254 def test_proxy_accepts_http_env255 with_env 'http_proxy', "http://duncan.proxy.com:80" do256 conn = Faraday::Connection.new257 assert_equal 'duncan.proxy.com', conn.proxy.host258 end259 end260 def test_proxy_accepts_http_env_with_auth261 with_env 'http_proxy', "http://a%40b:my%20pass@duncan.proxy.com:80" do262 conn = Faraday::Connection.new263 assert_equal 'a@b', conn.proxy.user264 assert_equal 'my pass', conn.proxy.password265 end266 end267 def test_proxy_accepts_env_without_scheme268 with_env 'http_proxy', "localhost:8888" do269 uri = Faraday::Connection.new.proxy[:uri]270 assert_equal 'localhost', uri.host271 assert_equal 8888, uri.port272 end273 end274 def test_no_proxy_from_env275 with_env 'http_proxy', nil do276 conn = Faraday::Connection.new277 assert_equal nil, conn.proxy278 end279 end280 def test_no_proxy_from_blank_env281 with_env 'http_proxy', '' do282 conn = Faraday::Connection.new283 assert_equal nil, conn.proxy284 end285 end286 def test_proxy_doesnt_accept_uppercase_env287 with_env 'HTTP_PROXY', "http://localhost:8888/" do288 conn = Faraday::Connection.new289 assert_nil conn.proxy290 end291 end292 def test_proxy_requires_uri293 conn = Faraday::Connection.new294 assert_raises ArgumentError do295 conn.proxy :uri => :bad_uri, :user => 'rick'296 end297 end298 def test_dups_connection_object299 conn = Faraday::Connection.new 'http://sushi.com/foo',300 :ssl => { :verify => :none },301 :headers => {'content-type' => 'text/plain'},302 :params => {'a'=>'1'}303 other = conn.dup304 assert_equal conn.build_exclusive_url, other.build_exclusive_url305 assert_equal 'text/plain', other.headers['content-type']306 assert_equal '1', other.params['a']307 other.basic_auth('', '')308 other.headers['content-length'] = 12309 other.params['b'] = '2'310 assert_equal 2, other.builder.handlers.size311 assert_equal 2, conn.builder.handlers.size312 assert !conn.headers.key?('content-length')313 assert !conn.params.key?('b')314 end315 def test_initialize_with_false_option316 conn = Faraday::Connection.new :ssl => {:verify => false}317 assert !conn.ssl.verify?318 end319 def test_init_with_block320 conn = Faraday::Connection.new { }321 assert_equal 0, conn.builder.handlers.size322 end323 def test_init_with_block_yields_connection324 conn = Faraday::Connection.new(:params => {'a'=>'1'}) { |faraday|325 faraday.adapter :net_http326 faraday.url_prefix = 'http://sushi.com/omnom'327 assert_equal '1', faraday.params['a']328 }329 assert_equal 1, conn.builder.handlers.size...
client.rb
Source:client.rb
...5 attr_accessor :uri6 MANIFEST_VERSION = 'application/vnd.docker.distribution.manifest.v2+json'7 # Taken from: FaradayMiddleware::FollowRedirects8 REDIRECT_CODES = Set.new [301, 302, 303, 307]9 def initialize(base_uri, options = {})10 @base_uri = base_uri11 @options = options12 end13 def repository_tags(name)14 response_body faraday.get("/v2/#{name}/tags/list")15 end16 def repository_manifest(name, reference)17 response_body faraday.get("/v2/#{name}/manifests/#{reference}")18 end19 def repository_tag_digest(name, reference)20 response = faraday.head("/v2/#{name}/manifests/#{reference}")21 response.headers['docker-content-digest'] if response.success?22 end23 def delete_repository_tag(name, reference)24 faraday.delete("/v2/#{name}/manifests/#{reference}").success?25 end26 def blob(name, digest, type = nil)27 type ||= 'application/octet-stream'28 response_body faraday_blob.get("/v2/#{name}/blobs/#{digest}", nil, 'Accept' => type), allow_redirect: true29 end30 def delete_blob(name, digest)31 faraday.delete("/v2/#{name}/blobs/#{digest}").success?32 end33 private34 def initialize_connection(conn, options)35 conn.request :json36 if options[:user] && options[:password]37 conn.request(:basic_auth, options[:user].to_s, options[:password].to_s)38 elsif options[:token]39 conn.request(:authorization, :bearer, options[:token].to_s)40 end41 conn.adapter :net_http42 end43 def accept_manifest(conn)44 conn.headers['Accept'] = MANIFEST_VERSION45 conn.response :json, content_type: 'application/json'46 conn.response :json, content_type: 'application/vnd.docker.distribution.manifest.v1+prettyjws'47 conn.response :json, content_type: 'application/vnd.docker.distribution.manifest.v1+json'48 conn.response :json, content_type: 'application/vnd.docker.distribution.manifest.v2+json'49 end50 def response_body(response, allow_redirect: false)51 if allow_redirect && REDIRECT_CODES.include?(response.status)52 response = redirect_response(response.headers['location'])53 end54 response.body if response && response.success?55 end56 def redirect_response(location)57 return unless location58 # We explicitly remove authorization token59 faraday_blob.get(location) do |req|60 req['Authorization'] = ''61 end62 end63 def faraday64 @faraday ||= Faraday.new(@base_uri) do |conn|65 initialize_connection(conn, @options)66 accept_manifest(conn)67 end68 end69 def faraday_blob70 @faraday_blob ||= Faraday.new(@base_uri) do |conn|71 initialize_connection(conn, @options)72 end73 end74 end75end...
initialize
Using AI Code Generation
1conn = Faraday.new(:url => 'http://www.google.com') do |faraday|2response = conn.get '/search', { :q => 'ruby' }3conn = Faraday.new(:url => 'http://www.google.com') do |faraday|4response = conn.post '/search', { :q => 'ruby' }5conn = Faraday.new(:url => 'http://www.google.com') do |faraday|6response = conn.put '/search', { :q => 'ruby' }7conn = Faraday.new(:url => 'http://www.google.com') do |faraday|8response = conn.delete '/search', { :q => 'ruby' }9conn = Faraday.new(:url => 'http://www.google.com') do |faraday|
initialize
Using AI Code Generation
1conn = Faraday.new(:url => 'http://www.google.com') do |faraday|2conn.get '/search', { q: 'ruby' }3conn = Faraday.new(:url => 'http://www.google.com') do |faraday|4conn.get '/search', { q: 'ruby' }5conn = Faraday.new(:url => 'http://www.google.com') do |faraday|6conn.get '/search', { q: 'ruby' }7conn = Faraday.new(:url => 'http://www.google.com') do |faraday|8conn.get '/search', { q: 'ruby' }9conn = Faraday.new(:url => 'http://www.google.com') do |faraday|
initialize
Using AI Code Generation
1conn = Faraday.new(:url => 'http://www.google.com') do |faraday|2response = conn.get '/search', { :q => 'ruby' }3conn = Faraday.new(:url => 'http://www.google.com') do |faraday|4response = conn.post '/search', { :q => 'ruby' }5conn = Faraday.new(:url => 'http://www.google.com') do |faraday|6response = conn.put '/search', { :q => 'ruby' }7conn = Faraday.new(:url => 'http://www.google.com') do |faraday|8response = conn.delete '/search', { :q => 'ruby' }9conn = Faraday.new(:url => 'http://www.google.com') do |faraday|
initialize
Using AI Code Generation
1conn = Faraday.new(:url => 'http://www.google.com') do |faraday|2response = conn.get '/search', { :q => 'ruby' }3conn = Faraday.new(:url => 'http://www.google.com') do |faraday|4response = conn.post '/search', { :q => 'ruby' }5conn = Faraday.new(:url => 'http://www.google.com') do |faraday|6response = conn.put '/search', { :q => 'ruby' }7conn = Faraday.new(:url => 'http://www.google.com') do |faraday|8response = conn.delete '/search', { :q => 'ruby' }9conn = Faraday.new(:url => 'http://www.google.com') do |faraday|
initialize
Using AI Code Generation
1conn = Faraday.new(:url => 'http://www.google.com') do |faraday|2conn.get '/search', { q: 'ruby' }3conn = Faraday.new(:url => 'http://www.google.com') do |faraday|4conn.get '/search', { q: 'ruby' }5conn = Faraday.new(:url => 'http://www.google.com') do |faraday|6conn.get '/search', { q: 'ruby' }7conn = Faraday.new(:url => 'http://www.google.com') do |faraday|8conn.get '/search', { q: 'ruby' }9conn = Faraday.new(:url => 'http://www.google.com') do |faraday|
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!!