Permalink
Browse files

Refactored parts of dynamic responses code

  • Loading branch information...
1 parent b4adc85 commit 281f253f275c963d783857e56a2469f2252dd6e3 Bartosz Blimke committed Mar 13, 2010
Showing with 16 additions and 32 deletions.
  1. +5 −3 lib/webmock/request_stub.rb
  2. +11 −29 lib/webmock/response.rb
@@ -16,15 +16,17 @@ def with(params = {}, &block)
def to_return(*response_hashes, &block)
if block
- @responses_sequences << ResponsesSequence.new([WebMock::Response.for(block)])
+ @responses_sequences << ResponsesSequence.new([ResponseFactory.response_for(block)])
else
- @responses_sequences << ResponsesSequence.new([*response_hashes].flatten.map {|r| WebMock::Response.for(r)})
+ @responses_sequences << ResponsesSequence.new([*response_hashes].flatten.map {|r| ResponseFactory.response_for(r)})
end
self
end
def to_raise(*exceptions)
- @responses_sequences << ResponsesSequence.new([*exceptions].flatten.map {|e| WebMock::Response.new(:exception => e)})
+ @responses_sequences << ResponsesSequence.new([*exceptions].flatten.map {|e|
+ ResponseFactory.response_for(:exception => e)
+ })
self
end
View
@@ -5,22 +5,24 @@ class StringIO
module WebMock
- class Response
- def self.for(options)
+ class ResponseFactory
+ def self.response_for(options)
if options.respond_to?(:call)
WebMock::DynamicResponse.new(options)
else
WebMock::Response.new(options)
end
end
+ end
+ class Response
attr_reader :options
def initialize(options = {})
if options.is_a?(IO) || options.is_a?(String)
- self.options = read_raw_response(options)
- else
- self.options = options
+ self.options = read_raw_response(options)
+ else
+ self.options = options
end
@options[:headers] = Util::Headers.normalize_headers(@options[:headers]) unless @options[:headers].is_a?(Proc)
end
@@ -98,39 +100,19 @@ def read_raw_response(raw_response)
end
- class DynamicResponse
+ class DynamicResponse < Response
attr_accessor :responder
def initialize(responder)
@responder = responder
end
- def headers
- @headers
- end
-
- def body
- @body || ''
+ def dup
+ self.class.new(@responder)
end
def evaluate(request_signature)
- response = responder.call(request_signature)
- @headers = response[:headers]
- @body = response[:body]
- @status = response[:status]
- @exception = response[:exception]
- end
-
- def raise_error_if_any
- raise @exception.new('Exception from WebMock') if @exception
- end
-
- def status
- @status || 200
- end
-
- def ==(other)
- other.is_a?(self.class) && other.responder == self.responder
+ self.options = @responder.call(request_signature)
end
end
end

0 comments on commit 281f253

Please sign in to comment.