Best Vcr_ruby code snippet using Header.stubbed
recursive_webhook_detection_spec.rb
Source:recursive_webhook_detection_spec.rb
...6 let_it_be(:project) { create(:project, :repository, namespace: user.namespace, creator: user) }7 let_it_be(:merge_request) { create(:merge_request, source_project: project) }8 let_it_be(:project_hook) { create(:project_hook, project: project, merge_requests_events: true) }9 let_it_be(:system_hook) { create(:system_hook, merge_requests_events: true) }10 let(:stubbed_project_hook_hostname) { stubbed_hostname(project_hook.url, hostname: stubbed_project_hook_ip_address) }11 let(:stubbed_system_hook_hostname) { stubbed_hostname(system_hook.url, hostname: stubbed_system_hook_ip_address) }12 let(:stubbed_project_hook_ip_address) { '8.8.8.8' }13 let(:stubbed_system_hook_ip_address) { '8.8.8.9' }14 # Trigger a change to the merge request to fire the webhooks.15 def trigger_web_hooks16 params = { merge_request: { description: FFaker::Lorem.sentence } }17 put project_merge_request_path(project, merge_request), params: params, headers: headers18 end19 def stub_requests20 stub_full_request(project_hook.url, method: :post, ip_address: stubbed_project_hook_ip_address)21 stub_full_request(system_hook.url, method: :post, ip_address: stubbed_system_hook_ip_address)22 end23 before do24 login_as(user)25 end26 context 'when the request headers include the recursive webhook detection header' do27 let(:uuid) { SecureRandom.uuid }28 let(:headers) { { Gitlab::WebHooks::RecursionDetection::UUID::HEADER => uuid } }29 it 'executes all webhooks, logs no errors, and the webhook requests contain the same UUID header', :aggregate_failures do30 stub_requests31 expect(Gitlab::AuthLogger).not_to receive(:error)32 trigger_web_hooks33 expect(WebMock).to have_requested(:post, stubbed_project_hook_hostname)34 .with { |req| req.headers['X-Gitlab-Event-Uuid'] == uuid }35 .once36 expect(WebMock).to have_requested(:post, stubbed_system_hook_hostname)37 .with { |req| req.headers['X-Gitlab-Event-Uuid'] == uuid }38 .once39 end40 context 'when one of the webhooks is recursive' do41 before do42 # Recreate the necessary state for the previous request to be43 # considered made from the webhook.44 Gitlab::WebHooks::RecursionDetection.set_request_uuid(uuid)45 Gitlab::WebHooks::RecursionDetection.register!(project_hook)46 Gitlab::WebHooks::RecursionDetection.set_request_uuid(nil)47 end48 it 'blocks and logs an error for the recursive webhook, but execute the non-recursive webhook', :aggregate_failures do49 stub_requests50 expect(Gitlab::AuthLogger).to receive(:error).with(51 include(52 message: 'Recursive webhook blocked from executing',53 hook_id: project_hook.id,54 recursion_detection: {55 uuid: uuid,56 ids: [project_hook.id]57 }58 )59 ).once60 trigger_web_hooks61 expect(WebMock).not_to have_requested(:post, stubbed_project_hook_hostname)62 expect(WebMock).to have_requested(:post, stubbed_system_hook_hostname).once63 end64 end65 context 'when the count limit has been reached' do66 let_it_be(:previous_hooks) { create_list(:project_hook, 3) }67 before do68 stub_const('Gitlab::WebHooks::RecursionDetection::COUNT_LIMIT', 2)69 # Recreate the necessary state for a number of previous webhooks to70 # have been triggered previously.71 Gitlab::WebHooks::RecursionDetection.set_request_uuid(uuid)72 previous_hooks.each { Gitlab::WebHooks::RecursionDetection.register!(_1) }73 Gitlab::WebHooks::RecursionDetection.set_request_uuid(nil)74 end75 it 'blocks and logs errors for all hooks', :aggregate_failures do76 stub_requests77 previous_hook_ids = previous_hooks.map(&:id)78 expect(Gitlab::AuthLogger).to receive(:error).with(79 include(80 message: 'Recursive webhook blocked from executing',81 hook_id: project_hook.id,82 recursion_detection: {83 uuid: uuid,84 ids: include(*previous_hook_ids)85 }86 )87 ).once88 expect(Gitlab::AuthLogger).to receive(:error).with(89 include(90 message: 'Recursive webhook blocked from executing',91 hook_id: system_hook.id,92 recursion_detection: {93 uuid: uuid,94 ids: include(*previous_hook_ids)95 }96 )97 ).once98 trigger_web_hooks99 expect(WebMock).not_to have_requested(:post, stubbed_project_hook_hostname)100 expect(WebMock).not_to have_requested(:post, stubbed_system_hook_hostname)101 end102 end103 end104 context 'when the recursive webhook detection header is absent' do105 let(:headers) { {} }106 let(:uuid_header_spy) do107 Class.new do108 attr_reader :values109 def initialize110 @values = []111 end112 def to_proc113 proc do |method, *args|114 method.call(*args).tap do |headers|115 @values << headers[Gitlab::WebHooks::RecursionDetection::UUID::HEADER]116 end117 end118 end119 end.new120 end121 before do122 allow(Gitlab::WebHooks::RecursionDetection).to receive(:header).at_least(:once).and_wrap_original(&uuid_header_spy)123 end124 it 'executes all webhooks, logs no errors, and the webhook requests contain different UUID headers', :aggregate_failures do125 stub_requests126 expect(Gitlab::AuthLogger).not_to receive(:error)127 trigger_web_hooks128 uuid_headers = uuid_header_spy.values129 expect(uuid_headers).to all(be_present)130 expect(uuid_headers.uniq.length).to eq(2)131 expect(WebMock).to have_requested(:post, stubbed_project_hook_hostname)132 .with { |req| uuid_headers.include?(req.headers['X-Gitlab-Event-Uuid']) }133 .once134 expect(WebMock).to have_requested(:post, stubbed_system_hook_hostname)135 .with { |req| uuid_headers.include?(req.headers['X-Gitlab-Event-Uuid']) }136 .once137 end138 it 'uses new UUID values between requests' do139 stub_requests140 trigger_web_hooks141 trigger_web_hooks142 uuid_headers = uuid_header_spy.values143 expect(uuid_headers).to all(be_present)144 expect(uuid_headers.length).to eq(4)145 expect(uuid_headers.uniq.length).to eq(4)146 expect(WebMock).to have_requested(:post, stubbed_project_hook_hostname).twice147 expect(WebMock).to have_requested(:post, stubbed_system_hook_hostname).twice148 end149 end150end...
typhoeus.rb
Source:typhoeus.rb
...19 request.encoded_body,20 request.options.fetch(:headers, {})21 end22 private23 def externally_stubbed?24 ::Typhoeus::Expectation.find_by(request)25 end26 def set_typed_request_for_after_hook(*args)27 super28 request.instance_variable_set(:@__typed_vcr_request, @after_hook_typed_request)29 end30 def on_unhandled_request31 invoke_after_request_hook(nil)32 super33 end34 def on_stubbed_by_vcr_request35 response = ::Typhoeus::Response.new \36 :http_version => stubbed_response.http_version,37 :code => stubbed_response.status.code,38 :status_message => stubbed_response.status.message,39 :headers => stubbed_response_headers,40 :body => stubbed_response.body,41 :effective_url => stubbed_response.adapter_metadata.fetch('effective_url', request.url),42 :mock => true43 first_header_line = "HTTP/#{stubbed_response.http_version} #{response.code} #{response.status_message}\r\n"44 response.instance_variable_set(:@first_header_line, first_header_line)45 response.instance_variable_get(:@options)[:response_headers] =46 first_header_line + response.headers.map { |k,v| "#{k}: #{v}"}.join("\r\n")47 response48 end49 def stubbed_response_headers50 @stubbed_response_headers ||= {}.tap do |hash|51 stubbed_response.headers.each do |key, values|52 hash[key] = values.size == 1 ? values.first : values53 end if stubbed_response.headers54 end55 end56 end57 # @private58 class << self59 def vcr_response_from(response)60 VCR::Response.new \61 VCR::ResponseStatus.new(response.code, response.status_message),62 response.headers,63 response.body,64 response.http_version,65 { "effective_url" => response.effective_url }66 end67 def collect_chunks(request)...
stubbed
Using AI Code Generation
1 File.read('header.txt')2 File.read('header.txt')3 File.read('header.txt')4 File.read('header.txt')5 File.read('header.txt')6 File.read('header.txt')7 File.read('header.txt')
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!!