Skip to content

Commit

Permalink
Drop support for typhoeus 0.4 (#905)
Browse files Browse the repository at this point in the history
typhoeus 0.4 was release almost 10 years ago and is long outdated. This removes all patches and specs that target this version.
  • Loading branch information
aka47 committed Sep 1, 2021
1 parent b1e8703 commit 2b46c02
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 306 deletions.
7 changes: 0 additions & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ jobs:
ruby-version: ${{ matrix.ruby-version }}
bundler-cache: true

- name: typhoeus-0.4
run: |
bundle install
bundle exec rspec spec/lib/vcr/library_hooks/typhoeus_0.4_spec.rb 2>> "${ALL_WARNINGS}"
env:
BUNDLE_GEMFILE: Gemfile.typhoeus-0.4

- name: faraday-1.0.0
run: |
bundle install
Expand Down
5 changes: 0 additions & 5 deletions Appraisals
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
appraise "typhoeus-old" do
gem 'typhoeus', '~> 0.4.2'
gem 'webmock', '1.8.11'
end

appraise "faraday-old" do
gem 'faraday', '~> 0.8.8'
end
8 changes: 0 additions & 8 deletions Gemfile.typhoeus-0.4

This file was deleted.

205 changes: 94 additions & 111 deletions lib/vcr/library_hooks/typhoeus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,137 +2,121 @@
require 'vcr/request_handler'
require 'typhoeus'

if Float(Typhoeus::VERSION[/^\d+\.\d+/]) < 0.5
require 'vcr/library_hooks/typhoeus_0.4'
else
VCR::VersionChecker.new('Typhoeus', Typhoeus::VERSION, '0.5.0').check_version!

module VCR
class LibraryHooks
module VCR
class LibraryHooks
# @private
module Typhoeus
# @private
module Typhoeus
# @private
class RequestHandler < ::VCR::RequestHandler
attr_reader :request
def initialize(request)
@request = request
request.block_connection = false if VCR.turned_on?
end

def vcr_request
@vcr_request ||= VCR::Request.new \
request.options.fetch(:method, :get),
request.url,
request_body,
request.options.fetch(:headers, {})
end

private
class RequestHandler < ::VCR::RequestHandler
attr_reader :request
def initialize(request)
@request = request
request.block_connection = false if VCR.turned_on?
end

def externally_stubbed?
::Typhoeus::Expectation.find_by(request)
end
def vcr_request
@vcr_request ||= VCR::Request.new \
request.options.fetch(:method, :get),
request.url,
request.encoded_body,
request.options.fetch(:headers, {})
end

def set_typed_request_for_after_hook(*args)
super
request.instance_variable_set(:@__typed_vcr_request, @after_hook_typed_request)
end
private

def on_unhandled_request
invoke_after_request_hook(nil)
super
end
def externally_stubbed?
::Typhoeus::Expectation.find_by(request)
end

def on_stubbed_by_vcr_request
response = ::Typhoeus::Response.new \
:http_version => stubbed_response.http_version,
:code => stubbed_response.status.code,
:status_message => stubbed_response.status.message,
:headers => stubbed_response_headers,
:body => stubbed_response.body,
:effective_url => stubbed_response.adapter_metadata.fetch('effective_url', request.url),
:mock => true

first_header_line = "HTTP/#{stubbed_response.http_version} #{response.code} #{response.status_message}\r\n"
response.instance_variable_set(:@first_header_line, first_header_line)
response.instance_variable_get(:@options)[:response_headers] =
first_header_line + response.headers.map { |k,v| "#{k}: #{v}"}.join("\r\n")

response
end
def set_typed_request_for_after_hook(*args)
super
request.instance_variable_set(:@__typed_vcr_request, @after_hook_typed_request)
end

def stubbed_response_headers
@stubbed_response_headers ||= {}.tap do |hash|
stubbed_response.headers.each do |key, values|
hash[key] = values.size == 1 ? values.first : values
end if stubbed_response.headers
end
end
def on_unhandled_request
invoke_after_request_hook(nil)
super
end

if ::Typhoeus::Request.method_defined?(:encoded_body)
def request_body
request.encoded_body
end
else
def request_body
request.options.fetch(:body, "")
end
end
def on_stubbed_by_vcr_request
response = ::Typhoeus::Response.new \
:http_version => stubbed_response.http_version,
:code => stubbed_response.status.code,
:status_message => stubbed_response.status.message,
:headers => stubbed_response_headers,
:body => stubbed_response.body,
:effective_url => stubbed_response.adapter_metadata.fetch('effective_url', request.url),
:mock => true

first_header_line = "HTTP/#{stubbed_response.http_version} #{response.code} #{response.status_message}\r\n"
response.instance_variable_set(:@first_header_line, first_header_line)
response.instance_variable_get(:@options)[:response_headers] =
first_header_line + response.headers.map { |k,v| "#{k}: #{v}"}.join("\r\n")

response
end

# @private
class << self
def vcr_response_from(response)
VCR::Response.new \
VCR::ResponseStatus.new(response.code, response.status_message),
response.headers,
response.body,
response.http_version,
{ "effective_url" => response.effective_url }
def stubbed_response_headers
@stubbed_response_headers ||= {}.tap do |hash|
stubbed_response.headers.each do |key, values|
hash[key] = values.size == 1 ? values.first : values
end if stubbed_response.headers
end
end
end

def collect_chunks(request)
chunks = ''
request.on_body.unshift(
Proc.new do |body, response|
chunks += body
request.instance_variable_set(:@chunked_body, chunks)
end
)
end
# @private
class << self
def vcr_response_from(response)
VCR::Response.new \
VCR::ResponseStatus.new(response.code, response.status_message),
response.headers,
response.body,
response.http_version,
{ "effective_url" => response.effective_url }
end

def restore_body_from_chunks(response, request)
response.options[:response_body] = request.instance_variable_get(:@chunked_body)
end
def collect_chunks(request)
chunks = ''
request.on_body.unshift(
Proc.new do |body, response|
chunks += body
request.instance_variable_set(:@chunked_body, chunks)
end
)
end

::Typhoeus.on_complete do |response|
request = response.request
def restore_body_from_chunks(response, request)
response.options[:response_body] = request.instance_variable_get(:@chunked_body)
end
end

restore_body_from_chunks(response, request) if request.streaming?
::Typhoeus.on_complete do |response|
request = response.request

unless VCR.library_hooks.disabled?(:typhoeus)
vcr_response = vcr_response_from(response)
typed_vcr_request = request.send(:remove_instance_variable, :@__typed_vcr_request)
restore_body_from_chunks(response, request) if request.streaming?

unless request.response.mock
http_interaction = VCR::HTTPInteraction.new(typed_vcr_request, vcr_response)
VCR.record_http_interaction(http_interaction)
end
unless VCR.library_hooks.disabled?(:typhoeus)
vcr_response = vcr_response_from(response)
typed_vcr_request = request.send(:remove_instance_variable, :@__typed_vcr_request)

VCR.configuration.invoke_hook(:after_http_request, typed_vcr_request, vcr_response)
unless request.response.mock
http_interaction = VCR::HTTPInteraction.new(typed_vcr_request, vcr_response)
VCR.record_http_interaction(http_interaction)
end

VCR.configuration.invoke_hook(:after_http_request, typed_vcr_request, vcr_response)
end
end

::Typhoeus.before do |request|
collect_chunks(request) if request.streaming?
if response = VCR::LibraryHooks::Typhoeus::RequestHandler.new(request).handle
request.on_headers.each { |cb| cb.call(response) }
request.on_body.each { |cb| cb.call(response.body, response) }
request.finish(response)
else
true
end
::Typhoeus.before do |request|
collect_chunks(request) if request.streaming?
if response = VCR::LibraryHooks::Typhoeus::RequestHandler.new(request).handle
request.on_headers.each { |cb| cb.call(response) }
request.on_body.each { |cb| cb.call(response.body, response) }
request.finish(response)
else
true
end
end
end
Expand All @@ -146,4 +130,3 @@ def restore_body_from_chunks(response, request)
WebMock::HttpLibAdapters::TyphoeusAdapter.disable!
end
end

103 changes: 0 additions & 103 deletions lib/vcr/library_hooks/typhoeus_0.4.rb

This file was deleted.

Loading

0 comments on commit 2b46c02

Please sign in to comment.