Best Webmock_ruby code snippet using Curl.curb_or_webmock
curb_adapter.rb
Source:curb_adapter.rb
...43 end44 end45 module Curl46 class WebMockCurlEasy < Curl::Easy47 def curb_or_webmock48 request_signature = build_request_signature49 WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)50 if WebMock::StubRegistry.instance.registered_request?(request_signature)51 webmock_response = WebMock::StubRegistry.instance.response_for_request(request_signature)52 build_curb_response(webmock_response)53 WebMock::CallbackRegistry.invoke_callbacks(54 {:lib => :curb}, request_signature, webmock_response)55 invoke_curb_callbacks56 true57 elsif WebMock.net_connect_allowed?(request_signature.uri)58 res = yield59 if WebMock::CallbackRegistry.any_callbacks?60 webmock_response = build_webmock_response61 WebMock::CallbackRegistry.invoke_callbacks(62 {:lib => :curb, :real_request => true}, request_signature,63 webmock_response)64 end65 res66 else67 raise WebMock::NetConnectNotAllowedError.new(request_signature)68 end69 end70 def build_request_signature71 method = @webmock_method.to_s.downcase.to_sym72 uri = WebMock::Util::URI.heuristic_parse(self.url)73 uri.path = uri.normalized_path.gsub("[^:]//","/")74 uri.user = self.username75 uri.password = self.password76 request_body = case method77 when :post78 self.post_body || @post_body79 when :put80 @put_data81 else82 nil83 end84 request_signature = WebMock::RequestSignature.new(85 method,86 uri.to_s,87 :body => request_body,88 :headers => self.headers89 )90 request_signature91 end92 def build_curb_response(webmock_response)93 raise Curl::Err::TimeoutError if webmock_response.should_timeout94 webmock_response.raise_error_if_any95 @body_str = webmock_response.body96 @response_code = webmock_response.status[0]97 @header_str = "HTTP/1.1 #{webmock_response.status[0]} #{webmock_response.status[1]}\r\n"98 if webmock_response.headers99 @header_str << webmock_response.headers.map do |k,v|100 "#{k}: #{v.is_a?(Array) ? v.join(", ") : v}"101 end.join("\r\n")102 location = webmock_response.headers['Location']103 if self.follow_location? && location104 @last_effective_url = location105 webmock_follow_location(location)106 end107 @content_type = webmock_response.headers["Content-Type"]108 end109 @last_effective_url ||= self.url110 end111 def webmock_follow_location(location)112 first_url = self.url113 self.url = location114 curb_or_webmock do115 send( "http_#{@webmock_method}_without_webmock" )116 end117 self.url = first_url118 end119 def invoke_curb_callbacks120 @on_progress.call(0.0,1.0,0.0,1.0) if @on_progress121 @on_header.call(self.header_str) if @on_header122 @on_body.call(self.body_str) if @on_body123 @on_complete.call(self) if @on_complete124 case response_code125 when 200..299126 @on_success.call(self) if @on_success127 when 400..599128 @on_failure.call(self, self.response_code) if @on_failure129 end130 end131 def build_webmock_response132 status, headers =133 WebMock::HttpLibAdapters::CurbAdapter.parse_header_string(self.header_str)134 webmock_response = WebMock::Response.new135 webmock_response.status = [self.response_code, status]136 webmock_response.body = self.body_str137 webmock_response.headers = headers138 webmock_response139 end140 ###141 ### Mocks of Curl::Easy methods below here.142 ###143 def http_with_webmock(method)144 @webmock_method = method145 curb_or_webmock do146 http_without_webmock(method)147 end148 end149 alias_method :http_without_webmock, :http150 alias_method :http, :http_with_webmock151 %w[ get head delete ].each do |verb|152 define_method "http_#{verb}_with_webmock" do153 @webmock_method = verb154 curb_or_webmock do155 send( "http_#{verb}_without_webmock" )156 end157 end158 alias_method "http_#{verb}_without_webmock", "http_#{verb}"159 alias_method "http_#{verb}", "http_#{verb}_with_webmock"160 end161 def http_put_with_webmock data = nil162 @webmock_method = :put163 @put_data = data if data164 curb_or_webmock do165 http_put_without_webmock(data)166 end167 end168 alias_method :http_put_without_webmock, :http_put169 alias_method :http_put, :http_put_with_webmock170 def http_post_with_webmock *data171 @webmock_method = :post172 @post_body = data.join('&') if data && !data.empty?173 curb_or_webmock do174 http_post_without_webmock(*data)175 end176 end177 alias_method :http_post_without_webmock, :http_post178 alias_method :http_post, :http_post_with_webmock179 def perform_with_webmock180 @webmock_method ||= :get181 curb_or_webmock do182 perform_without_webmock183 end184 end185 alias :perform_without_webmock :perform186 alias :perform :perform_with_webmock187 def put_data_with_webmock= data188 @webmock_method = :put189 @put_data = data190 self.put_data_without_webmock = data191 end192 alias_method :put_data_without_webmock=, :put_data=193 alias_method :put_data=, :put_data_with_webmock=194 def post_body_with_webmock= data195 @webmock_method = :post...
curb.rb
Source:curb.rb
1require 'web_mock/web_mock'2if defined?(Curl)3 module Curl4 class Easy5 def curb_or_webmock6 request_signature = build_request_signature7 WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)8 if WebMock::StubRegistry.instance.registered_request?(request_signature)9 webmock_response = WebMock::StubRegistry.instance.response_for_request(request_signature)10 build_curb_response(webmock_response)11 WebMock::CallbackRegistry.invoke_callbacks(12 {:lib => :curb}, request_signature, webmock_response)13 invoke_curb_callbacks14 true15 elsif WebMock.net_connect_allowed?(request_signature.uri)16 res = yield17 if WebMock::CallbackRegistry.any_callbacks?18 webmock_response = build_webmock_response19 WebMock::CallbackRegistry.invoke_callbacks(20 {:lib => :curb, :real_request => true}, request_signature,21 webmock_response)22 end23 res24 else25 raise WebMock::NetConnectNotAllowedError.new(request_signature)26 end27 end28 def build_request_signature29 method = @webmock_method.to_s.downcase.to_sym30 uri = WebMock::Util::URI.heuristic_parse(self.url)31 uri.path = uri.normalized_path.gsub("[^:]//","/")32 uri.user = self.username33 uri.password = self.password34 request_body = case method35 when :post36 self.post_body || @post_body37 when :put38 @put_data39 else40 nil41 end42 request_signature = WebMock::RequestSignature.new(43 method,44 uri.to_s,45 :body => request_body,46 :headers => self.headers47 )48 request_signature49 end50 def build_curb_response(webmock_response)51 raise Curl::Err::TimeoutError if webmock_response.should_timeout52 webmock_response.raise_error_if_any53 @body_str = webmock_response.body54 @response_code = webmock_response.status[0]55 @header_str = "HTTP/1.1 #{webmock_response.status[0]} #{webmock_response.status[1]}\r\n"56 if webmock_response.headers57 @header_str << webmock_response.headers.map do |k,v|58 "#{k}: #{v.is_a?(Array) ? v.join(", ") : v}"59 end.join("\r\n")60 location = webmock_response.headers['Location']61 if self.follow_location? && location62 @last_effective_url = location63 webmock_follow_location(location)64 end65 @content_type = webmock_response.headers["Content-Type"]66 end67 @last_effective_url ||= self.url68 end69 def webmock_follow_location(location)70 first_url = self.url71 self.url = location72 curb_or_webmock do73 send( "http_#{@webmock_method}_without_webmock" )74 end75 self.url = first_url76 end77 def invoke_curb_callbacks78 @on_progress.call(0.0,1.0,0.0,1.0) if @on_progress79 @on_header.call(self.header_str) if @on_header80 @on_body.call(self.body_str) if @on_body81 @on_complete.call(self) if @on_complete82 case response_code83 when 200..29984 @on_success.call(self) if @on_success85 when 500..59986 @on_failure.call(self, self.response_code) if @on_failure87 end88 end89 def build_webmock_response90 status, headers = WebmockHelper.parse_header_string(self.header_str)91 webmock_response = WebMock::Response.new92 webmock_response.status = [self.response_code, status]93 webmock_response.body = self.body_str94 webmock_response.headers = headers95 webmock_response96 end97 ###98 ### Mocks of Curl::Easy methods below here.99 ###100 def http_with_webmock(method)101 @webmock_method = method102 curb_or_webmock do103 http_without_webmock(method)104 end105 end106 alias_method :http_without_webmock, :http107 alias_method :http, :http_with_webmock108 %w[ get head delete ].each do |verb|109 define_method "http_#{verb}_with_webmock" do110 @webmock_method = verb111 curb_or_webmock do112 send( "http_#{verb}_without_webmock" )113 end114 end115 alias_method "http_#{verb}_without_webmock", "http_#{verb}"116 alias_method "http_#{verb}", "http_#{verb}_with_webmock"117 end118 def http_put_with_webmock data = nil119 @webmock_method = :put120 @put_data = data if data121 curb_or_webmock do122 http_put_without_webmock(data)123 end124 end125 alias_method :http_put_without_webmock, :http_put126 alias_method :http_put, :http_put_with_webmock127 def http_post_with_webmock data = nil128 @webmock_method = :post129 @post_body = data if data130 curb_or_webmock do131 http_post_without_webmock(data)132 end133 end134 alias_method :http_post_without_webmock, :http_post135 alias_method :http_post, :http_post_with_webmock136 def perform_with_webmock137 @webmock_method ||= :get138 curb_or_webmock do139 perform_without_webmock140 end141 end142 alias :perform_without_webmock :perform143 alias :perform :perform_with_webmock144 def put_data_with_webmock= data145 @webmock_method = :put146 @put_data = data147 self.put_data_without_webmock = data148 end149 alias_method :put_data_without_webmock=, :put_data=150 alias_method :put_data=, :put_data_with_webmock=151 def post_body_with_webmock= data152 @webmock_method = :post...
curb_or_webmock
Using AI Code Generation
1 def self.curb_or_webmock(url)2 Curl.get(url).body_str3 WebMock::API.stub_request(:get, url).to_return(body: 'hello')4 Curl.get(url).body_str5 expect(Curl.curb_or_webmock('http://google.com')).to eq 'hello'
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!!