This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user