Permalink
Browse files

Added support for dynamically evaluated raw responses recorded with `…

…curl -is`
  • Loading branch information...
bblimke committed Oct 25, 2010
1 parent 784553c commit f0408f859468c24001a9c2ada471e2fe7bb4b669
Showing with 75 additions and 26 deletions.
  1. +8 −0 CHANGELOG.md
  2. +7 −2 README.md
  3. +1 −2 lib/webmock/request_registry.rb
  4. +4 −8 lib/webmock/response.rb
  5. +36 −14 spec/response_spec.rb
  6. +19 −0 spec/webmock_spec.rb
View
@@ -1,5 +1,13 @@
#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
* Curb support!!! Thanks to the awesome work of Pete Higgins!
View
@@ -207,14 +207,19 @@ You can also use WebMock outside a test framework:
to_return { |request| {:body => request.body} }
RestClient.post('www.example.net', 'abc') # ===> "abc\n"
-
+
### Responses dynamically evaluated from lambda
-
+
stub_request(:any, 'www.example.net').
to_return(lambda { |request| {:body => request.body} })
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
stub_request(:any, 'www.example.net').
@@ -43,8 +43,7 @@ def request_stub_for(request_signature)
end
def evaluate_response_for_request(response, request_signature)
- evaluated_response = response.dup
- evaluated_response.evaluate!(request_signature)
+ response.evaluate(request_signature)
end
end
View
@@ -80,7 +80,7 @@ def options=(options)
@should_timeout = options[:should_timeout]
end
- def evaluate!(request_signature)
+ def evaluate(request_signature)
self.body = @body.call(request_signature) if @body.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)
@@ -136,13 +136,9 @@ def initialize(responder)
@responder = responder
end
- def dup
- self.class.new(@responder)
- end
-
- def evaluate!(request_signature)
- self.options = @responder.call(request_signature)
- self
+ def evaluate(request_signature)
+ options = @responder.call(request_signature)
+ Response.new(options)
end
end
end
View
@@ -58,14 +58,14 @@
@response.raise_error_if_any
}.should raise_error(ArgumentError, "Exception from WebMock")
end
-
+
it "should raise error if any assigned as instance" do
@response = WebMock::Response.new(:exception => ArgumentError.new("hello world"))
lambda {
@response.raise_error_if_any
}.should raise_error(ArgumentError, "hello world")
end
-
+
it "should raise error if any assigned as string" do
@response = WebMock::Response.new(:exception => "hello world")
lambda {
@@ -78,7 +78,7 @@
end
end
-
+
describe "timeout" do
it "should know if it should timeout" do
@@ -170,7 +170,7 @@
"Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
"Content-Type"=>"text/html; charset=UTF-8",
"Content-Length"=>"438",
- "Connection"=>"Keep-Alive",
+ "Connection"=>"Keep-Alive",
"Accept"=>"image/jpeg, image/png"
}
end
@@ -195,17 +195,17 @@
it "should have evaluated body" do
@response = WebMock::Response.new(:body => lambda {|request| request.body})
- @response.evaluate!(@request_signature).body.should == "abc"
+ @response.evaluate(@request_signature).body.should == "abc"
end
it "should have evaluated headers" do
@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
it "should have evaluated status" do
@response = WebMock::Response.new(:status => lambda {|request| 302})
- @response.evaluate!(@request_signature).status.should == [302, ""]
+ @response.evaluate(@request_signature).status.should == [302, ""]
end
end
@@ -216,7 +216,7 @@
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'})
response = WebMock::DynamicResponse.new(lambda {|request|
{
@@ -225,19 +225,41 @@
:status => 302
}
})
- response.evaluate!(request_signature)
- response.body.should == "abc"
- response.headers.should == {'A' => 'a'}
- response.status.should == [302, ""]
+ evaluated_response = response.evaluate(request_signature)
+ evaluated_response.body.should == "abc"
+ evaluated_response.headers.should == {'A' => 'a'}
+ evaluated_response.status.should == [302, ""]
end
it "should be equal to static response after evaluation" do
request_signature = WebMock::RequestSignature.new(:post, "www.example.com", :body => "abc")
response = WebMock::DynamicResponse.new(lambda {|request| {:body => request.body}})
- response.evaluate!(request_signature)
- response.should == WebMock::Response.new(:body => "abc")
+ evaluated_response = response.evaluate(request_signature)
+ evaluated_response.should == WebMock::Response.new(:body => "abc")
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
View
@@ -691,6 +691,25 @@ def call(request)
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
it "should return responses one by one if declared in array" do

0 comments on commit f0408f8

Please sign in to comment.