Skip to content

Commit

Permalink
Added support for dynamically evaluated raw responses recorded with `…
Browse files Browse the repository at this point in the history
…curl -is`
  • Loading branch information
bblimke committed Oct 25, 2010
1 parent 784553c commit f0408f8
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 26 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,13 @@
#Changelog #Changelog


## Master

* Support for dynamically evaluated raw responses recorded with `curl -is` <br/>
i.e.

`curl -is www.example.com > /tmp/www.example.com.txt`
stub_request(:get, "www.example.com").to_return(lambda { |request| File.new("/tmp/#{request.uri.host.to_s}.txt" }))

## 1.4.0 ## 1.4.0


* Curb support!!! Thanks to the awesome work of Pete Higgins! * Curb support!!! Thanks to the awesome work of Pete Higgins!
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -207,14 +207,19 @@ You can also use WebMock outside a test framework:
to_return { |request| {:body => request.body} } to_return { |request| {:body => request.body} }


RestClient.post('www.example.net', 'abc') # ===> "abc\n" RestClient.post('www.example.net', 'abc') # ===> "abc\n"

### Responses dynamically evaluated from lambda ### Responses dynamically evaluated from lambda

stub_request(:any, 'www.example.net'). stub_request(:any, 'www.example.net').
to_return(lambda { |request| {:body => request.body} }) to_return(lambda { |request| {:body => request.body} })


RestClient.post('www.example.net', 'abc') # ===> "abc\n" RestClient.post('www.example.net', 'abc') # ===> "abc\n"


### Dynamically evaluated raw responses recorded with `curl -is`

`curl -is www.example.com > /tmp/www.example.com.txt`
stub_request(:get, "www.example.com").to_return(lambda { |request| File.new("/tmp/#{request.uri.host.to_s}.txt" }))

### Responses with dynamically evaluated parts ### Responses with dynamically evaluated parts


stub_request(:any, 'www.example.net'). stub_request(:any, 'www.example.net').
Expand Down
3 changes: 1 addition & 2 deletions lib/webmock/request_registry.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ def request_stub_for(request_signature)
end end


def evaluate_response_for_request(response, request_signature) def evaluate_response_for_request(response, request_signature)
evaluated_response = response.dup response.evaluate(request_signature)
evaluated_response.evaluate!(request_signature)
end end


end end
Expand Down
12 changes: 4 additions & 8 deletions lib/webmock/response.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def options=(options)
@should_timeout = options[:should_timeout] @should_timeout = options[:should_timeout]
end end


def evaluate!(request_signature) def evaluate(request_signature)
self.body = @body.call(request_signature) if @body.is_a?(Proc) self.body = @body.call(request_signature) if @body.is_a?(Proc)
self.headers = @headers.call(request_signature) if @headers.is_a?(Proc) self.headers = @headers.call(request_signature) if @headers.is_a?(Proc)
self.status = @status.call(request_signature) if @status.is_a?(Proc) self.status = @status.call(request_signature) if @status.is_a?(Proc)
Expand Down Expand Up @@ -136,13 +136,9 @@ def initialize(responder)
@responder = responder @responder = responder
end end


def dup def evaluate(request_signature)
self.class.new(@responder) options = @responder.call(request_signature)
end Response.new(options)

def evaluate!(request_signature)
self.options = @responder.call(request_signature)
self
end end
end end
end end
50 changes: 36 additions & 14 deletions spec/response_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@
@response.raise_error_if_any @response.raise_error_if_any
}.should raise_error(ArgumentError, "Exception from WebMock") }.should raise_error(ArgumentError, "Exception from WebMock")
end end

it "should raise error if any assigned as instance" do it "should raise error if any assigned as instance" do
@response = WebMock::Response.new(:exception => ArgumentError.new("hello world")) @response = WebMock::Response.new(:exception => ArgumentError.new("hello world"))
lambda { lambda {
@response.raise_error_if_any @response.raise_error_if_any
}.should raise_error(ArgumentError, "hello world") }.should raise_error(ArgumentError, "hello world")
end end

it "should raise error if any assigned as string" do it "should raise error if any assigned as string" do
@response = WebMock::Response.new(:exception => "hello world") @response = WebMock::Response.new(:exception => "hello world")
lambda { lambda {
Expand All @@ -78,7 +78,7 @@
end end


end end

describe "timeout" do describe "timeout" do


it "should know if it should timeout" do it "should know if it should timeout" do
Expand Down Expand Up @@ -170,7 +170,7 @@
"Date"=>"Sat, 23 Jan 2010 01:01:05 GMT", "Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
"Content-Type"=>"text/html; charset=UTF-8", "Content-Type"=>"text/html; charset=UTF-8",
"Content-Length"=>"438", "Content-Length"=>"438",
"Connection"=>"Keep-Alive", "Connection"=>"Keep-Alive",
"Accept"=>"image/jpeg, image/png" "Accept"=>"image/jpeg, image/png"
} }
end end
Expand All @@ -195,17 +195,17 @@


it "should have evaluated body" do it "should have evaluated body" do
@response = WebMock::Response.new(:body => lambda {|request| request.body}) @response = WebMock::Response.new(:body => lambda {|request| request.body})
@response.evaluate!(@request_signature).body.should == "abc" @response.evaluate(@request_signature).body.should == "abc"
end end


it "should have evaluated headers" do it "should have evaluated headers" do
@response = WebMock::Response.new(:headers => lambda {|request| request.headers}) @response = WebMock::Response.new(:headers => lambda {|request| request.headers})
@response.evaluate!(@request_signature).headers.should == {'A' => 'a'} @response.evaluate(@request_signature).headers.should == {'A' => 'a'}
end end


it "should have evaluated status" do it "should have evaluated status" do
@response = WebMock::Response.new(:status => lambda {|request| 302}) @response = WebMock::Response.new(:status => lambda {|request| 302})
@response.evaluate!(@request_signature).status.should == [302, ""] @response.evaluate(@request_signature).status.should == [302, ""]
end end


end end
Expand All @@ -216,7 +216,7 @@


describe "evaluating response options" do describe "evaluating response options" do


it "should have evaluated options" do it "should evaluate new response with evaluated options" do
request_signature = WebMock::RequestSignature.new(:post, "www.example.com", :body => "abc", :headers => {'A' => 'a'}) request_signature = WebMock::RequestSignature.new(:post, "www.example.com", :body => "abc", :headers => {'A' => 'a'})
response = WebMock::DynamicResponse.new(lambda {|request| response = WebMock::DynamicResponse.new(lambda {|request|
{ {
Expand All @@ -225,19 +225,41 @@
:status => 302 :status => 302
} }
}) })
response.evaluate!(request_signature) evaluated_response = response.evaluate(request_signature)
response.body.should == "abc" evaluated_response.body.should == "abc"
response.headers.should == {'A' => 'a'} evaluated_response.headers.should == {'A' => 'a'}
response.status.should == [302, ""] evaluated_response.status.should == [302, ""]
end end


it "should be equal to static response after evaluation" do it "should be equal to static response after evaluation" do
request_signature = WebMock::RequestSignature.new(:post, "www.example.com", :body => "abc") request_signature = WebMock::RequestSignature.new(:post, "www.example.com", :body => "abc")
response = WebMock::DynamicResponse.new(lambda {|request| {:body => request.body}}) response = WebMock::DynamicResponse.new(lambda {|request| {:body => request.body}})
response.evaluate!(request_signature) evaluated_response = response.evaluate(request_signature)
response.should == WebMock::Response.new(:body => "abc") evaluated_response.should == WebMock::Response.new(:body => "abc")
end end


describe "when raw response is evaluated" do
before(:each) do
@files = {
"www.example.com" => File.new(File.expand_path(File.dirname(__FILE__)) + "/example_curl_output.txt")
}
@request_signature = WebMock::RequestSignature.new(:get, "www.example.com")
end

describe "as a file" do
it "should return response" do
response = WebMock::DynamicResponse.new(lambda {|request| @files[request.uri.host.to_s] })
response.evaluate(@request_signature).body.size.should == 438
end
end

describe "as a string" do
it "should return response" do
response = WebMock::DynamicResponse.new(lambda {|request| @files[request.uri.host.to_s].read })
response.evaluate(@request_signature).body.size.should == 438
end
end
end
end end


end end
Expand Down
19 changes: 19 additions & 0 deletions spec/webmock_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -691,6 +691,25 @@ def call(request)
end end
end end


describe "replying raw responses evaluated dynamically" do
before(:each) do
@files = {
"www.example.com" => File.new(File.expand_path(File.dirname(__FILE__)) + "/example_curl_output.txt")
}
end

it "should return response from evaluated file" do
stub_http_request(:get, "www.example.com").to_return(lambda {|request| @files[request.uri.host.to_s] })
http_request(:get, "http://www.example.com/").body.size.should == 438
end

it "should return response from evaluated string" do
stub_http_request(:get, "www.example.com").to_return(lambda {|request| @files[request.uri.host.to_s].read })
http_request(:get, "http://www.example.com/").body.size.should == 438
end

end

describe "sequences of responses" do describe "sequences of responses" do


it "should return responses one by one if declared in array" do it "should return responses one by one if declared in array" do
Expand Down

0 comments on commit f0408f8

Please sign in to comment.