spec/leadlight/request_spec.rb in leadlight-0.0.3 vs spec/leadlight/request_spec.rb in leadlight-0.0.4
- old
+ new
@@ -92,30 +92,52 @@
its(:http_method) { should eq(:post) }
end
describe "#submit" do
+ let(:request_params) { stub }
+ let(:faraday_request) {
+ stub(options: {}, headers: {}, params: request_params)
+ }
+
+ before do
+ connection.stub(:run_request).
+ and_yield(faraday_request).
+ and_return(stub.as_null_object)
+ end
+
it "starts a request runnning" do
connection.should_receive(:run_request).
with(http_method, url, anything, {}).
and_return(faraday_response)
subject.submit
end
it "triggers the on_prepare_request hook in the block passed to #run_request" do
- yielded = :nothing
- faraday_request = stub(options: {}, headers: {})
- connection.stub(:run_request).
- and_yield(faraday_request).
- and_return(stub.as_null_object)
+ yielded = :nothing
subject.on_prepare_request do |request|
yielded = request
end
subject.submit
yielded.should equal(faraday_request)
end
+ context "with request params" do
+ let(:params) {{ query: 'value' }}
+
+ it "passes params to request" do
+ request_params.should_receive(:update).with(params)
+ subject.submit
+ end
+ end
+
+ context "with no request params" do
+ it "doesn't alter request params" do
+ request_params.should_not_receive(:update)
+ subject.submit
+ end
+ end
end
shared_examples_for "synchronous methods" do
it "returns once the request has completed" do
timeout(1) do
@@ -226,9 +248,44 @@
submit_and_complete
yielded.should equal(faraday_response)
end
end
+ describe "#on_error" do
+ def submit_and_complete
+ t = Thread.new do
+ subject.submit
+ subject.wait
+ end
+ run_completion_handlers
+ t.join(1)
+ subject
+ end
+
+ it "yields to the block when response is an error" do
+ called = :not_set
+ block = proc{ called = true }
+ faraday_response.should_receive(:success?).and_return(false)
+ submit_and_complete.on_error(&block)
+ called.should be_true
+ end
+
+ it "does not yield to the block when response is sucess" do
+ called = :not_set
+ block = proc { called = true }
+ faraday_response.should_receive(:success?).and_return(true)
+ submit_and_complete.on_error(&block)
+ called.should eq(:not_set)
+ end
+
+ it "passes the representation to the handler" do
+ passed = :not_set
+ block = ->(arg) { passed = arg }
+ faraday_response.should_receive(:success?).and_return(false)
+ submit_and_complete.on_error(&block)
+ passed.should equal(representation)
+ end
+ end
describe "#raise_on_error" do
def submit_and_complete
t = Thread.new do
subject.submit