How to use request method of WebMock Package

Best Webmock_ruby code snippet using WebMock.request

typhoeus_hydra_adapter.rb

Source:typhoeus_hydra_adapter.rb Github

copy

Full Screen

...11 adapter_for :typhoeus12 def self.enable!13 @disabled = false14 add_before_callback15 add_after_request_callback16 ::Typhoeus::Config.block_connection = true17 end18 def self.disable!19 @disabled = true20 remove_after_request_callback21 remove_before_callback22 ::Typhoeus::Config.block_connection = false23 end24 def self.disabled?25 !!@disabled26 end27 def self.add_before_callback28 unless Typhoeus.before.include?(BEFORE_CALLBACK)29 Typhoeus.before << BEFORE_CALLBACK30 end31 end32 def self.remove_before_callback33 Typhoeus.before.delete_if {|v| v == BEFORE_CALLBACK }34 end35 def self.add_after_request_callback36 unless Typhoeus.on_complete.include?(AFTER_REQUEST_CALLBACK)37 Typhoeus.on_complete << AFTER_REQUEST_CALLBACK38 end39 end40 def self.remove_after_request_callback41 Typhoeus.on_complete.delete_if {|v| v == AFTER_REQUEST_CALLBACK }42 end43 def self.build_request_signature(req)44 uri = WebMock::Util::URI.heuristic_parse(req.url)45 uri.path = uri.normalized_path.gsub("[^:]//","/")46 headers = req.options[:headers]47 if req.options[:userpwd]48 headers['Authorization'] = WebMock::Util::Headers.basic_auth_header(req.options[:userpwd])49 end50 body = req.options[:body]51 if body.is_a?(Hash)52 body = WebMock::Util::QueryMapper.values_to_query(body)53 end54 request_signature = WebMock::RequestSignature.new(55 req.options[:method] || :get,56 uri.to_s,57 body: body,58 headers: headers59 )60 req.instance_variable_set(:@__webmock_request_signature, request_signature)61 request_signature62 end63 def self.build_webmock_response(typhoeus_response)64 webmock_response = WebMock::Response.new65 webmock_response.status = [typhoeus_response.code, typhoeus_response.status_message]66 webmock_response.body = typhoeus_response.body67 webmock_response.headers = typhoeus_response.headers68 webmock_response69 end70 def self.generate_typhoeus_response(request_signature, webmock_response)71 response = if webmock_response.should_timeout72 ::Typhoeus::Response.new(73 code: 0,74 status_message: "",75 body: "",76 headers: {},77 return_code: :operation_timedout78 )79 else80 ::Typhoeus::Response.new(81 code: webmock_response.status[0],82 status_message: webmock_response.status[1],83 body: webmock_response.body,84 headers: webmock_response.headers,85 effective_url: request_signature.uri86 )87 end88 response.mock = :webmock89 response90 end91 def self.request_hash(request_signature)92 hash = {}93 hash[:body] = request_signature.body94 hash[:headers] = request_signature.headers95 hash96 end97 AFTER_REQUEST_CALLBACK = Proc.new do |response|98 request = response.request99 request_signature = request.instance_variable_get(:@__webmock_request_signature)100 webmock_response =101 ::WebMock::HttpLibAdapters::TyphoeusAdapter.102 build_webmock_response(response)103 if response.mock104 WebMock::CallbackRegistry.invoke_callbacks(105 {lib: :typhoeus},106 request_signature,107 webmock_response108 )109 else110 WebMock::CallbackRegistry.invoke_callbacks(111 {lib: :typhoeus, real_request: true},112 request_signature,113 webmock_response114 )115 end116 end117 BEFORE_CALLBACK = Proc.new do |request|118 Typhoeus::Expectation.all.delete_if {|e| e.from == :webmock }119 res = true120 unless WebMock::HttpLibAdapters::TyphoeusAdapter.disabled?121 request_signature = ::WebMock::HttpLibAdapters::TyphoeusAdapter.build_request_signature(request)122 request.block_connection = false;123 ::WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)124 if webmock_response = ::WebMock::StubRegistry.instance.response_for_request(request_signature)125 # ::WebMock::HttpLibAdapters::TyphoeusAdapter.stub_typhoeus(request_signature, webmock_response, self)126 response = ::WebMock::HttpLibAdapters::TyphoeusAdapter.generate_typhoeus_response(request_signature, webmock_response)127 if request.respond_to?(:on_headers)128 request.execute_headers_callbacks(response)129 end130 if request.respond_to?(:streaming?) && request.streaming?131 response.options[:response_body] = ""132 request.on_body.each { |callback| callback.call(webmock_response.body, response) }133 end134 request.finish(response)135 webmock_response.raise_error_if_any136 res = false137 elsif !WebMock.net_connect_allowed?(request_signature.uri)138 raise WebMock::NetConnectNotAllowedError.new(request_signature)139 end140 end141 res142 end143 end144 end145 end146end...

Full Screen

Full Screen

webmock.rb

Source:webmock.rb Github

copy

Full Screen

1require 'vcr/util/version_checker'2require 'vcr/request_handler'3require 'webmock'4VCR::VersionChecker.new('WebMock', WebMock.version, '1.8.0', '1.8').check_version!5module VCR6 class LibraryHooks7 # @private8 module WebMock9 extend self10 attr_accessor :global_hook_disabled11 alias global_hook_disabled? global_hook_disabled12 def with_global_hook_disabled13 self.global_hook_disabled = true14 begin15 yield16 ensure17 self.global_hook_disabled = false18 end19 end20 # @private21 module Helpers22 def vcr_request_for(webmock_request)23 VCR::Request.new \24 webmock_request.method,25 webmock_request.uri.to_s,26 webmock_request.body,27 request_headers_for(webmock_request)28 end29 # @private30 def vcr_response_for(webmock_response)31 VCR::Response.new \32 VCR::ResponseStatus.new(*webmock_response.status),33 webmock_response.headers,34 webmock_response.body,35 nil36 end37 if defined?(::Excon)38 # @private39 def request_headers_for(webmock_request)40 return nil unless webmock_request.headers41 # WebMock hooks deeply into a Excon at a place where it manually adds a "Host"42 # header, but this isn't a header we actually care to store...43 webmock_request.headers.dup.tap do |headers|44 headers.delete("Host")45 end46 end47 else48 # @private49 def request_headers_for(webmock_request)50 webmock_request.headers51 end52 end53 def typed_request_for(webmock_request, remove = false)54 if webmock_request.instance_variables.find { |v| v.to_sym == :@__typed_vcr_request }55 meth = remove ? :remove_instance_variable : :instance_variable_get56 return webmock_request.send(meth, :@__typed_vcr_request)57 end58 warn <<-EOS.gsub(/^\s+\|/, '')59 |WARNING: There appears to be a bug in WebMock's after_request hook60 | and VCR is attempting to work around it. Some VCR features61 | may not work properly.62 EOS63 Request::Typed.new(vcr_request_for(webmock_request), :unknown)64 end65 end66 class RequestHandler < ::VCR::RequestHandler67 include Helpers68 attr_reader :request69 def initialize(request)70 @request = request71 end72 private73 def externally_stubbed?74 # prevent infinite recursion...75 VCR::LibraryHooks::WebMock.with_global_hook_disabled do76 ::WebMock.registered_request?(request)77 end78 end79 def set_typed_request_for_after_hook(*args)80 super81 request.instance_variable_set(:@__typed_vcr_request, @after_hook_typed_request)82 end83 def vcr_request84 @vcr_request ||= vcr_request_for(request)85 end86 def on_externally_stubbed_request87 # nil allows WebMock to handle the request88 nil89 end90 def on_unhandled_request91 invoke_after_request_hook(nil)92 super93 end94 def on_stubbed_by_vcr_request95 {96 :body => stubbed_response.body,97 :status => [stubbed_response.status.code.to_i, stubbed_response.status.message],98 :headers => stubbed_response.headers99 }100 end101 end102 extend Helpers103 ::WebMock.globally_stub_request do |req|104 global_hook_disabled? ? nil : RequestHandler.new(req).handle105 end106 ::WebMock.after_request(:real_requests_only => true) do |request, response|107 unless VCR.library_hooks.disabled?(:webmock)108 http_interaction = VCR::HTTPInteraction.new \109 typed_request_for(request), vcr_response_for(response)110 VCR.record_http_interaction(http_interaction)111 end112 end113 ::WebMock.after_request do |request, response|114 unless VCR.library_hooks.disabled?(:webmock)115 VCR.configuration.invoke_hook \116 :after_http_request,117 typed_request_for(request, :remove),118 vcr_response_for(response)119 end120 end121 end122 end123end124# @private125module WebMock126 class << self127 # ensure HTTP requests are always allowed; VCR takes care of disallowing128 # them at the appropriate times in its hook129 def net_connect_allowed_with_vcr?(*args)130 VCR.turned_on? ? true : net_connect_allowed_without_vcr?(*args)131 end132 alias net_connect_allowed_without_vcr? net_connect_allowed?133 alias net_connect_allowed? net_connect_allowed_with_vcr?134 end unless respond_to?(:net_connect_allowed_with_vcr?)135end...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Webmock_ruby automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful