Add bin and edit workflow
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 9s

This commit is contained in:
2026-09-16 13:11:16 -06:00
parent c8ac4fcae5
commit 4cee170d66
17576 changed files with 895740 additions and 2 deletions
@@ -0,0 +1,339 @@
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("1.9.0")
require 'spec_helper'
require 'typhoeus/adapters/faraday'
describe Faraday::Adapter::Typhoeus do
let(:base_url) { "http://localhost:3001" }
let(:adapter) { described_class.new(nil) }
let(:request) { Typhoeus::Request.new(base_url) }
let(:conn) do
Faraday.new(:url => base_url) do |faraday|
faraday.adapter :typhoeus
end
end
let(:response) { conn.get("/") }
context "when parallel" do
it "returns a faraday response" do
response = nil
conn.in_parallel { response = conn.get("/") }
expect(response).to be_a(Faraday::Response)
end
it "succeeds" do
response = nil
conn.in_parallel { response = conn.get("/") }
expect(response.status).to be(200)
end
end
context "when not parallel" do
it "returns a faraday response" do
expect(response).to be_a(Faraday::Response)
end
it "succeeds" do
expect(response.status).to be(200)
end
end
context "when a response is stubbed" do
before do
stub = Typhoeus::Response.new \
:code => 200,
:headers => { "Foo" => "2", "Bar" => "3" },
:body => "Hello",
:mock => true
Typhoeus.stub(base_url + '/').and_return(stub)
end
it 'stubs the status code' do
expect(response.status).to eq(200)
end
it 'stubs the response body' do
expect(response.body).to eq("Hello")
end
it 'stubs the headers' do
expect(response.headers).to eq("Foo" => "2", "Bar" => "3")
end
end
describe "#initialize" do
let(:request) { adapter.method(:typhoeus_request).call({}) }
context "when typhoeus request options specified" do
let(:adapter) { described_class.new(nil, { :forbid_reuse => true, :maxredirs => 1 }) }
it "should set option for request" do
expect(request.options[:forbid_reuse]).to be_truthy
expect(request.options[:maxredirs]).to eq(1)
end
end
end
describe "#perform_request" do
let(:env) { {} }
context "when body" do
let(:env) { { :body => double(:read => "body") } }
it "reads body" do
expect(adapter.method(:read_body).call(env)).to eq("body")
end
end
context "parallel_manager" do
context "when given" do
let(:env) { { :parallel_manager => double(:queue => true), :ssl => {}, :request => {} } }
it "uses" do
adapter.method(:perform_request).call(env)
end
end
context "when not given" do
let(:env) { { :method => :get, :ssl => {}, :request => {} } }
it "falls back to single" do
expect(Typhoeus::Request).to receive(:new).and_return(double(:options => {}, :on_complete => [], :run => true))
adapter.method(:perform_request).call(env)
end
end
end
end
describe "#request" do
let(:env) do
{ :url => "url", :method => :get, :body => "body", :request_headers => {}, :ssl => {}, :request => {} }
end
let(:request) { adapter.method(:request).call(env) }
it "returns request" do
expect(request).to be_a(Typhoeus::Request)
end
it "sets url" do
expect(request.base_url).to eq("url")
end
it "sets http method" do
expect(request.original_options[:method]).to eq(:get)
end
it "sets body" do
expect(request.original_options[:body]).to eq("body")
end
it "sets headers" do
expect(request.original_options[:headers]).to eq({})
end
it "sets on_complete callback" do
expect(request.on_complete.size).to eq(1)
end
end
context "when the connection failed" do
before do
stub = Typhoeus::Response.new \
:response_code => 0,
:return_code => 0,
:mock => true
Typhoeus.stub(base_url + '/').and_return(stub)
end
context "when parallel" do
it "isn't successful" do
response = nil
conn.in_parallel { response = conn.get("/") }
expect(response.success?).to be_falsey
end
it "translates the response code into an error message" do
response = nil
conn.in_parallel { response = conn.get("/") }
expect(response.env[:typhoeus_return_message]).to eq("No error")
end
end
context "when not parallel" do
it "raises an error" do
expect { conn.get("/") }.to raise_error(Faraday::ConnectionFailed, "No error")
end
end
end
describe "#configure_socket" do
let(:env) { { :request => { :bind => { :host => "interface" } } } }
before { adapter.method(:configure_socket).call(request, env) }
context "when host" do
it "sets interface" do
expect(request.options[:interface]).to eq("interface")
end
end
end
describe "#configure_timeout" do
before { adapter.method(:configure_timeout).call(request, env) }
context "when timeout" do
let(:env) { { :request => { :timeout => 1 } } }
it "sets timeout_ms" do
expect(request.options[:timeout_ms]).to eq(1000)
end
end
context "when open_timeout" do
let(:env) { { :request => { :open_timeout => 1 } } }
it "sets connecttimeout_ms" do
expect(request.options[:connecttimeout_ms]).to eq(1000)
end
end
end
describe "#configure_proxy" do
before { adapter.method(:configure_proxy).call(request, env) }
context "when proxy" do
let(:env) { { :request => { :proxy => { :uri => double(:scheme => 'http', :host => "localhost", :port => "3001") } } } }
it "sets proxy" do
expect(request.options[:proxy]).to eq("http://localhost:3001")
end
context "when username and password" do
let(:env) do
{ :request => { :proxy => {
:uri => double(:scheme => 'http', :host => :a, :port => :b),
:user => "a",
:password => "b"
} } }
end
it "sets proxyuserpwd" do
expect(request.options[:proxyuserpwd]).to eq("a:b")
end
end
end
end
describe "#configure_ssl" do
before { adapter.method(:configure_ssl).call(request, env) }
context "when version" do
let(:env) { { :ssl => { :version => "a" } } }
it "sets sslversion" do
expect(request.options[:sslversion]).to eq("a")
end
end
context "when client_cert" do
let(:env) { { :ssl => { :client_cert => "a" } } }
it "sets sslcert" do
expect(request.options[:sslcert]).to eq("a")
end
end
context "when client_key" do
let(:env) { { :ssl => { :client_key => "a" } } }
it "sets sslkey" do
expect(request.options[:sslkey]).to eq("a")
end
end
context "when ca_file" do
let(:env) { { :ssl => { :ca_file => "a" } } }
it "sets cainfo" do
expect(request.options[:cainfo]).to eq("a")
end
end
context "when ca_path" do
let(:env) { { :ssl => { :ca_path => "a" } } }
it "sets capath" do
expect(request.options[:capath]).to eq("a")
end
end
context "when client_cert_passwd" do
let(:env) { { :ssl => { :client_cert_passwd => "a" } } }
it "sets keypasswd to the value of client_cert_passwd" do
expect(request.options[:keypasswd]).to eq("a")
end
end
context "when client_certificate_password" do
let(:env) { { :ssl => { :client_certificate_password => "a" } } }
it "sets keypasswd to the value of client_cert_passwd" do
expect(request.options[:keypasswd]).to eq("a")
end
end
context "when no client_cert_passwd" do
let(:env) { { :ssl => { } } }
it "does not set keypasswd on options" do
expect(request.options).not_to have_key :keypasswd
end
end
context "when verify is false" do
let(:env) { { :ssl => { :verify => false } } }
it "sets ssl_verifyhost to 0" do
expect(request.options[:ssl_verifyhost]).to eq(0)
end
it "sets ssl_verifypeer to false" do
expect(request.options[:ssl_verifypeer]).to be_falsey
end
end
context "when verify is true" do
let(:env) { { :ssl => { :verify => true } } }
it "sets ssl_verifyhost to 2" do
expect(request.options[:ssl_verifyhost]).to eq(2)
end
it "sets ssl_verifypeer to true" do
expect(request.options[:ssl_verifypeer]).to be_truthy
end
end
end
describe "#parallel?" do
context "when parallel_manager" do
let(:env) { { :parallel_manager => true } }
it "returns true" do
expect(adapter.method(:parallel?).call(env)).to be_truthy
end
end
context "when no parallel_manager" do
let(:env) { { :parallel_manager => nil } }
it "returns false" do
expect(adapter.method(:parallel?).call(env)).to be_falsey
end
end
end
end
end
@@ -0,0 +1,41 @@
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("1.9.0")
require 'dalli'
require 'typhoeus/cache/dalli'
require 'spec_helper'
describe Typhoeus::Cache::Dalli do
let(:dalli) { instance_double(Dalli::Client) }
let(:cache) { Typhoeus::Cache::Dalli.new(dalli) }
let(:base_url) { "localhost:3001" }
let(:request) { Typhoeus::Request.new(base_url, {:method => :get}) }
let(:response) { Typhoeus::Response.new(:response_code => 0, :return_code => 0, :mock => true) }
describe "#set" do
it "sends the request to Dalli" do
expect(dalli).to receive(:set).with(request.cache_key, response, nil)
cache.set(request, response)
end
end
describe "#get" do
it "returns nil when the key is not in the cache" do
expect(dalli).to receive(:get).with(request.cache_key).and_return(nil)
expect(cache.get(request)).to be_nil
end
it "returns the cached response when the key is in cache" do
expect(dalli).to receive(:get).with(request.cache_key).and_return(response)
result = cache.get(request)
expect(result).to_not be_nil
expect(result.response_code).to eq(response.response_code)
expect(result.return_code).to eq(response.return_code)
expect(result.headers).to eq(response.headers)
expect(result.body).to eq(response.body)
end
end
end
end
@@ -0,0 +1,41 @@
require 'redis'
require 'typhoeus/cache/redis'
require 'spec_helper'
describe Typhoeus::Cache::Redis do
let(:redis) { instance_double(Redis) }
let(:cache) { Typhoeus::Cache::Redis.new(redis) }
let(:base_url) { "localhost:3001" }
let(:request) { Typhoeus::Request.new(base_url, {:method => :get}) }
let(:response) { Typhoeus::Response.new(:response_code => 0, :return_code => 0, :mock => true) }
let(:serialized_response) { Marshal.dump(response) }
describe "#set" do
it "sends the serialized request to Redis" do
expect(redis).to receive(:set).with(request.cache_key, serialized_response)
expect(redis).to_not receive(:expire).with(request.cache_key, request.cache_ttl)
cache.set(request, response)
end
end
describe "#get" do
it "returns nil when the key is not in Redis" do
expect(redis).to receive(:get).with(request.cache_key).and_return(nil)
expect(cache.get(request)).to be_nil
end
it "returns the cached response when the key is in Redis" do
expect(redis).to receive(:get).with(request.cache_key).and_return(serialized_response)
result = cache.get(request)
expect(result).to_not be_nil
expect(result.response_code).to eq(response.response_code)
expect(result.return_code).to eq(response.return_code)
expect(result.headers).to eq(response.headers)
expect(result.body).to eq(response.body)
end
end
end
@@ -0,0 +1,15 @@
require 'spec_helper'
describe Typhoeus::Config do
let(:config) { Typhoeus::Config }
[:block_connection, :memoize, :verbose, :cache, :user_agent, :proxy].each do |name|
it "responds to #{name}" do
expect(config).to respond_to(name)
end
it "responds to #{name}=" do
expect(config).to respond_to("#{name}=")
end
end
end
@@ -0,0 +1,143 @@
require 'spec_helper'
describe Typhoeus::EasyFactory do
let(:base_url) { "http://localhost:3001" }
let(:hydra) { Typhoeus::Hydra.new(:max_concurrency => 1) }
let(:options) { {} }
let(:request) { Typhoeus::Request.new(base_url, options) }
let(:easy_factory) { described_class.new(request, hydra) }
describe "#get" do
context "when option[:cache_ttl]" do
let(:options) { {:cache_ttl => 1} }
it "creates Ethon::Easy" do
expect(easy_factory.get).to be_a(Ethon::Easy)
end
end
context "timeouts" do
it "sets nosignal to true by default" do
expect(easy_factory.easy).to receive(:http_request).with(anything(), anything(), hash_including(:nosignal => true))
easy_factory.get
end
context "when timeout is not a whole number and timeout_ms is not set" do
let(:options) { {:timeout => 0.1} }
it "ceils timeout and sets timeout_ms" do
expect(easy_factory.easy).to receive(:http_request).with(anything(), anything(), hash_including(:timeout_ms => 100, :timeout => 1))
easy_factory.get
end
end
context "when timeout is not a whole number and timeout_ms is set" do
let(:options) { {:timeout => 0.1, :timeout_ms => 123} }
it "ceils timeout and does not change timeout_ms" do
expect(easy_factory.easy).to receive(:http_request).with(anything(), anything(), hash_including(:timeout_ms => 123, :timeout => 1))
easy_factory.get
end
end
context "when connecttimeout is not a whole number and connecttimeout_ms is not set" do
let(:options) { {:connecttimeout => 0.1} }
it "ceils connecttimeout and sets connecttimeout_ms" do
expect(easy_factory.easy).to receive(:http_request).with(anything(), anything(), hash_including(:connecttimeout_ms => 100, :connecttimeout => 1))
easy_factory.get
end
end
context "when connecttimeout is not a whole number and connecttimeout_ms is set" do
let(:options) { {:connecttimeout => 0.1, :connecttimeout_ms => 123} }
it "ceils connecttimeout and does not change connecttimeout_ms" do
expect(easy_factory.easy).to receive(:http_request).with(anything(), anything(), hash_including(:connecttimeout_ms => 123, :connecttimeout => 1))
easy_factory.get
end
end
end
context "when invalid option" do
let(:options) { {:invalid => 1} }
it "reraises" do
expect{ easy_factory.get }.to raise_error(Ethon::Errors::InvalidOption)
end
end
context "when removed option" do
let(:options) { {:cache_timeout => 1} }
it "reraises with help" do
expect{ easy_factory.get }.to raise_error(
Ethon::Errors::InvalidOption, /The option cache_timeout was removed/
)
end
end
context "when changed option" do
let(:options) { {:proxy_auth_method => 1} }
it "reraises with help" do
expect{ easy_factory.get }.to raise_error(
Ethon::Errors::InvalidOption, /Please try proxyauth instead of proxy_auth_method/
)
end
end
context "when renamed option" do
let(:options) { {:connect_timeout => 1} }
it "warns" do
expect(easy_factory).to receive(:warn).with(
"Deprecated option connect_timeout. Please use connecttimeout instead."
)
easy_factory.get
end
it "passes correct option" do
expect(easy_factory).to receive(:warn)
expect(easy_factory.easy).to receive(:connecttimeout=).with(1)
easy_factory.get
end
end
end
describe "#set_callback" do
it "sets easy.on_progress callback when an on_progress callback is provided" do
request.on_progress { 1 }
expect(easy_factory.easy).to receive(:on_progress)
easy_factory.send(:set_callback)
end
it "sets easy.on_complete callback" do
expect(easy_factory.easy).to receive(:on_complete)
easy_factory.send(:set_callback)
end
it "finishes request" do
easy_factory.send(:set_callback)
expect(request).to receive(:finish)
easy_factory.easy.complete
end
it "resets easy" do
easy_factory.send(:set_callback)
expect(easy_factory.easy).to receive(:reset)
easy_factory.easy.complete
end
it "pushes easy back into the pool" do
easy_factory.send(:set_callback)
easy_factory.easy.complete
expect(Typhoeus::Pool.send(:easies)).to include(easy_factory.easy)
end
it "adds next request" do
easy_factory.hydra.instance_variable_set(:@queued_requests, [request])
expect(easy_factory.hydra).to receive(:add).with(request)
easy_factory.send(:set_callback)
easy_factory.easy.complete
end
end
end
@@ -0,0 +1,13 @@
require 'spec_helper'
describe Typhoeus::Errors::NoStub do
let(:base_url) { "localhost:3001" }
let(:request) { Typhoeus::Request.new(base_url) }
let(:message) { "The connection is blocked and no stub defined: " }
subject { Typhoeus::Errors::NoStub }
it "displays the request url" do
expect { raise subject.new(request) }.to raise_error(subject, message + base_url)
end
end
@@ -0,0 +1,280 @@
require 'spec_helper'
describe Typhoeus::Expectation do
let(:options) { {} }
let(:base_url) { "www.example.com" }
let(:expectation) { described_class.new(base_url, options) }
describe ".new" do
it "sets base_url" do
expect(expectation.instance_variable_get(:@base_url)).to eq(base_url)
end
it "sets options" do
expect(expectation.instance_variable_get(:@options)).to eq(options)
end
it "initializes response_counter" do
expect(expectation.instance_variable_get(:@response_counter)).to eq(0)
end
end
describe ".all" do
context "when @expectations nil" do
it "returns empty array" do
expect(Typhoeus::Expectation.all).to eq([])
end
end
context "when @expectations not nil" do
let(:expectations) { [1] }
it "returns @expectations" do
Typhoeus::Expectation.instance_variable_set(:@expectations, expectations)
expect(Typhoeus::Expectation.all).to be(expectations)
end
end
end
describe ".clear" do
let(:expectations) { double(:clear) }
it "clears all" do
expect(expectations).to receive(:clear)
Typhoeus::Expectation.instance_variable_set(:@expectations, expectations)
Typhoeus::Expectation.clear
Typhoeus::Expectation.instance_variable_set(:@expectations, nil)
end
end
describe ".response_for" do
let(:request) { Typhoeus::Request.new("") }
let(:stubbed_response) { Typhoeus::Response.new }
it "finds a matching expectation and returns its next response" do
Typhoeus::Expectation.all << expectation
expect(expectation).to receive(:matches?).with(request).and_return(true)
expect(expectation).to receive(:response).with(request).and_return(stubbed_response)
response = Typhoeus::Expectation.response_for(request)
expect(response).to be(stubbed_response)
end
it "returns nil if no matching expectation is found" do
response = Typhoeus::Expectation.response_for(request)
expect(response).to be(nil)
end
end
describe "#stubbed_from" do
it "sets value" do
expectation.stubbed_from(:webmock)
expect(expectation.from).to eq(:webmock)
end
it "returns self" do
expect(expectation.stubbed_from(:webmock)).to be(expectation)
end
end
describe "#and_return" do
context "when value" do
it "adds to responses" do
expectation.and_return(1)
expect(expectation.responses).to eq([1])
end
end
context "when array" do
it "adds to responses" do
expectation.and_return([1, 2])
expect(expectation.responses).to eq([1, 2])
end
end
context "when block" do
it "adds to responses" do
block = Proc.new {}
expectation.and_return(&block)
expect(expectation.responses).to eq([block])
end
end
end
describe "#responses" do
it "returns responses" do
expect(expectation.responses).to be_a(Array)
end
end
describe "#response" do
let(:request) { Typhoeus::Request.new("") }
before { expectation.instance_variable_set(:@responses, responses) }
context "when one response" do
context "is pre-constructed" do
let(:responses) { [Typhoeus::Response.new] }
it "returns response" do
expect(expectation.response(request)).to be(responses[0])
end
end
context "is lazily-constructed" do
def construct_response(request)
@request_from_response_construction = request
lazily_constructed_response
end
let(:lazily_constructed_response) { Typhoeus::Response.new }
let(:responses) { [ Proc.new { |request| construct_response(request) } ] }
it "returns response" do
expect(expectation.response(request)).to be(lazily_constructed_response)
expect(@request_from_response_construction).to be(request)
end
end
end
context "when multiple responses" do
let(:responses) { [Typhoeus::Response.new, Typhoeus::Response.new, Typhoeus::Response.new] }
it "returns one by one" do
3.times do |i|
expect(expectation.response(request)).to be(responses[i])
end
end
end
end
describe "#matches?" do
let(:request) { double(:base_url => nil) }
it "calls url_match?" do
expect(expectation).to receive(:url_match?)
expectation.matches?(request)
end
it "calls options_match?" do
expect(expectation).to receive(:url_match?).and_return(true)
expect(expectation).to receive(:options_match?)
expectation.matches?(request)
end
end
describe "#url_match?" do
let(:request_url) { "www.example.com" }
let(:request) { Typhoeus::Request.new(request_url) }
let(:url_match) { expectation.method(:url_match?).call(request.base_url) }
context "when string" do
context "when match" do
it "returns true" do
expect(url_match).to be_truthy
end
end
context "when no match" do
let(:base_url) { "no_match" }
it "returns false" do
expect(url_match).to be_falsey
end
end
end
context "when regexp" do
context "when match" do
let(:base_url) { /example/ }
it "returns true" do
expect(url_match).to be_truthy
end
end
context "when no match" do
let(:base_url) { /nomatch/ }
it "returns false" do
expect(url_match).to be_falsey
end
context "with nil request_url" do
let(:request_url) { nil }
it "returns false" do
expect(url_match).to be_falsey
end
end
end
end
context "when nil" do
let(:base_url) { nil }
it "returns true" do
expect(url_match).to be_truthy
end
end
context "when not string, regexp, nil" do
let(:base_url) { 1 }
it "returns false" do
expect(url_match).to be_falsey
end
end
end
describe "options_match?" do
let(:request_options) { {} }
let(:request) { Typhoeus::Request.new(nil, request_options) }
let(:options_match) { expectation.method(:options_match?).call(request) }
context "when match" do
let(:options) { { :a => 1 } }
let(:request_options) { options }
it "returns true" do
expect(options_match).to be_truthy
end
end
context "when options are a subset from request_options" do
let(:options) { { :a => 1 } }
let(:request_options) { { :a => 1, :b => 2 } }
it "returns true" do
expect(options_match).to be_truthy
end
end
context "when options are nested" do
let(:options) { { :a => { :b => 1 } } }
let(:request_options) { options }
it "returns true" do
expect(options_match).to be_truthy
end
end
context "when options contains an array" do
let(:options) { { :a => [1, 2] } }
let(:request_options) { options }
it "returns true" do
expect(options_match).to be_truthy
end
end
context "when no match" do
let(:options) { { :a => 1 } }
it "returns false" do
expect(options_match).to be_falsey
end
end
end
end
@@ -0,0 +1,22 @@
require 'spec_helper'
describe Typhoeus::Hydra::Addable do
let(:hydra) { Typhoeus::Hydra.new() }
let(:request) { Typhoeus::Request.new("localhost:3001", {:method => :get}) }
it "asks easy factory for an easy" do
multi = double
expect(Typhoeus::EasyFactory).to receive(:new).with(request, hydra).and_return(double(:get => 1))
expect(hydra).to receive(:multi).and_return(multi)
expect(multi).to receive(:add).with(1)
hydra.add(request)
end
it "adds easy to multi" do
multi = double
expect(Typhoeus::EasyFactory).to receive(:new).with(request, hydra).and_return(double(:get => 1))
expect(hydra).to receive(:multi).and_return(multi)
expect(multi).to receive(:add).with(1)
hydra.add(request)
end
end
@@ -0,0 +1,98 @@
require 'spec_helper'
describe Typhoeus::Hydra::Before do
let(:request) { Typhoeus::Request.new("") }
let(:hydra) { Typhoeus::Hydra.new }
let(:receive_counter) { double :mark => :twain }
describe "#add" do
context "when before" do
context "when one" do
it "executes" do
Typhoeus.before { |r| receive_counter.mark }
expect(receive_counter).to receive(:mark)
hydra.add(request)
end
context "when true" do
it "calls super" do
Typhoeus.before { true }
expect(Typhoeus::Expectation).to receive(:response_for)
hydra.add(request)
end
end
context "when falsy" do
context "when queue requests" do
let(:queued_request) { Typhoeus::Request.new("") }
before { hydra.queue(queued_request) }
it "dequeues" do
Typhoeus.before { false }
hydra.add(request)
expect(hydra.queued_requests).to be_empty
end
end
context "when false" do
it "doesn't call super" do
Typhoeus.before { false }
expect(Typhoeus::Expectation).to receive(:response_for).never
hydra.add(request)
end
end
context "when response" do
it "doesn't call super" do
Typhoeus.before { Typhoeus::Response.new }
expect(Typhoeus::Expectation).to receive(:response_for).never
hydra.add(request)
end
end
end
end
context "when multi" do
context "when all true" do
before { 3.times { Typhoeus.before { |r| receive_counter.mark } } }
it "calls super" do
expect(Typhoeus::Expectation).to receive(:response_for)
hydra.add(request)
end
it "executes all" do
expect(receive_counter).to receive(:mark).exactly(3).times
hydra.add(request)
end
end
context "when middle false" do
before do
Typhoeus.before { |r| receive_counter.mark }
Typhoeus.before { |r| receive_counter.mark; nil }
Typhoeus.before { |r| receive_counter.mark }
end
it "doesn't call super" do
expect(Typhoeus::Expectation).to receive(:response_for).never
hydra.add(request)
end
it "executes only two" do
expect(receive_counter).to receive(:mark).exactly(2).times
hydra.add(request)
end
end
end
end
context "when no before" do
it "calls super" do
expect(Typhoeus::Expectation).to receive(:response_for)
hydra.add(request)
end
end
end
end
@@ -0,0 +1,18 @@
require 'spec_helper'
describe Typhoeus::Hydra::BlockConnection do
let(:base_url) { "localhost:3001" }
let(:hydra) { Typhoeus::Hydra.new() }
let(:request) { Typhoeus::Request.new(base_url, {:method => :get}) }
describe "add" do
context "when block_connection activated" do
before { Typhoeus::Config.block_connection = true }
after { Typhoeus::Config.block_connection = false }
it "raises" do
expect{ hydra.add(request) }.to raise_error(Typhoeus::Errors::NoStub)
end
end
end
end
@@ -0,0 +1,88 @@
require 'spec_helper'
describe Typhoeus::Hydra::Cacheable do
let(:base_url) { "localhost:3001" }
let(:hydra) { Typhoeus::Hydra.new() }
let(:request) { Typhoeus::Request.new(base_url, {:method => :get}) }
let(:response) { Typhoeus::Response.new }
let(:cache) { MemoryCache.new }
describe "add" do
context "when cache activated" do
before { Typhoeus::Config.cache = cache }
after { Typhoeus::Config.cache = false }
context "when request new" do
it "sets no response" do
hydra.add(request)
expect(request.response).to be_nil
end
it "doesn't call complete" do
expect(request).to receive(:complete).never
hydra.add(request)
end
end
context "when request in memory" do
before { cache.memory[request] = response }
it "returns response with cached status" do
hydra.add(request)
expect(response.cached?).to be_truthy
end
context "when no queued requests" do
it "finishes request" do
expect(request).to receive(:finish).with(response)
hydra.add(request)
expect(response.cached?).to be_truthy
end
end
context "when queued requests" do
let(:queued_request) { Typhoeus::Request.new(base_url, {:method => :get}) }
before { cache.memory[queued_request] = response }
it "finishes both requests" do
hydra.queue(queued_request)
expect(request).to receive(:finish).with(response)
expect(queued_request).to receive(:finish).with(response)
hydra.add(request)
end
end
end
context "when cache is specified on a request" do
before { Typhoeus::Config.cache = false }
context "when cache is false" do
let(:non_cached_request) { Typhoeus::Request.new(base_url, {:method => :get, :cache => false}) }
it "initiates an HTTP call" do
expect(Typhoeus::EasyFactory).to receive(:new).with(non_cached_request, hydra).and_call_original
hydra.add(non_cached_request)
end
end
context "when cache is defined" do
let(:cached_request) { Typhoeus::Request.new(base_url, {:method => :get, :cache => cache}) }
before { cache.memory[cached_request] = response }
it "uses the cache instead of making a new request" do
expect(Typhoeus::EasyFactory).not_to receive(:new)
hydra.add(cached_request)
expect(cached_request.response).to be_cached
expect(cached_request.response).to eq(response)
end
end
end
end
end
end
@@ -0,0 +1,53 @@
require 'spec_helper'
describe Typhoeus::Hydra::Memoizable do
let(:base_url) { "localhost:3001" }
let(:hydra) { Typhoeus::Hydra.new() }
let(:request) { Typhoeus::Request.new(base_url) }
describe "add" do
context "when memoization activated" do
before { Typhoeus::Config.memoize = true }
context "when request new" do
it "sets no response" do
hydra.add(request)
expect(request.response).to be_nil
end
it "doesn't call complete" do
expect(request).to receive(:complete).never
hydra.add(request)
end
end
context "when request in memory" do
let(:response) { Typhoeus::Response.new }
before { hydra.memory[request] = response }
it "finishes request" do
expect(request).to receive(:finish).with(response, true)
hydra.add(request)
end
context "when queued request" do
let(:queued_request) { Typhoeus::Request.new(base_url) }
it "dequeues" do
hydra.queue(queued_request)
expect(request).to receive(:finish).with(response, true)
expect(queued_request).to receive(:finish).with(response, true)
hydra.add(request)
end
end
end
end
end
describe "#run" do
it "clears memory before starting" do
expect(hydra.memory).to receive(:clear)
hydra.run
end
end
end
@@ -0,0 +1,98 @@
require 'spec_helper'
describe Typhoeus::Hydra::Queueable do
let(:base_url) { "localhost:3001" }
let(:options) { {} }
let(:hydra) { Typhoeus::Hydra.new(options) }
describe "#queue" do
let(:request) { Typhoeus::Request.new("") }
it "accepts requests" do
hydra.queue(request)
end
it "sets hydra on request" do
hydra.queue(request)
expect(request.hydra).to eq(hydra)
end
it "adds to queued requests" do
hydra.queue(request)
expect(hydra.queued_requests).to include(request)
end
it "adds to front of queued requests" do
hydra.queue_front(request)
expect(hydra.queued_requests.first).to be(request)
end
end
describe "#abort" do
before { hydra.queued_requests << 1 }
it "clears queue" do
hydra.abort
expect(hydra.queued_requests).to be_empty
end
end
describe "#dequeue_many" do
before do
requests.each { |r| hydra.queue r }
end
context "when no request queued" do
let(:requests) { [] }
it "does nothing" do
expect(hydra).to_not receive(:add)
hydra.dequeue_many
end
end
context "when request queued" do
let(:first) { Typhoeus::Request.new("localhost:3001/first") }
let(:requests) { [first] }
it "adds request from queue to multi" do
expect(hydra).to receive(:add).with(first)
hydra.dequeue_many
end
end
context "when three request queued" do
let(:first) { Typhoeus::Request.new("localhost:3001/first") }
let(:second) { Typhoeus::Request.new("localhost:3001/second") }
let(:third) { Typhoeus::Request.new("localhost:3001/third") }
let(:requests) { [first, second, third] }
it "adds requests from queue to multi" do
expect(hydra).to receive(:add).with(first)
expect(hydra).to receive(:add).with(second)
expect(hydra).to receive(:add).with(third)
hydra.dequeue_many
end
context "when max_concurrency is two" do
let(:options) { {:max_concurrency => 2} }
it "adds requests from queue to multi" do
expect(hydra).to receive(:add).with(first)
expect(hydra).to receive(:add).with(second)
expect(hydra).to_not receive(:add).with(third)
hydra.dequeue_many
end
end
context "when max_concurrency is a string" do
let(:options) { {:max_concurrency => "2"} }
it "adds requests from queue to multi" do
expect(hydra).to receive(:add).with(first)
expect(hydra).to receive(:add).with(second)
expect(hydra).to_not receive(:add).with(third)
hydra.dequeue_many
end
end
end
end
end
@@ -0,0 +1,137 @@
require 'spec_helper'
describe Typhoeus::Hydra::Runnable do
let(:base_url) { "localhost:3001" }
let(:options) { {} }
let(:hydra) { Typhoeus::Hydra.new(options) }
let(:receive_counter) { double :mark => :twain }
describe "#run" do
let(:requests) { [] }
before do
requests.each { |r| hydra.queue r }
end
it "runs multi#dequeue_many" do
expect(hydra).to receive(:dequeue_many)
hydra.run
end
it "runs multi#perform" do
expect(hydra.multi).to receive(:perform)
hydra.run
end
context "when request queued" do
let(:first) { Typhoeus::Request.new("localhost:3001/first") }
let(:requests) { [first] }
it "sends" do
hydra.run
expect(first.response).to be
end
end
context "when three request queued" do
let(:first) { Typhoeus::Request.new("localhost:3001/first") }
let(:second) { Typhoeus::Request.new("localhost:3001/second") }
let(:third) { Typhoeus::Request.new("localhost:3001/third") }
let(:requests) { [first, second, third] }
it "sends first" do
hydra.run
expect(first.response).to be
end
it "sends second" do
hydra.run
expect(second.response).to be
end
it "sends third" do
hydra.run
expect(third.response).to be
end
it "sends first first" do
first.on_complete do
expect(second.response).to be_nil
expect(third.response).to be_nil
end
end
it "sends second second" do
first.on_complete do
expect(first.response).to be
expect(third.response).to be_nil
end
end
it "sends thirds last" do
first.on_complete do
expect(second.response).to be
expect(third.response).to be
end
end
end
context "when really queued request" do
let(:options) { {:max_concurrency => 1} }
let(:first) { Typhoeus::Request.new("localhost:3001/first") }
let(:second) { Typhoeus::Request.new("localhost:3001/second") }
let(:third) { Typhoeus::Request.new("localhost:3001/third") }
let(:requests) { [first, second, third] }
it "sends first" do
hydra.run
expect(first.response).to be
end
it "sends second" do
hydra.run
expect(second.response).to be
end
it "sends third" do
hydra.run
expect(third.response).to be
end
end
context "when request queued in callback" do
let(:first) do
Typhoeus::Request.new("localhost:3001/first").tap do |r|
r.on_complete{ hydra.queue(second) }
end
end
let(:second) { Typhoeus::Request.new("localhost:3001/second") }
let(:requests) { [first] }
before { Typhoeus.on_complete { |r| receive_counter.mark } }
after { Typhoeus.on_complete.clear; Typhoeus.before.clear }
context "when real request" do
context "when max_concurrency default" do
let(:options) { {} }
it "calls on_complete callback once for every response" do
expect(receive_counter).to receive(:mark).exactly(2).times
hydra.run
end
end
end
context "when no real request" do
context "when before hook returns and finishes response" do
before { Typhoeus.before{ |request| request.finish(Typhoeus::Response.new) } }
it "simulates real multi run and adds and finishes both requests" do
expect(receive_counter).to receive(:mark).exactly(2).times
hydra.run
end
end
end
end
end
end
@@ -0,0 +1,48 @@
require 'spec_helper'
describe Typhoeus::Hydra::Stubbable do
let(:base_url) { "localhost:3001" }
let(:request) { Typhoeus::Request.new(base_url) }
let(:response) { Typhoeus::Response.new }
let(:hydra) { Typhoeus::Hydra.new }
before { Typhoeus.stub(base_url).and_return(response) }
describe "#add" do
it "checks expectations" do
hydra.add(request)
end
context "when expectation found" do
it "calls on_headers callbacks" do
canary = :not_called
request.on_headers do
canary = :called
end
hydra.add(request)
hydra.run
expect(canary).to eq(:called)
end
it "calls on_body callbacks" do
canary = :not_called
request.on_body do
canary = :called
end
hydra.add(request)
hydra.run
expect(canary).to eq(:called)
end
it "finishes response" do
expect(request).to receive(:finish)
hydra.add(request)
end
it "is a mock" do
hydra.add(request)
expect(request.response.mock).to be(true)
end
end
end
end
@@ -0,0 +1,22 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe Typhoeus::Hydra do
let(:base_url) { "localhost:3001" }
let(:options) { {} }
let(:hydra) { Typhoeus::Hydra.new(options) }
describe "#new" do
let(:options) { {:pipeling => true} }
it "passes options to multi" do
expect(Ethon::Multi).to receive(:new).with(options)
hydra
end
end
describe "#hydra" do
it "returns a hydra" do
expect(Typhoeus::Hydra.hydra).to be_a(Typhoeus::Hydra)
end
end
end
@@ -0,0 +1,137 @@
require 'spec_helper'
describe Typhoeus::Pool do
let(:easy) { Ethon::Easy.new }
after { Typhoeus::Pool.clear }
describe "#easies" do
it "returns array" do
expect(Typhoeus::Pool.send(:easies)).to be_a(Array)
end
end
describe "#release" do
it "resets easy" do
expect(easy).to receive(:reset)
Typhoeus::Pool.release(easy)
end
it "flush cookies to disk" do
expect(easy).to receive(:cookielist=).with('flush')
expect(easy).to receive(:reset)
expect(easy).to receive(:cookielist=).with('all')
Typhoeus::Pool.release(easy)
end
it "writes cookies to disk" do
tempfile1 = Tempfile.new('cookies')
tempfile2 = Tempfile.new('cookies')
easy.cookiejar = tempfile1.path
easy.url = "localhost:3001/cookies-test"
easy.perform
Typhoeus::Pool.release(easy)
expect(File.zero?(tempfile1.path)).to be(false)
expect(File.read(tempfile1.path)).to match(/\s+foo\s+bar$/)
expect(File.read(tempfile1.path)).to match(/\s+bar\s+foo$/)
# do it again - and check if tempfile1 wasn't change
easy.cookiejar = tempfile2.path
easy.url = "localhost:3001/cookies-test2"
easy.perform
Typhoeus::Pool.release(easy)
# tempfile 1
expect(File.zero?(tempfile1.path)).to be(false)
expect(File.read(tempfile1.path)).to match(/\s+foo\s+bar$/)
expect(File.read(tempfile1.path)).to match(/\s+bar\s+foo$/)
# tempfile2
expect(File.zero?(tempfile2.path)).to be(false)
expect(File.read(tempfile2.path)).to match(/\s+foo2\s+bar$/)
expect(File.read(tempfile2.path)).to match(/\s+bar2\s+foo$/)
end
it "puts easy back into pool" do
Typhoeus::Pool.release(easy)
expect(Typhoeus::Pool.send(:easies)).to include(easy)
end
context "when threaded access" do
it "releases correct number of easies" do
(0..9).map do |n|
Thread.new do
Typhoeus::Pool.release(Ethon::Easy.new)
end
end.map(&:join)
expect(Typhoeus::Pool.send(:easies).size).to eq(10)
end
end
end
describe "#get" do
context "when easy in pool" do
before { Typhoeus::Pool.send(:easies) << easy }
it "takes" do
expect(Typhoeus::Pool.get).to eq(easy)
end
end
context "when no easy in pool" do
it "creates" do
expect(Typhoeus::Pool.get).to be_a(Ethon::Easy)
end
context "when threaded access" do
it "creates correct number of easies" do
queue = Queue.new
(0..9).map do |n|
Thread.new do
queue.enq(Typhoeus::Pool.get)
end
end.map(&:join)
array = Array.new(queue.size) { queue.pop }
expect(array.uniq.size).to eq(10)
end
end
end
context "when forked" do
before do
allow(Process).to receive(:pid).and_return(1)
Typhoeus::Pool.send(:easies) << easy
allow(Process).to receive(:pid).and_return(2)
end
after do
allow(Process).to receive(:pid).and_call_original
Typhoeus::Pool.instance_variable_set(:@pid, Process.pid)
end
it "creates" do
expect(Typhoeus::Pool.get).to_not eq(easy)
end
end
end
describe "#with" do
it "is re-entrant" do
array = []
Typhoeus::Pool.with_easy do |e1|
array << e1
Typhoeus::Pool.with_easy do |e2|
array << e2
Typhoeus::Pool.with_easy do |e3|
array << e3
end
end
end
expect(array.uniq.size).to eq(3)
end
end
end
@@ -0,0 +1,19 @@
require 'spec_helper'
describe Typhoeus::Request::Actions do
[:get, :post, :put, :delete, :head, :patch, :options].each do |name|
describe ".#{name}" do
let(:response) { Typhoeus::Request.method(name).call("http://localhost:3001") }
it "returns ok" do
expect(response.return_code).to eq(:ok)
end
unless name == :head
it "makes #{name.to_s.upcase} Request" do
expect(response.response_body).to include("\"REQUEST_METHOD\":\"#{name.to_s.upcase}\"")
end
end
end
end
end
@@ -0,0 +1,93 @@
require 'spec_helper'
describe Typhoeus::Request::Before do
let(:request) { Typhoeus::Request.new("") }
let(:receive_counter) { double :mark => :twain }
describe "#queue" do
context "when before" do
context "when one" do
it "executes" do
Typhoeus.before { |r| receive_counter.mark }
expect(receive_counter).to receive(:mark)
request.run
end
context "when true" do
it "calls super" do
Typhoeus.before { true }
expect(Typhoeus::Expectation).to receive(:response_for)
request.run
end
end
context "when false" do
it "doesn't call super" do
Typhoeus.before { false }
expect(Typhoeus::Expectation).to receive(:response_for).never
request.run
end
it "returns response" do
Typhoeus.before { |r| r.response = 1; false }
expect(request.run).to be(1)
end
end
context "when a response" do
it "doesn't call super" do
Typhoeus.before { Typhoeus::Response.new }
expect(Typhoeus::Expectation).to receive(:response_for).never
request.run
end
it "returns response" do
Typhoeus.before { |r| r.response = Typhoeus::Response.new }
expect(request.run).to be_a(Typhoeus::Response)
end
end
end
context "when multi" do
context "when all true" do
before { 3.times { Typhoeus.before { |r| receive_counter.mark } } }
it "calls super" do
expect(Typhoeus::Expectation).to receive(:response_for)
request.run
end
it "executes all" do
expect(receive_counter).to receive(:mark).exactly(3)
request.run
end
end
context "when middle false" do
before do
Typhoeus.before { |r| receive_counter.mark }
Typhoeus.before { |r| receive_counter.mark; nil }
Typhoeus.before { |r| receive_counter.mark }
end
it "doesn't call super" do
expect(Typhoeus::Expectation).to receive(:response_for).never
request.run
end
it "executes only two" do
expect(receive_counter).to receive(:mark).exactly(2).times
request.run
end
end
end
end
context "when no before" do
it "calls super" do
expect(Typhoeus::Expectation).to receive(:response_for)
request.run
end
end
end
end
@@ -0,0 +1,75 @@
require 'spec_helper'
describe Typhoeus::Request::BlockConnection do
let(:base_url) { "localhost:3001" }
let(:request) { Typhoeus::Request.new(base_url, {:method => :get}) }
describe "run" do
context "when blocked" do
before { request.block_connection = true }
it "raises" do
expect{ request.run }.to raise_error(Typhoeus::Errors::NoStub)
end
end
context "when not blocked" do
before { request.block_connection = false }
it "doesn't raise" do
expect{ request.run }.to_not raise_error
end
end
end
describe "#blocked?" do
context "when local block_connection" do
context "when true" do
before { request.block_connection = true }
it "returns true" do
expect(request.blocked?).to be_truthy
end
end
context "when false" do
before { request.block_connection = false }
it "returns false" do
expect(request.blocked?).to be_falsey
end
end
end
context "when global block_connection" do
context "when true" do
before { Typhoeus::Config.block_connection = true }
after { Typhoeus::Config.block_connection = false }
it "returns true" do
expect(request.blocked?).to be_truthy
end
end
context "when false" do
before { Typhoeus::Config.block_connection = false }
it "returns false" do
expect(request.blocked?).to be_falsey
end
end
end
context "when global and local block_connection" do
before do
Typhoeus::Config.block_connection = true
request.block_connection = false
end
after { Typhoeus::Config.block_connection = false }
it "takes local" do
expect(request.blocked?).to be_falsey
end
end
end
end
@@ -0,0 +1,94 @@
require 'spec_helper'
describe Typhoeus::Request::Cacheable do
let(:cache) { MemoryCache.new }
let(:options) { {} }
let(:request) { Typhoeus::Request.new("http://localhost:3001", options) }
let(:response) { Typhoeus::Response.new }
before { Typhoeus::Config.cache = cache }
after { Typhoeus::Config.cache = false }
describe "#response=" do
context "when cache activated" do
context "when request new" do
it "caches response" do
request.response = response
expect(cache.memory[request]).to be
end
it "doesn't set cached on response" do
request.response = response
expect(request.response.cached?).to be_falsey
end
end
context "when request in memory" do
before { cache.memory[request] = response }
it "finishes request" do
expect(request).to receive(:finish).with(response)
request.run
end
it "sets cached to true for response" do
request.run
expect(request.response.cached?).to be_truthy
end
end
end
end
describe "#run" do
context "when cache activated" do
context "when request new" do
it "fetches response" do
expect(request.response).to_not be(response)
end
end
context "when request in memory" do
before { cache.memory[request] = response }
it "finishes request" do
expect(request).to receive(:finish).with(response)
request.run
end
end
context "when cache is specified on a request" do
before { Typhoeus::Config.cache = false }
context "when cache is false" do
let(:options) { { :cache => false } }
it "finishes request" do
expect(request.response).to_not be(response)
request.run
end
end
context "when cache is defined" do
let(:options) { { :cache => cache } }
before { cache.memory[request] = response }
it "finishes request" do
expect(request).to receive(:finish).with(response)
request.run
end
end
end
end
end
describe "#cache_ttl" do
context "when option[:cache_ttl]" do
let(:options) { {:cache_ttl => 1} }
it "returns" do
expect(request.cache_ttl).to be(1)
end
end
end
end
@@ -0,0 +1,91 @@
require 'spec_helper'
describe Typhoeus::Request::Callbacks do
let(:request) { Typhoeus::Request.new("fubar") }
[:on_complete, :on_success, :on_failure, :on_progress].each do |callback|
describe "##{callback}" do
it "responds" do
expect(request).to respond_to(callback)
end
context "when no block given" do
it "returns @#{callback}" do
expect(request.method(callback).call).to eq([])
end
end
context "when block given" do
it "stores" do
request.method(callback).call { p 1 }
expect(request.instance_variable_get("@#{callback}").size).to eq(1)
end
end
context "when multiple blocks given" do
it "stores" do
request.method(callback).call { p 1 }
request.method(callback).call { p 2 }
expect(request.instance_variable_get("@#{callback}").size).to eq(2)
end
end
end
end
describe "#execute_callbacks" do
[:on_complete, :on_success, :on_failure, :on_progress].each do |callback|
context "when #{callback}" do
context "when local callback" do
before do
code = if callback == :on_failure
500
else
200
end
request.response = Typhoeus::Response.new(:mock => true, :response_code => code)
request.method(callback).call {|r| expect(r).to be_a(Typhoeus::Response) }
end
it "executes blocks and passes response" do
request.execute_callbacks
end
it "sets handled_response" do
request.method(callback).call { 1 }
request.execute_callbacks
expect(request.response.handled_response).to be(1)
end
end
context "when global callback" do
before do
request.response = Typhoeus::Response.new
Typhoeus.method(callback).call {|r| expect(r).to be_a(Typhoeus::Response) }
end
it "executes blocks and passes response" do
request.execute_callbacks
end
end
context "when global and local callbacks" do
before do
request.response = Typhoeus::Response.new
Typhoeus.method(callback).call {|r| r.instance_variable_set(:@fu, 1) }
request.method(callback).call {|r| expect(r.instance_variable_get(:@fu)).to eq(1) }
end
it "runs global first" do
request.execute_callbacks
end
end
end
end
context "when local on_complete and gobal on_success" do
it "runs all global callbacks first" do
skip
end
end
end
end
@@ -0,0 +1,60 @@
require 'spec_helper'
describe Typhoeus::Request::Marshal do
let(:base_url) { "localhost:3001" }
let(:request) { Typhoeus::Request.new(base_url) }
describe "#marshal_dump" do
%w(on_complete on_success on_failure on_progress).each do |name|
context "when #{name} handler" do
before { request.instance_variable_set("@#{name}", Proc.new{}) }
it "doesn't include @#{name}" do
expect(request.send(:marshal_dump).map(&:first)).to_not include("@#{name}")
end
it "doesn't raise when dumped" do
expect { Marshal.dump(request) }.to_not raise_error
end
context "when loading" do
let(:loaded) { Marshal.load(Marshal.dump(request)) }
it "includes base_url" do
expect(loaded.base_url).to eq(request.base_url)
end
it "doesn't include #{name}" do
expect(loaded.instance_variables).to_not include("@#{name}")
end
end
end
end
context 'when run through hydra' do
let(:options) { {} }
let(:hydra) { Typhoeus::Hydra.new(options) }
before(:each) do
hydra.queue(request)
hydra.run
end
it "doesn't include @hydra" do
expect(request.send(:marshal_dump).map(&:first)).to_not include("@hydra")
end
context 'when loading' do
let(:loaded) { Marshal.load(Marshal.dump(request)) }
it "includes base_url" do
expect(loaded.base_url).to eq(request.base_url)
end
it "doesn't include #{name}" do
expect(loaded.instance_variables).to_not include("@hydra")
end
end
end
end
end
@@ -0,0 +1,34 @@
require 'spec_helper'
describe Typhoeus::Request::Memoizable do
let(:options) { {} }
let(:request) { Typhoeus::Request.new("fu", options) }
let(:response) { Typhoeus::Response.new }
let(:hydra) { Typhoeus::Hydra.new }
describe "#response=" do
context "when memoization activated" do
before { Typhoeus::Config.memoize = true }
after { Typhoeus::Config.memoize = false }
context "when GET request" do
let(:options) { {:method => :get} }
before { request.hydra = hydra }
it "stores response in memory" do
request.response = response
expect(hydra.memory[request]).to be
end
end
context "when no GET request" do
let(:options) { {:method => :post} }
it "doesn't store response in memory" do
request.response = response
expect(hydra.memory[request]).to be_nil
end
end
end
end
end
@@ -0,0 +1,101 @@
require 'spec_helper'
describe Typhoeus::Request::Operations do
let(:base_url) { "localhost:3001" }
let(:options) { {} }
let(:request) { Typhoeus::Request.new(base_url, options) }
describe "#run" do
let(:easy) { Ethon::Easy.new }
before { expect(Typhoeus::Pool).to receive(:get).and_return(easy) }
it "grabs an easy" do
request.run
end
it "generates settings" do
expect(easy).to receive(:http_request)
request.run
end
it "performs" do
expect(easy).to receive(:perform)
request.run
end
it "sets response" do
request.run
expect(request.response).to be
end
it "releases easy" do
expect(Typhoeus::Pool).to receive(:release)
request.run
end
it "calls on_body" do
on_body_called = false
request.on_body { |body, response| on_body_called = true }
request.run
expect(on_body_called).to be_truthy
expect(request.response.body).to satisfy { |v| v.nil? || v == '' }
end
it "makes response headers available to on_body" do
headers = nil
request.on_body { |body, response| headers = response.headers }
request.run
expect(headers).to be
expect(headers).to eq(request.response.headers)
end
it "calls on_headers and on_body" do
headers = nil
request.on_headers { |response| headers = response.headers }
request.on_body { |body, response| expect(headers).not_to be_nil ; expect(response.headers).to eq(headers) }
request.on_complete { |response| expect(response).not_to be_nil ; expect(response.headers).to eq(headers) ; expect(response.body).to be_empty }
request.run
end
it "calls on_headers and on_complete" do
headers = nil
request.on_headers { |response| headers = response.headers }
request.on_complete { |response| expect(response).not_to be_nil ; expect(response.headers).to eq(headers) ; expect(response.body).not_to be_empty }
request.run
end
it "calls on_complete" do
callback = double(:call)
expect(callback).to receive(:call)
request.instance_variable_set(:@on_complete, [callback])
request.run
end
it "returns a response" do
expect(request.run).to be_a(Typhoeus::Response)
end
end
describe "#finish" do
let(:response) { Typhoeus::Response.new }
it "assigns response" do
request.finish(response)
expect(request.response).to be(response)
end
it "assigns request to response" do
request.finish(response)
expect(request.response.request).to be(request)
end
it "executes callbacks" do
expect(request).to receive(:execute_callbacks)
request.finish(response)
end
it "returns response" do
expect(request.finish(response)).to be(response)
end
end
end
@@ -0,0 +1,13 @@
require 'spec_helper'
describe Typhoeus::Request::Responseable do
let(:request) { Typhoeus::Request.new("base_url", {}) }
let(:response) { Typhoeus::Response.new }
describe "#response=" do
it "stores response" do
request.response = response
expect(request.response).to eq(response)
end
end
end
@@ -0,0 +1,45 @@
require 'spec_helper'
describe Typhoeus::Request::Stubbable do
let(:base_url) { "localhost:3001" }
let(:request) { Typhoeus::Request.new(base_url) }
let(:response) { Typhoeus::Response.new }
before { Typhoeus.stub(base_url).and_return(response) }
describe "#run" do
it "checks expectations" do
request.run
end
context "when expectation found" do
it "calls on_headers callbacks" do
canary = :not_called
request.on_headers do
canary = :called
end
request.run
expect(canary).to eq(:called)
end
it "calls on_body callbacks" do
canary = :not_called
request.on_body do
canary = :called
end
request.run
expect(canary).to eq(:called)
end
it "finishes request" do
expect(request).to receive(:finish)
request.run
end
it "sets mock on response" do
request.run
expect(request.response.mock).to be(true)
end
end
end
end
@@ -0,0 +1,256 @@
require 'spec_helper'
describe Typhoeus::Request do
let(:base_url) { "localhost:3001" }
let(:options) do
{
verbose: true,
headers: { "User-Agent" => "Fubar", "Expect" => "" },
maxredirs: 50
}
end
let(:request) { Typhoeus::Request.new(base_url, options) }
describe ".url" do
context "when no parameters" do
it "returns base_url" do
expect(request.url).to eq(request.base_url)
end
end
context "when parameters" do
let(:options) { {:params => {:a => 1}} }
it "returns full url" do
expect(request.url).to eq("#{request.base_url}?a=1")
end
end
it "pushes an easy back into the pool" do
easy = double.as_null_object
allow(Typhoeus::Pool).to receive(:get).and_return(easy)
expect(Typhoeus::Pool).to receive(:release).with(easy)
request.url
end
end
describe ".new" do
it "stores base_url" do
expect(request.base_url).to eq(base_url)
end
it "stores options" do
expect(request.options).to eq(options)
end
it "stores original options" do
expect(request.original_options).to eq(options)
expect(request.original_options).to_not be(request.options)
end
it "sets defaults" do
expect(request.options[:headers]['User-Agent']).to be
end
end
describe "set_defaults" do
context "when header with user agent" do
let(:options) { {:headers => {'User-Agent' => "Custom"} } }
it "doesn't modify user agent" do
expect(request.options[:headers]['User-Agent']).to eq("Custom")
end
end
context "when header without user agent" do
let(:options) { {:headers => {} } }
it "add user agent" do
agent = request.options[:headers]['User-Agent']
expect(agent).to eq(Typhoeus::USER_AGENT)
end
end
context "when Config.user_agent set" do
before { Typhoeus.configure { |config| config.user_agent = "Default" } }
after { Typhoeus.configure { |config| config.user_agent = nil } }
context "with headers" do
let(:options) { {:headers => { "User-Agent" => "Fubar" } } }
it "uses the request options' user agent" do
expect(request.options[:headers]["User-Agent"]).to eq("Fubar")
end
end
context "without headers" do
let(:options) { {:headers => {} } }
it "uses the global options' user agent" do
expect(request.options[:headers]["User-Agent"]).to eq("Default")
end
end
end
context "when Config.verbose set" do
before { Typhoeus.configure { |config| config.verbose = true} }
after { Typhoeus.configure { |config| config.verbose = false} }
it "respects" do
expect(request.options[:verbose]).to be_truthy
end
end
context "when Config.timeout set" do
before { Typhoeus.configure { |config| config.timeout = 10 } }
after { Typhoeus.configure { |config| config.timeout = nil } }
it "respects configuration" do
expect(request.options[:timeout]).to eq(10)
end
end
context "when Config.connecttimeout set" do
before { Typhoeus.configure { |config| config.connecttimeout = 10 } }
after { Typhoeus.configure { |config| config.connecttimeout = nil } }
it "respects configuration" do
expect(request.options[:connecttimeout]).to eq(10)
end
end
context "when maxredirs" do
context "when not set" do
it "defaults to 50" do
expect(request.options[:maxredirs]).to be(50)
end
end
context "when set" do
let(:options) { {:maxredirs => 1} }
it "respects" do
expect(request.options[:maxredirs]).to be(1)
end
end
end
context "when Config.proxy set" do
before { Typhoeus.configure { |config| config.proxy = "http://proxy.internal" } }
after { Typhoeus.configure { |config| config.proxy = nil } }
it "respects" do
expect(request.options[:proxy]).to eq("http://proxy.internal")
end
context "when option proxy set" do
let(:options) { {:proxy => nil} }
it "does not override" do
expect(request.options[:proxy]).to be_nil
end
end
end
end
describe "#eql?" do
context "when another class" do
let(:other) { "" }
it "returns false" do
expect(request).to_not eql other
end
end
context "when same class" do
let(:other) { Typhoeus::Request.new("base_url", options) }
context "when other base_url" do
it "returns false" do
expect(request).to_not eql other
end
end
context "when same base_url and other options" do
let(:other) { Typhoeus::Request.new(base_url, {}) }
it "returns false" do
expect(request).to_not eql other
end
end
context "when same base_url and options" do
context "when same order" do
let(:other) { Typhoeus::Request.new(base_url, options) }
it "returns true" do
expect(request).to eql other
end
end
context "when different order" do
let(:other_options) {
{:headers => { 'User-Agent' => "Fubar", 'Expect' => ""}, :verbose => true }
}
let(:other) { Typhoeus::Request.new(base_url, other_options)}
it "returns true" do
expect(request).to eql other
end
end
end
end
end
describe "#hash" do
context "when request.eql?(other)" do
context "when different order" do
let(:other_options) {
{:headers => { 'User-Agent' => "Fubar", 'Expect' => "" }, :verbose => true }
}
let(:other) { Typhoeus::Request.new(base_url, other_options)}
it "has same hashes" do
expect(request.hash).to eq(other.hash)
end
end
context "when same order" do
let(:other) { Typhoeus::Request.new(base_url, options) }
it "has same hashes" do
expect(request.hash).to eq(other.hash)
end
end
context "when hashes with different orders are contained in arrays" do
let(:request) { Typhoeus::Request.new(base_url, :params => [{:b => 2, :a => 1}]) }
let(:other) { Typhoeus::Request.new(base_url, :params => [{:a => 1, :b => 2}]) }
it "has different hashes" do
expect(request.hash).to eq(other.hash)
end
end
end
context "when not request.eql?(other)" do
let(:request) { Typhoeus::Request.new(base_url, :params => {:foo => 'bar'}) }
let(:other) { Typhoeus::Request.new(base_url, :params => {:foo => 'baz'}) }
it "has different hashes" do
expect(request.hash).to_not eq(other.hash)
end
end
end
describe "#encoded_body" do
let(:request) {
Typhoeus::Request.new("www.example.com",:body => {:a => 1})
}
it "returns encoded body" do
expect(request.encoded_body).to eq("a=1")
end
end
end
@@ -0,0 +1,147 @@
require 'spec_helper'
describe Typhoeus::Response::Header do
let(:raw) { nil }
let(:header) { Typhoeus::Response::Header.new(raw) }
describe ".new" do
context "when string" do
let(:raw) { 'Date: Fri, 29 Jun 2012 10:09:23 GMT' }
it "sets Date" do
expect(header['Date']).to eq('Fri, 29 Jun 2012 10:09:23 GMT')
end
it "provides case insensitive access" do
expect(header['DaTe']).to eq('Fri, 29 Jun 2012 10:09:23 GMT')
end
it "provides symbol access" do
expect(header[:date]).to eq('Fri, 29 Jun 2012 10:09:23 GMT')
end
end
context "when hash" do
let(:raw) { { 'Date' => 'Fri, 29 Jun 2012 10:09:23 GMT' } }
it "sets Date" do
expect(header['Date']).to eq(raw['Date'])
end
it "provides case insensitive access" do
expect(header['DaTe']).to eq(raw['Date'])
end
end
end
describe "#parse" do
context "when no header" do
it "returns nil" do
expect(header).to be_empty
end
end
context "when header" do
let(:raw) do
'HTTP/1.1 200 OK
Set-Cookie: NID=61=LblqYgUOu; expires=Sat, 29-Dec-2012 10:09:23 GMT; path=/; domain=.google.de; HttpOnly
Date: Fri, 29 Jun 2012 10:09:23 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
Set-Cookie: PREF=ID=77e93yv0hPtejLou; expires=Sun, 29-Jun-2014 10:09:23 GMT; path=/; domain=.google.de
Set-Cookie: NID=61=LblqYgh5Ou; expires=Sat, 29-Dec-2012 10:09:23 GMT; path=/; domain=.google.de; HttpOnly
P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
Server: gws
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Transfer-Encoding: chunked'.gsub(/^\s{8}/, '')
end
it "sets raw" do
expect(header.send(:raw)).to eq(raw)
end
it "sets Set-Cookie" do
expect(header['set-cookie'].size).to eq(3)
end
it "provides case insensitive access" do
expect(header['Set-CooKie'].size).to eq(3)
end
[
'NID=61=LblqYgUOu; expires=Sat, 29-Dec-2012 10:09:23 GMT; path=/; domain=.google.de; HttpOnly',
'PREF=ID=77e93yv0hPtejLou; expires=Sun, 29-Jun-2014 10:09:23 GMT; path=/; domain=.google.de',
'NID=61=LblqYgh5Ou; expires=Sat, 29-Dec-2012 10:09:23 GMT; path=/; domain=.google.de; HttpOnly'
].each_with_index do |cookie, i|
it "sets Cookie##{i}" do
expect(header['set-cookie']).to include(cookie)
end
end
{
'Date' => 'Fri, 29 Jun 2012 10:09:23 GMT', 'Expires' => '-1',
'Cache-Control' => 'private, max-age=0',
'Content-Type' => 'text/html; charset=ISO-8859-1',
'P3P' => 'CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."',
'Server' => 'gws', 'X-XSS-Protection' => '1; mode=block',
'X-Frame-Options' => 'SAMEORIGIN', 'Transfer-Encoding' => 'chunked'
}.each do |name, value|
it "sets #{name}" do
expect(header[name.downcase]).to eq(value)
end
end
context 'includes a multi-line header' do
let(:raw) do
'HTTP/1.1 200 OK
Date: Fri, 29 Jun 2012 10:09:23 GMT
Content-Security-Policy: default-src "self";
img-src * data: "self";
upgrade-insecure-requests;'.gsub(/^\s{10}/, '')
end
it "joins header parts" do
expect(header).to eq({
'Date' => 'Fri, 29 Jun 2012 10:09:23 GMT',
'Content-Security-Policy' => 'default-src "self"; img-src * data: "self"; upgrade-insecure-requests;'
})
end
end
context 'includes line with only whitespace' do
let(:raw) do
'HTTP/1.1 200 OK
Date: Fri, 29 Jun 2012 10:09:23 GMT
'.gsub(/^\s{10}/, '')
end
it 'ignores it' do
expect(header).to eq({ 'Date' => 'Fri, 29 Jun 2012 10:09:23 GMT' })
end
end
context 'with broken headers' do
let(:raw) do
'HTTP/1.1 200 OK
Date:
Content-Type
'.gsub(/^\s{10}/, '')
end
it 'returns empty string for invalid headers' do
expect(header.to_hash).to include({ 'Date' => '', 'Content-Type' => '' })
end
end
end
end
it "can be Marshal'd" do
header = Typhoeus::Response::Header.new("Foo: Bar")
expect {
Marshal.dump(header)
}.not_to raise_error
end
end
@@ -0,0 +1,323 @@
require 'spec_helper'
describe Typhoeus::Response::Informations do
let(:options) { {} }
let(:response) { Typhoeus::Response.new(options) }
describe "#return_code" do
let(:options) { { :return_code => :ok } }
it "returns return_code from options" do
expect(response.return_code).to be(:ok)
end
end
describe "#debug_info" do
let(:options) { { :debug_info => Ethon::Easy::DebugInfo.new } }
it "returns debug_info from options" do
expect(response.debug_info).to be_a(Ethon::Easy::DebugInfo)
end
end
describe "#return_message" do
let(:options) { { :return_code => :couldnt_connect } }
it "returns a message" do
expect(response.return_message).to eq("Couldn't connect to server")
end
describe "with nil return_code" do
let(:options) { { :return_code => nil } }
it "returns nil" do
expect(response.return_message).to be_nil
end
end
end
describe "#response_body" do
context "when response_body" do
let(:options) { { :response_body => "body" } }
it "returns response_body from options" do
expect(response.response_body).to eq("body")
end
end
context "when body" do
let(:options) { { :body => "body" } }
it "returns body from options" do
expect(response.body).to eq("body")
end
end
end
describe "#response_headers" do
let(:options) { { :response_headers => "Length: 1" } }
context "when no mock" do
it "returns response_headers from options" do
expect(response.response_headers).to eq("Length: 1")
end
end
context "when mock" do
context "when no response_headers" do
context "when headers" do
let(:options) { { :mock => true, :headers => {"Length" => 1, "Content-Type" => "text/plain" } } }
it "constructs response_headers" do
expect(response.response_headers).to include("Length: 1")
expect(response.response_headers).to include("Content-Type: text/plain")
expect(response.response_headers).to include("\r\n")
end
end
context "when multiple values for a header" do
let(:options) { { :mock => true, :headers => {"Length" => 1, "Content-Type" => "text/plain", "set-cookie" => ["cookieone=one","cookietwo=two"] } } }
it "constructs response_headers" do
expect(response.response_headers).to include("Length: 1")
expect(response.response_headers).to include("Content-Type: text/plain")
expect(response.response_headers).to include("set-cookie: cookieone=one,cookietwo=two")
expect(response.response_headers).to include("\r\n")
end
end
end
end
end
describe "#response_code" do
context "when response_code" do
let(:options) { { :response_code => "200" } }
it "returns response_code from options" do
expect(response.response_code).to eq(200)
end
end
context "when code" do
let(:options) { { :code => "200" } }
it "returns code from options" do
expect(response.code).to eq(200)
end
end
end
describe "#httpauth_avail" do
let(:options) { { :httpauth_avail => "code" } }
it "returns httpauth_avail from options" do
expect(response.httpauth_avail).to eq("code")
end
end
describe "#total_time" do
let(:options) { { :total_time => 1 } }
it "returns total_time from options" do
expect(response.total_time).to eq(1)
end
end
describe "#starttransfer_time" do
let(:options) { { :starttransfer_time => 1 } }
it "returns starttransfer_time from options" do
expect(response.starttransfer_time).to eq(1)
end
end
describe "#appconnect_time" do
let(:options) { { :appconnect_time => 1 } }
it "returns appconnect_time from options" do
expect(response.appconnect_time).to eq(1)
end
end
describe "#pretransfer_time" do
let(:options) { { :pretransfer_time => 1 } }
it "returns pretransfer_time from options" do
expect(response.pretransfer_time).to eq(1)
end
end
describe "#connect_time" do
let(:options) { { :connect_time => 1 } }
it "returns connect_time from options" do
expect(response.connect_time).to eq(1)
end
end
describe "#namelookup_time" do
let(:options) { { :namelookup_time => 1 } }
it "returns namelookup_time from options" do
expect(response.namelookup_time).to eq(1)
end
end
describe "#redirect_time" do
let(:options) { { :redirect_time => 1 } }
it "returns redirect_time from options" do
expect(response.redirect_time).to eq(1)
end
end
describe "#effective_url" do
let(:options) { { :effective_url => "http://www.example.com" } }
it "returns effective_url from options" do
expect(response.effective_url).to eq("http://www.example.com")
end
end
describe "#primary_ip" do
let(:options) { { :primary_ip => "127.0.0.1" } }
it "returns primary_ip from options" do
expect(response.primary_ip).to eq("127.0.0.1")
end
end
describe "#redirect_count" do
let(:options) { { :redirect_count => 2 } }
it "returns redirect_count from options" do
expect(response.redirect_count).to eq(2)
end
end
describe "#redirect_url" do
let(:options) { { :redirect_url => "http://www.example.com" } }
it "returns redirect_url from options" do
expect(response.redirect_url).to eq("http://www.example.com")
end
end
describe "#request_size" do
let(:options) { { :request_size => 2 } }
it "returns request_size from options" do
expect(response.request_size).to eq(2)
end
end
describe "#size_upload" do
let(:options) { { :size_upload => 2.0 } }
it "returns size_upload from options" do
expect(response.size_upload).to eq(2.0)
end
end
describe "#size_download" do
let(:options) { { :size_download => 2.0 } }
it "returns size_download from options" do
expect(response.size_download).to eq(2.0)
end
end
describe "#speed_upload" do
let(:options) { { :speed_upload => 2.0 } }
it "returns speed_upload from options" do
expect(response.speed_upload).to eq(2.0)
end
end
describe "#speed_download" do
let(:options) { { :speed_download => 2.0 } }
it "returns speed_download from options" do
expect(response.speed_download).to eq(2.0)
end
end
describe "#headers" do
context "when no response_headers" do
it "returns nil" do
expect(response.headers).to be_nil
end
end
context "when response_headers" do
let(:options) { {:response_headers => "Expire: -1\nServer: gws"} }
it "returns nonempty headers" do
expect(response.headers).to_not be_empty
end
it "has Expire" do
expect(response.headers['expire']).to eq('-1')
end
it "has Server" do
expect(response.headers['server']).to eq('gws')
end
end
context "when multiple headers" do
let(:options) { {:response_headers => "Server: A\r\n\r\nServer: B"} }
it "returns the last" do
expect(response.headers['server']).to eq("B")
end
end
context "when mock" do
context "when headers" do
let(:options) { {:mock => true, :headers => {"Length" => "1"}} }
it "returns Typhoeus::Response::Header" do
expect(response.headers).to be_a(Typhoeus::Response::Header)
end
it "returns headers" do
expect(response.headers.to_hash).to include("Length" => "1")
end
end
end
context "when requesting" do
let(:response) { Typhoeus.get("localhost:3001") }
it "returns headers" do
expect(response.headers).to_not be_empty
end
end
end
describe "#redirections" do
context "when no response_headers" do
it "returns empty array" do
expect(response.redirections).to be_empty
end
end
context "when headers" do
let(:options) { {:response_headers => "Expire: -1\nServer: gws"} }
it "returns empty array" do
expect(response.redirections).to be_empty
end
end
context "when multiple headers" do
let(:options) { {:response_headers => "Server: A\r\n\r\nServer: B"} }
it "returns response from all but last headers" do
expect(response.redirections.size).to eq(1)
end
end
end
end
@@ -0,0 +1,256 @@
require 'spec_helper'
describe Typhoeus::Response::Status do
let(:response) { Typhoeus::Response.new(options) }
let(:options) { {} }
describe "timed_out?" do
context "when return code is operation_timedout" do
let(:options) { {:return_code => :operation_timedout} }
it "return true" do
expect(response).to be_timed_out
end
end
end
describe "#status_message" do
context "when no header" do
it "returns nil" do
expect(response.status_message).to be_nil
end
end
context "when header" do
context "when no message" do
let(:options) { {:response_headers => "HTTP/1.1 200\r\n"} }
it "returns nil" do
expect(response.status_message).to be_nil
end
end
context "when messsage" do
let(:options) { {:response_headers => "HTTP/1.1 200 message\r\n"} }
it "returns message" do
expect(response.status_message).to eq("message")
end
end
end
end
describe "#http_version" do
context "when no header" do
it "returns nil" do
expect(response.http_version).to be_nil
end
end
context "when header" do
context "when no http version" do
let(:options) { {:response_headers => "HTTP OK"} }
it "returns nil" do
expect(response.http_version).to be_nil
end
end
context "when invalid http_version" do
let(:options) { {:response_headers => "HTTP foo/bar OK"} }
it "returns nil" do
expect(response.http_version).to be_nil
end
end
context "when valid http_version" do
let(:options) { {:response_headers => "HTTP/1.1 OK"} }
it "returns http_version" do
expect(response.http_version).to eq("1.1")
end
end
end
end
describe "#success?" do
context "when response code 200-299" do
let(:options) { {:return_code => return_code, :response_code => 201} }
context "when mock" do
before { response.mock = true }
context "when return_code :ok" do
let(:return_code) { :ok }
it "returns true" do
expect(response.success?).to be_truthy
end
end
context "when return_code nil" do
let(:return_code) { nil }
it "returns true" do
expect(response.success?).to be_truthy
end
end
end
context "when no mock" do
before { response.mock = nil }
context "when return_code :ok" do
let(:return_code) { :ok }
it "returns true" do
expect(response.success?).to be_truthy
end
end
context "when return_code nil" do
let(:return_code) { nil }
it "returns false" do
expect(response.success?).to be_falsey
end
end
end
end
context "when response code is not 200-299" do
let(:options) { {:return_code => :ok, :response_code => 500} }
it "returns false" do
expect(response.success?).to be_falsey
end
end
end
describe "#failure?" do
context "when response code between 300-526 and 100-300" do
let(:options) { {:return_code => return_code, :response_code => 300} }
context "when mock" do
before { response.mock = true }
context "when return_code :internal_server_error" do
let(:return_code) { :internal_server_error }
it "returns true" do
expect(response.failure?).to be_truthy
end
end
context "when return_code nil" do
let(:return_code) { nil }
it "returns true" do
expect(response.failure?).to be_truthy
end
end
end
context "when no mock" do
before { response.mock = nil }
context "when return_code :internal_server_error" do
let(:return_code) { :internal_server_error }
it "returns true" do
expect(response.failure?).to be_truthy
end
end
context "when return_code nil" do
let(:return_code) { nil }
it "returns false" do
expect(response.failure?).to be_falsey
end
end
end
end
context "when response code is not 300-526" do
let(:options) { {:return_code => :ok, :response_code => 200} }
it "returns false" do
expect(response.failure?).to be_falsey
end
end
end
describe "#modified?" do
context "when response code 304" do
let(:options) { {:return_code => :ok, :response_code => 304} }
context "when mock" do
before { response.mock = true }
context "when return_code :ok" do
let(:return_code) { :ok }
it "returns false" do
expect(response.modified?).to be_falsey
end
end
context "when return_code nil" do
let(:return_code) { nil }
it "returns false" do
expect(response.modified?).to be_falsey
end
end
end
context "when no mock" do
before { response.mock = nil }
context "when return_code :ok" do
let(:return_code) { :ok }
it "returns false" do
expect(response.modified?).to be_falsey
end
end
context "when return_code nil" do
let(:return_code) { nil }
it "returns true" do
expect(response.modified?).to be_falsey
end
end
end
end
context "when response code is not 304" do
let(:options) { {:return_code => :ok, :response_code => 500} }
it "returns true" do
expect(response.modified?).to be_truthy
end
end
end
describe "#first_header_line" do
context "when multiple header" do
let(:options) { {:response_headers => "1\r\n\r\n2\r\nbla"} }
it "returns first line of last block" do
expect(response.method(:first_header_line).call).to eq("2")
end
end
context "when single header" do
let(:options) { {:response_headers => "1"} }
it "returns first line" do
expect(response.method(:first_header_line).call).to eq("1")
end
end
end
end
@@ -0,0 +1,100 @@
require 'spec_helper'
describe Typhoeus::Response do
let(:response) { Typhoeus::Response.new(options) }
let(:options) { {} }
describe ".new" do
context "when options" do
context "when return_code" do
let(:options) { {:return_code => 2} }
it "stores" do
expect(response.options[:return_code]).to be(2)
end
end
context "when headers" do
let(:options) { {:headers => {'A' => 'B'}} }
it "stores unmodified" do
expect(response.options[:headers]).to be(options[:headers])
end
it "sets @headers to a Typhoeus::Response::Header" do
expect(response.instance_variable_get(:@headers)).to be_a(Typhoeus::Response::Header)
end
it "has key" do
expect(response.headers['A']).to eq('B')
end
end
end
end
describe "#mock" do
context "when @mock" do
before { response.mock = true }
it "returns @mock" do
expect(response.mock).to be_truthy
end
end
context "when options[:mock]" do
let(:options) { {:mock => true} }
it "returns options[:mock]" do
expect(response.mock).to be_truthy
end
end
context "when @mock and options[:mock]" do
let(:options) { {:mock => 1} }
before { response.mock = 2 }
it "returns @mock" do
expect(response.mock).to be(2)
end
end
end
describe "#handled_response" do
let(:handled_response) { Typhoeus::Response.new }
context "when @handled_response" do
before { response.handled_response = handled_response }
it "returns @handled_response" do
expect(response.handled_response).to be(handled_response)
end
end
context "when @handled_response is nil" do
before { response.handled_response = nil }
it "returns response" do
expect(response.handled_response).to be(response)
end
end
end
describe "#cached" do
context "when @cached" do
before { response.cached = true }
it "returns cached status" do
expect(response.cached?).to be_truthy
end
end
context "when @cached is nil" do
before { response.cached = nil }
it "returns false" do
expect(response.cached?).to be_falsey
end
end
end
end