Permalink
Browse files

Improved the HTTP spec. Tested all posibilities with blocks. DRYed ou…

…t the Query factory methods.
  • Loading branch information...
1 parent 7de789d commit f3d3988a656114dfdda462cd82505cfb47a6a97f @supermarin supermarin committed Jul 31, 2012
Showing with 32 additions and 21 deletions.
  1. +11 −12 motion/http.rb
  2. +21 −9 spec/motion/http_spec.rb
View
@@ -19,38 +19,37 @@ module HTTP
# end
#
def self.get(url, options={}, &block)
- options[:action] = block if block_given?
- HTTP::Query.new(url, :get, options)
+ create_query(url, :get, options, block)
end
# Make a POST request
def self.post(url, options={}, &block)
- options[:action] = block if block_given?
- HTTP::Query.new(url, :post, options)
+ create_query(url, :post, options, block)
end
# Make a PUT request
def self.put(url, options={}, &block)
- options[:action] = block if block_given?
- HTTP::Query.new(url, :put, options)
+ create_query(url, :put, options, block)
end
# Make a DELETE request
def self.delete(url, options={}, &block)
- options[:action] = block if block_given?
- HTTP::Query.new(url, :delete, options)
+ create_query(url, :delete, options, block)
end
# Make a HEAD request
def self.head(url, options={}, &block)
- options[:action] = block if block_given?
- HTTP::Query.new(url, :head, options)
+ create_query(url, :head, options, block)
end
# Make a PATCH request
def self.patch(url, options={}, &block)
- options[:action] = block if block_given?
- HTTP::Query.new(url, :patch, options)
+ create_query(url, :patch, options, block)
+ end
+
+ def self.create_query(url, method, options, block)
+ options[:action] = block if block
+ HTTP::Query.new(url, method, options)
end
# Response class wrapping the results of a Query's response
@@ -27,13 +27,25 @@ def test_http_method(method)
end
end
- it "uses the block instead of action passed in " do
+ it "uses the block instead of :action if both were given" do
[:get, :post, :put, :delete, :head, :patch].each do |method|
called = false
expected_delegator = Proc.new {|response| called = true }
-
+
query = BubbleWrap::HTTP.send(method, @localhost_url, { action: 'not_valid' }, &expected_delegator)
+ query.connectionDidFinishLoading(query.connection)
+
query.instance_variable_get(:@delegator).should.equal expected_delegator
+ called.should.equal true
+ end
+ end
+
+ it "works with classic blocks as well" do
+ [:get, :post, :put, :delete, :head, :patch].each do |method|
+ called = false
+ query = BubbleWrap::HTTP.send(method, @localhost_url, { action: 'not_valid' } ) do |response|
+ called = true
+ end
query.connectionDidFinishLoading(query.connection)
called.should.equal true
end
@@ -93,7 +105,7 @@ def test_http_method(method)
values: ['apple', 'orange', 'peach'],
credentials: @credentials
}
- @action = lambda{|fa, ke|}
+ @action = Proc.new { |response| @real_response = response; @delegator_was_called = true }
@format = "application/x-www-form-urlencoded"
@cache_policy = 24234
@leftover_option = 'trololo'
@@ -536,22 +548,22 @@ def query_received_data
end
describe "after 30 redirects" do
- before do
- 31.times do
- @query.connection(nil, willSendRequest:@request, redirectResponse:nil)
- end
+ before do
+ 31.times do
+ @query.connection(nil, willSendRequest:@request, redirectResponse:nil)
+ end
end
it "sets the error message on response" do
- @query.response.error_message.should.equal "Too many redirections"
+ @real_response.error_message.should.equal "Too many redirections"
end
it "sets the request.done_loading" do
@query.request.done_loading?.should.equal true
end
it "calls the delegator block" do
- # having troubles with block execution
+ @delegator_was_called.should.equal true
end
end

0 comments on commit f3d3988

Please sign in to comment.