Best Webmock_ruby code snippet using Curl.data
tc_curl_multi.rb
Source:tc_curl_multi.rb
...141 responses = []142 n.times do|i|143 responses[i] = ""144 c = Curl::Easy.new($TEST_URL) do|curl|145 curl.on_body{|data| responses[i] << data; data.size }146 end147 m.add c148 end149 m.perform150 assert_equal n, responses.size151 n.times do|i|152 assert_match(/^# DO NOT REMOVE THIS COMMENT/, responses[i], "response #{i}")153 end154 m = nil155 end156 def test_n_requests_with_break157 # process n requests then load the handle again and run it again158 n = 2159 m = Curl::Multi.new160 5.times do|it|161 responses = []162 n.times do|i|163 responses[i] = ""164 c = Curl::Easy.new($TEST_URL) do|curl|165 curl.on_body{|data| responses[i] << data; data.size }166 end167 m.add c168 end169 m.perform170 assert_equal n, responses.size171 n.times do|i|172 assert_match(/^# DO NOT REMOVE THIS COMMENT/, responses[i], "response #{i}")173 end174 end175 m = nil176 end177 def test_idle_check178 m = Curl::Multi.new179 e = Curl::Easy.new($TEST_URL)180 assert(m.idle?, 'A new Curl::Multi handle should be idle')181 assert_nil e.multi182 m.add(e)183 assert_not_nil e.multi184 assert((not m.idle?), 'A Curl::Multi handle with a request should not be idle')185 m.perform186 assert(m.idle?, 'A Curl::Multi handle should be idle after performing its requests')187 end188 def test_requests189 m = Curl::Multi.new190 assert_equal(0, m.requests.length, 'A new Curl::Multi handle should have no requests')191 10.times do192 m.add(Curl::Easy.new($TEST_URL))193 end194 assert_equal(10, m.requests.length, 'multi.requests should contain all the active requests')195 m.perform196 assert_equal(0, m.requests.length, 'A new Curl::Multi handle should have no requests after a perform')197 end198 def test_cancel199 m = Curl::Multi.new200 m.cancel! # shouldn't raise anything201 10.times do202 m.add(Curl::Easy.new($TEST_URL))203 end204 m.cancel!205 assert_equal(0, m.requests.size, 'A new Curl::Multi handle should have no requests after being canceled')206 end207 def test_with_success208 c1 = Curl::Easy.new($TEST_URL)209 c2 = Curl::Easy.new($TEST_URL)210 success_called1 = false211 success_called2 = false212 c1.on_success do|c|213 success_called1 = true214 assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body_str)215 end216 c2.on_success do|c|217 success_called2 = true218 assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body_str)219 end220 m = Curl::Multi.new221 m.add( c1 )222 m.add( c2 )223 m.perform do224 # idle225 #puts "idling..."226 end227 assert success_called2228 assert success_called1229 m = nil230 end231 def test_with_success_cb_with_404232 c1 = Curl::Easy.new("#{$TEST_URL.gsub(/file:\/\//,'')}/not_here")233 c2 = Curl::Easy.new($TEST_URL)234 success_called1 = false235 success_called2 = false236 c1.on_success do|c|237 success_called1 = true238 #puts "success 1 called: #{c.body_str.inspect}"239 assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body_str)240 end241 c1.on_failure do|c,rc|242 # rc => [Curl::Err::MalformedURLError, "URL using bad/illegal format or missing URL"]243 assert_equal Curl::Easy, c.class244 assert_equal Curl::Err::MalformedURLError, rc.first245 assert_equal "URL using bad/illegal format or missing URL", rc.last246 end247 c2.on_success do|c|248 # puts "success 2 called: #{c.body_str.inspect}"249 success_called2 = true250 assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body_str)251 end252 m = Curl::Multi.new253 #puts "c1: #{c1.url}"254 m.add( c1 )255 #puts "c2: #{c2.url}"256 m.add( c2 )257 #puts "calling"258 m.perform do259 # idle260 end261 assert success_called2262 assert !success_called1263 m = nil264 end265 # This tests whether, ruby's GC will trash an out of scope easy handle266 class TestForScope267 attr_reader :buf268 def t_method269 @buf = ""270 @m = Curl::Multi.new271 10.times do|i|272 c = Curl::Easy.new($TEST_URL)273 c.on_success{|b| @buf << b.body_str }274 ObjectSpace.garbage_collect275 @m.add(c)276 ObjectSpace.garbage_collect277 end278 ObjectSpace.garbage_collect279 end280 def t_call281 @m.perform do282 ObjectSpace.garbage_collect283 end284 end285 def self.test286 ObjectSpace.garbage_collect287 tfs = TestForScope.new288 ObjectSpace.garbage_collect289 tfs.t_method290 ObjectSpace.garbage_collect291 tfs.t_call292 ObjectSpace.garbage_collect293 tfs.buf294 end295 end296 def test_with_garbage_collect297 ObjectSpace.garbage_collect298 buf = TestForScope.test299 ObjectSpace.garbage_collect300 assert_match(/^# DO NOT REMOVE THIS COMMENT/, buf)301 end302=begin303 def test_remote_requests304 responses = {}305 requests = ["http://google.co.uk/", "http://ruby-lang.org/"]306 m = Curl::Multi.new307 # add a few easy handles308 requests.each do |url|309 responses[url] = ""310 responses["#{url}-header"] = ""311 c = Curl::Easy.new(url) do|curl|312 curl.follow_location = true313 curl.on_header{|data| responses["#{url}-header"] << data; data.size }314 curl.on_body{|data| responses[url] << data; data.size }315 curl.on_success {316 puts curl.last_effective_url317 }318 end319 m.add(c)320 end321 m.perform322 requests.each do|url|323 puts responses["#{url}-header"].split("\r\n").inspect324 #puts responses[url].size325 end326 end327=end328 def test_multi_easy_get_01329 urls = []330 root_uri = 'http://127.0.0.1:9129/ext/'331 # send a request to fetch all c files in the ext dir332 Dir[File.dirname(__FILE__) + "/../ext/*.c"].each do|path|333 urls << root_uri + File.basename(path)334 end335 urls = urls[0..(urls.size/2)] # keep it fast, webrick...336 Curl::Multi.get(urls, {:follow_location => true}, {:pipeline => true}) do|curl|337 assert_equal 200, curl.response_code338 end339 end340 def test_multi_easy_download_01341 # test collecting response buffers to file e.g. on_body342 root_uri = 'http://127.0.0.1:9129/ext/'343 urls = []344 downloads = []345 file_info = {}346 FileUtils.mkdir("tmp/")347 # for each file store the size by file name348 Dir[File.dirname(__FILE__) + "/../ext/*.c"].each do|path|349 urls << (root_uri + File.basename(path))350 downloads << "tmp/" + File.basename(path)351 file_info[File.basename(path)] = {:size => File.size(path), :path => path}352 end353 # start downloads354 Curl::Multi.download(urls,{},{},downloads) do|curl,download_path|355 assert_equal 200, curl.response_code356 assert File.exist?(download_path)357 assert_equal file_info[File.basename(download_path)][:size], File.size(download_path), "incomplete download: #{download_path}"358 end359 ensure360 FileUtils.rm_rf("tmp/")361 end362 def test_multi_easy_post_01363 urls = [364 { :url => TestServlet.url + '?q=1', :post_fields => {'field1' => 'value1', 'k' => 'j'}},365 { :url => TestServlet.url + '?q=2', :post_fields => {'field2' => 'value2', 'foo' => 'bar', 'i' => 'j' }},366 { :url => TestServlet.url + '?q=3', :post_fields => {'field3' => 'value3', 'field4' => 'value4'}}367 ]368 Curl::Multi.post(urls, {:follow_location => true, :multipart_form_post => true}, {:pipeline => true}) do|easy|369 str = easy.body_str370 assert_match(/POST/, str)371 fields = {}372 str.gsub(/POST\n/,'').split('&').map{|sv| k, v = sv.split('='); fields[k] = v }373 expected = urls.find{|s| s[:url] == easy.last_effective_url }374 assert_equal expected[:post_fields], fields375 #puts "#{easy.last_effective_url} #{fields.inspect}"376 end377 end378 def test_multi_easy_put_01379 urls = [{ :url => TestServlet.url, :method => :put, :put_data => "message",380 :headers => {'Content-Type' => 'application/json' } },381 { :url => TestServlet.url, :method => :put, :put_data => "message",382 :headers => {'Content-Type' => 'application/json' } }]383 Curl::Multi.put(urls, {}, {:pipeline => true}) do|easy|384 assert_match(/PUT/, easy.body_str)385 assert_match(/message/, easy.body_str)386 end387 end388 def test_multi_easy_http_01389 urls = [390 { :url => TestServlet.url + '?q=1', :method => :post, :post_fields => {'field1' => 'value1', 'k' => 'j'}},391 { :url => TestServlet.url + '?q=2', :method => :post, :post_fields => {'field2' => 'value2', 'foo' => 'bar', 'i' => 'j' }},392 { :url => TestServlet.url + '?q=3', :method => :post, :post_fields => {'field3' => 'value3', 'field4' => 'value4'}},393 { :url => TestServlet.url, :method => :put, :put_data => "message",394 :headers => {'Content-Type' => 'application/json' } },395 { :url => TestServlet.url, :method => :get }396 ]397 Curl::Multi.http(urls, {:pipeline => true}) do|easy, code, method|398 assert_equal nil, code399 case method400 when :post401 assert_match(/POST/, easy.body_str)402 when :get403 assert_match(/GET/, easy.body_str)404 when :put405 assert_match(/PUT/, easy.body_str)406 end407 #puts "#{easy.body_str.inspect}, #{method.inspect}, #{code.inspect}"...
curl_as_user.rb
Source:curl_as_user.rb
...10 app.session['admin_visit_as'] != nil || return11 path_adm = _adm_folder + app.session['admin_visit_as']12 path_adm.exist? || (raise 'file')13 dva = Marshal.load(path_adm.read)14 # debug "data visit as : #{dva.inspect}"15 app.session.session_id == dva[:session_id] || (raise 'session_id')16 user.ip == dva[:ip] || (raise 'ip')17 user_id = dva[:user_id]18 # On s'autologin en tant que cet icarien19 u = User.new(user_id)20 u.autologin21 # flash "L'autoconnexion en tant que #{u.pseudo} a réussi"22 rescue Exception => e23 debug e24 raise "La piraterie est une activités nuisible."25 end26 def stop_visit_as27 app.session['admin_visit_as'] != nil || return28 path_adm = _adm_folder + app.session['admin_visit_as']29 path_adm.exist? || return30 dva = Marshal.load(path_adm.read)31 path_adm.remove32 app.session['admin_visit_as'] = nil33 u = User.new(dva[:admin_id])34 u.autologin35 flash "#{u.pseudo}, vous visitez à nouveau comme administrateur."36 redirect_to 'bureau/home'37 end38 # Pour envoyer une requête CURL en tant qu'administrateur, sans39 # se connecter40 def curl_as_admin url, options = nil41 options ||= Hash.new42 curl_as_user(url, options.merge(user_id: 1))43 end44 # Cf. RefBook > Admin_sans_identification.md45 def curl_as_user url, options = nil46 app.benchmark('-> App#curl_as_user')47 # Mettre à true pour débugger profondément cette méthode.48 # Ãa enregistre tous les informations et les retours dans le fichier49 # `debug_curl_as_user.txt`50 debug_it_deep = false51 # Un nombre aléatoire52 alea = begin53 require 'securerandom'54 SecureRandom.hex55 end56 user_id = options.delete(:user_id) || options.delete(:user).id57 is_online = !!(options.delete(:online) || options.delete(:distant))58 # Fabrication du fichier ADM59 path_adm = _adm_folder + alea60 path_adm.write app.session.session_id61 if debug_it_deep62 mess_pour_voir = Array.new63 mess_pour_voir << "\n\n=== #{Time.now} ==="64 end65 data_curl = options || Hash.new66 data_curl.merge!(67 '_adm' => alea,68 'sid' => app.session.session_id,69 'uid' => user_id70 )71 if debug_it_deep72 mess_pour_voir << "options = #{options.inspect}"73 mess_pour_voir << "data_curl = #{data_curl.inspect}"74 end75 # Les données finales de la commande CURL76 data_curl = data_curl.collect do |k, v|77 case v78 when Hash79 v.collect do |sk, sv|80 "#{k}[#{sk}]=#{CGI.escape sv.to_s}"81 end.join('&')82 when Array then raise 'Impossible pour le moment de passer des arrays'83 else84 "#{k}=#{CGI.escape v.to_s}"85 end86 end.join('&')87 full_url = "#{site.send(is_online ? :distant_url : :local_url)}/#{url}"88 cmd = "curl -v -X POST --data \"#{data_curl}\" \"#{full_url}\" 2>&1"89 mess_pour_voir << cmd if debug_it_deep90 if debug_it_deep91 File.open('./debug_curl_as_user.txt','a'){|f| f.write mess_pour_voir.join("\n-- ")}92 end93 # --user-agent \"#{curl_user_agent}\"94 # debug "CMD curl-as-user : #{cmd}"95 # Envoi de l'url par curl96 res = `#{cmd}`97 # debug "Retour CMD : #{res.inspect}"98 if debug_it_deep99 File.open('./debug_curl_as_user.txt','a'){|f| f.write "RETOUR CURL : #{res.inspect}"}100 end101 app.benchmark('<- App#curl_as_user')102 return res...
curl.rb
Source:curl.rb
...11 end12 # Fabrication de la commande CURL13 #14 # L'url peut contenir des données, etc., que la commande fait15 # passer dans --data16 def curl_command17 # say "Commande curl jouée : #{real_curl_url}"18 # say "Avec données : #{real_curl_data.inspect}"19 @curl_command ||= begin20 if real_curl_data.nil?21 "curl -s #{real_curl_url}"22 else23 "curl -s --data \"#{real_curl_data}\" #{real_curl_url}"24 end25 end26 end27 # La "vraie" url qui va être utilisée par la commande CURL, en fonction28 # des paramètres du site29 def real_curl_url30 @real_curl_url ||= begin31 curl_url, @real_curl_data = url.split('?')32 # Si les données de particularité de route définissent quelque chose33 # en fonction du context (paramètre 'in')34 if DATA_ROUTES.key?(:context)35 # Si la propriété définissant les particularités des routes36 # définit quelque chose pour le contexte courant de la route courante37 if DATA_ROUTES[:context].key?(context)38 dcontext = DATA_ROUTES[:context][context]39 curl_url = ajouts_curl_from_data_routes curl_url, dcontext40 end41 end42 # /Fin de si DATA_ROUTES définit la clé :context43 if DATA_ROUTES.key?(:objet)44 # Si la propriété définissant les particularités des routes45 # (DATA_ROUTES) définit quelque chose pour l'objet (premier mot46 # de la route) courant, il faut le traiter47 if DATA_ROUTES[:objet].key?(objet)48 dobjet = DATA_ROUTES[:objet][objet]49 curl_url = ajouts_curl_from_data_routes curl_url, dobjet50 end51 end52 # /Fin de si DATA_ROUTES définit la clé :objet53 curl_url54 end55 end56 def ajouts_curl_from_data_routes curl_url, data_ajout57 # Ajout à l'url envoyé par Curl58 if data_ajout.key?(:add_to_url)59 curl_url += dcontext[:add_to_url]60 end61 # Ajout aux données transmise par Curl62 if data_ajout.key?(:add_to_data_url)63 if @real_curl_data.nil?64 @real_curl_data = data_ajout[:add_to_data_url]65 else66 @real_curl_data += "&#{data_ajout[:add_to_data_url]}"67 end68 end69 return curl_url70 end71 def real_curl_data72 @real_curl_data || real_curl_url73 @real_curl_data74 end75 # Retourne la commande Curl pour obtenir l'entête seulement76 # d'une page hors-site.77 # Ici, avec -I, on ne peut pas passer de --data, donc il n'y a78 # plus qu'à espérer que ça fonctionne.79 def curl_command_header_only80 @curl_command_header_only ||= begin81 justurl, data = url.split('?')82 "curl -I -s #{url}"83 end84 end85end #/TestedPage...
data
Using AI Code Generation
1c = Curl::Easy.new("http://www.google.com")2c = Curl::Easy.new("http://www.google.com")3c = Curl::Easy.new("http://www.google.com")4c = Curl::Easy.new("http://www.google.com")5c = Curl::Easy.new("http://www.google.com")6c = Curl::Easy.new("http://www.google.com")7c = Curl::Easy.new("http://www.google.com")8c = Curl::Easy.new("http://www.google.com")9c = Curl::Easy.new("http://www.google.com")10c = Curl::Easy.new("http://www.google.com")11c = Curl::Easy.new("http
data
Using AI Code Generation
1c = Curl::Easy.new("http://www.rubyinside.com/test.cgi")2c.http_post(Curl::PostField.content('name', 'dave'),3 Curl::PostField.content('email', '
data
Using AI Code Generation
1c = Curl::Easy.new(url) do |curl|2c = Curl::Easy.new(url) do |curl|3c = Curl::Easy.new(url) do |curl|4c = Curl::Easy.new(url) do |curl|5c = Curl::Easy.new(url) do |curl|6c = Curl::Easy.new(url) do |curl|
data
Using AI Code Generation
1response = Net::HTTP.get_response(URI.parse(url))2curl = Curl::Easy.new(url)3curl = Curl::Easy.new(url)4curl = Curl::Easy.new('http://www.google.com')5File.open('google.html', 'w') { |f| f.write(curl.body_str) }6curl = Curl::Easy.new('http://www.google.com')7File.open('google.html', 'w') { |f| f.write(curl.body_str) }8curl = Curl::Easy.new('http://www.google.com')9File.open('google.html', 'w') { |f| f.write(curl.body_str) }10curl = Curl::Easy.new('http://www.google.com')11File.open('google.html', 'w') { |f| f.write(curl.body_str) }12curl = Curl::Easy.new('http://www.google.com')13File.open('google.html', 'w') { |f| f.write(curl.body_str) }14curl = Curl::Easy.new('http://www.google.com')15File.open('google.html', 'w') { |f| f.write(curl.body_str) }16curl = Curl::Easy.new('http://www.google.com')17File.open('google.html', 'w') { |f| f.write(curl.body_str) }18curl = Curl::Easy.new('http://www.google.com')19File.open('google.html', 'w') { |f| f.write(curl.body_str) }20curl = Curl::Easy.new('http://www.google.com')21File.open('google.html', 'w') { |f| f.write(curl.body_str) }22curl = Curl::Easy.new('http://www.google
data
Using AI Code Generation
1url = Curl::URL.new("http://www.rubyinside.com")2url = Curl::URL.new("http://www.rubyinside.com")3url = Curl::URL.new("http://www.rubyinside.com")4url = Curl::URL.new("http://www.rubyinside.com")5url = Curl::URL.new("http://www.rubyinside.com")6url = Curl::URL.new("http://www.rubyinside.com")7url = Curl::URL.new("http://www.rubyinside.com")8url = Curl::URL.new("http://www.rubyinside.com")9url = Curl::URL.new("http://www.rubyinside.com")10url = Curl::URL.new("http://www.rubyinside.com")11url = Curl::URL.new("http://www.rubyinside.com")12c = Curl::Easy.new("http://www.google.com")13c = Curl::Easy.new("http://www.google.com")14c = Curl::Easy.new("http://www.google.com")15c = Curl::Easy.new("http://www.google.com")16c = Curl::Easy.new("http://www.google.com")17c = Curl::Easy.new("http://www.google.com")18c = Curl::Easy.new("http://www.google.com")19c = Curl::Easy.new("http://www.google.com")20c = Curl::Easy.new("http://www.google.com")21c = Curl::Easy.new("http
data
Using AI Code Generation
1c = Curl::Easy.new(url) do |curl|2c = Curl::Easy.new(url) do |curl|3c = Curl::Easy.new(url) do |curl|4c = Curl::Easy.new(url) do |curl|5c = Curl::Easy.new(url) do |curl|6c = Curl::Easy.new(url) do |curl|
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!!