Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added unit specs for dynamic responses behaviour

- Also fixed registry spec
- Removed some unused code
  • Loading branch information...
commit cdb7664fc6f7ff71a243900ab0525c0eb81f6611 1 parent 0f3bb21
Bartosz Blimke authored
View
3  lib/webmock/request_registry.rb
@@ -44,8 +44,7 @@ def request_stub_for(request_signature)
def evaluate_response_for_request(response, request_signature)
evaluated_response = response.dup
- evaluated_response.evaluate(request_signature)
- evaluated_response
+ evaluated_response.evaluate!(request_signature)
end
end
View
2  lib/webmock/request_stub.rb
@@ -1,7 +1,7 @@
module WebMock
class RequestStub
- attr_accessor :request_profile, :responses
+ attr_accessor :request_profile
def initialize(method, uri)
@request_profile = RequestProfile.new(method, uri)
View
16 lib/webmock/response.rb
@@ -24,7 +24,9 @@ def initialize(options = {})
else
self.options = options
end
- @options[:headers] = Util::Headers.normalize_headers(@options[:headers]) unless @options[:headers].is_a?(Proc)
+ if @options.has_key?(:headers) && !@options[:headers].is_a?(Proc)
+ @options[:headers] = Util::Headers.normalize_headers(@options[:headers])
+ end
end
def headers
@@ -56,12 +58,13 @@ def dup
dup_response
end
- def evaluate(request_signature)
- [:body, :headers].each do |attribute|
+ def evaluate!(request_signature)
+ [:body, :headers, :status].each do |attribute|
if options[attribute].is_a?(Proc)
options[attribute] = options[attribute].call(request_signature)
end
end
+ self
end
def ==(other)
@@ -111,8 +114,13 @@ def dup
self.class.new(@responder)
end
- def evaluate(request_signature)
+ def evaluate!(request_signature)
self.options = @responder.call(request_signature)
+ self
+ end
+
+ def ==(other)
+ options == other.options
end
end
end
View
29 spec/request_registry_spec.rb
@@ -49,10 +49,25 @@
describe "response for request" do
- it "should registered response for request profile" do
- @request_stub.instance_variable_set(:@responses, [@response = Response.new])
+ it "should report registered evaluated response for request profile" do
+ @request_stub.to_return(:body => "abc")
RequestRegistry.instance.register_request_stub(@request_stub)
- RequestRegistry.instance.response_for_request(@request_signature).should == @response
+ RequestRegistry.instance.response_for_request(@request_signature).should == Response.new(:body => "abc")
+ end
+
+ it "should report evaluated response" do
+ @request_stub.to_return {|request| {:body => request.method.to_s} }
+ RequestRegistry.instance.register_request_stub(@request_stub)
+ response1 = RequestRegistry.instance.response_for_request(@request_signature)
+ response1.should == Response.new(:body => "get")
+ end
+
+ it "should report clone of theresponse" do
+ @request_stub.to_return {|request| {:body => request.method.to_s} }
+ RequestRegistry.instance.register_request_stub(@request_stub)
+ response1 = RequestRegistry.instance.response_for_request(@request_signature)
+ response2 = RequestRegistry.instance.response_for_request(@request_signature)
+ response1.should_not be(response2)
end
it "should report nothing if no response for request is registered" do
@@ -61,15 +76,15 @@
it "should always return last registered matching response" do
@request_stub1 = RequestStub.new(:get, "www.example.com")
- @request_stub1.instance_variable_set(:@responses, [@response1 = Response.new])
+ @request_stub1.to_return(:body => "abc")
@request_stub2 = RequestStub.new(:get, "www.example.com")
- @request_stub2.instance_variable_set(:@responses, [@response2 = Response.new])
+ @request_stub2.to_return(:body => "def")
@request_stub3 = RequestStub.new(:get, "www.example.org")
- @request_stub3.instance_variable_set(:@responses, [@response3 = Response.new])
+ @request_stub3.to_return(:body => "ghj")
RequestRegistry.instance.register_request_stub(@request_stub1)
RequestRegistry.instance.register_request_stub(@request_stub2)
RequestRegistry.instance.register_request_stub(@request_stub3)
- RequestRegistry.instance.response_for_request(@request_signature).should == @response2
+ RequestRegistry.instance.response_for_request(@request_signature).should == Response.new(:body => "def")
end
end
View
74 spec/response_spec.rb
@@ -1,5 +1,26 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
+describe ResponseFactory do
+
+ describe "response_for" do
+
+ it "should create response with options passed as arguments" do
+ options = {:body => "abc", :headers => {:a => :b}}
+ Response.should_receive(:new).with(options).and_return(@response = mock(Response))
+ ResponseFactory.response_for(options).should == @response
+ end
+
+
+ it "should create dynamic response for argument responding to call" do
+ callable = mock(:call => {:body => "abc"})
+ DynamicResponse.should_receive(:new).with(callable).and_return(@response = mock(Response))
+ ResponseFactory.response_for(callable).should == @response
+ end
+
+ end
+
+end
+
describe Response do
before(:each) do
@response = Response.new(:headers => {'A' => 'a'})
@@ -131,6 +152,59 @@
end
+ describe "with dynamically evaluated options" do
+
+ before(:each) do
+ @request_signature = RequestSignature.new(:post, "www.example.com", :body => "abc", :headers => {'A' => 'a'})
+ end
+
+ it "should have evaluated body" do
+ @response = Response.new(:body => lambda {|request| request.body})
+ @response.evaluate!(@request_signature).body.should == "abc"
+ end
+
+ it "should have evaluated headers" do
+ @response = Response.new(:headers => lambda {|request| request.headers})
+ @response.evaluate!(@request_signature).headers.should == {'A' => 'a'}
+ end
+
+ it "should have evaluated status" do
+ @response = Response.new(:status => lambda {|request| 302})
+ @response.evaluate!(@request_signature).status.should == 302
+ end
+
+ end
+
+ end
+
+ describe DynamicResponse do
+
+ describe "evaluating response options" do
+
+ it "should have evaluated options" do
+ request_signature = RequestSignature.new(:post, "www.example.com", :body => "abc", :headers => {'A' => 'a'})
+ response = DynamicResponse.new(lambda {|request|
+ {
+ :body => request.body,
+ :headers => request.headers,
+ :status => 302
+ }
+ })
+ response.evaluate!(request_signature)
+ response.body.should == "abc"
+ response.headers.should == {'A' => 'a'}
+ response.status.should == 302
+ end
+
+ it "should be equal to static response after evaluation" do
+ request_signature = RequestSignature.new(:post, "www.example.com", :body => "abc")
+ response = DynamicResponse.new(lambda {|request| {:body => request.body}})
+ response.evaluate!(request_signature)
+ response.should == Response.new(:body => "abc")
+ end
+
+ end
+
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.