Permalink
Browse files

Added support for PUT and DELETE. Refactored the crap out of proxy me…

…thods
  • Loading branch information...
1 parent 896f8f1 commit 96f77f16d118aaa5ab953f395d6c377d39841e60 @gus gus committed Jul 10, 2010
@@ -1,35 +0,0 @@
-require 'open-uri'
-require 'httparty'
-
-module Riot
- module Gear
- module Http
-
- def get(*args)
- hookup { @smoke_response = topic.get(*args) }
- end
-
- def post(*args)
- hookup { @smoke_response = topic.post(*args) }
- end
-
- def put(*args)
- hookup { @smoke_response = topic.put(*args) }
- end
-
- def delete(*args)
- hookup { @smoke_response = topic.delete(*args) }
- end
-
- def persist_cookie(cookie_name)
- hookup do
- if cookie_value = cookie_values[cookie_name]
- topic.cookies({cookie_name => cookie_value["value"]})
- end
- end
- end
- end # Http
- end # Gear
-end # Riot
-
-Riot::Context.instance_eval { include Riot::Gear::Http }
@@ -0,0 +1,20 @@
+require 'open-uri'
+require 'httparty'
+
+module Riot
+ module Gear
+ module PersistCookie
+
+ def persist_cookie(cookie_name)
+ hookup do
+ if cookie_value = cookie_values[cookie_name]
+ topic.cookies({cookie_name => cookie_value["value"]})
+ end
+ end
+ end # persist_cookie
+
+ end # PersistCookie
+ end # Gear
+end # Riot
+
+Riot::Context.instance_eval { include Riot::Gear::PersistCookie }
@@ -8,6 +8,7 @@ class RiotPartyMiddleware < ::Riot::ContextMiddleware
def call(context)
setup_faux_class(context)
setup_helpers(context)
+ proxy_action_methods(context)
proxy_httparty_hookups(context)
middleware.call(context)
end
@@ -27,26 +28,40 @@ def setup_faux_class(context)
context.helper(:response) { @smoke_response }
end # setup_faux_class
- def action_methods
- [:get, :post, :put, :delete, :head, :options]
+ #
+ # Method proxying. This is the meat of the DSL.
+
+ def actionable_methods; [:get, :post, :put, :delete, :head]; end
+
+ def proxy_action_methods(context)
+ proxy_class_methods_to_context(context, actionable_methods) do |situation, result|
+ situation.instance_eval { @smoke_response = result }
+ end
end
- def proxy_methods
+ def proxiable_methods
methods = HTTParty::ClassMethods.instance_methods.map { |m| m.to_s.to_sym }
- methods - action_methods - [:default_options]
+ methods - actionable_methods - [:options, :default_options]
end
- # Basically, we're just passing standard HTTParty setup methods onto situation via hookups. Except
- # for the important action methods.
def proxy_httparty_hookups(context)
- proxy_methods.each do |httparty_method|
- (class << context; self; end).__send__(:define_method, httparty_method) do |*args|
+ proxy_class_methods_to_context(context, proxiable_methods)
+ end
+
+ # Basically, we're just passing standard HTTParty setup methods onto situation via hookups
+ def proxy_class_methods_to_context(context, methods, &proxy_block)
+ methods.each do |method_name|
+ (class << context; self; end).__send__(:define_method, method_name) do |*args|
hookup do
- topic.__send__(httparty_method, *args)
+ result = topic.__send__(method_name, *args)
+ yield(self, result) if proxy_block
end
- end
- end # proxy_methods.each
- end # proxy_httparty_hookups
+ end # class << context
+ end # methods.each
+ end # proxy_class_methods_to_context
+
+ #
+ # Helpful helpers
def setup_helpers(context)
helper_json_path(context)
View
@@ -5,11 +5,11 @@
Gem::Specification.new do |s|
s.name = %q{riot-gear}
- s.version = "0.0.5"
+ s.version = "0.0.6"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Justin 'Gus' Knowlden"]
- s.date = %q{2010-07-08}
+ s.date = %q{2010-07-09}
s.description = %q{Riot + HTTParty smoke testing framework. You'd use it for integration testing with real HTTP requests and responses}
s.email = %q{gus@gusg.us}
s.extra_rdoc_files = [
@@ -25,11 +25,14 @@ Gem::Specification.new do |s|
"lib/riot/gear/context/asserts_header.rb",
"lib/riot/gear/context/asserts_json.rb",
"lib/riot/gear/context/asserts_status.rb",
- "lib/riot/gear/context/http.rb",
+ "lib/riot/gear/context/persist_cookie.rb",
"lib/riot/gear/middleware.rb",
"lib/riot/gear/middleware/riotparty.rb",
"riot-gear.gemspec",
+ "test/actions/delete_test.rb",
"test/actions/get_test.rb",
+ "test/actions/post_test.rb",
+ "test/actions/put_test.rb",
"test/helpers/cookie_values_test.rb",
"test/helpers/json_path_test.rb",
"test/riotparty_proxy_methods_test.rb",
@@ -42,7 +45,10 @@ Gem::Specification.new do |s|
s.rubygems_version = %q{1.3.7}
s.summary = %q{Riot + HTTParty smoke testing framework}
s.test_files = [
- "test/actions/get_test.rb",
+ "test/actions/delete_test.rb",
+ "test/actions/get_test.rb",
+ "test/actions/post_test.rb",
+ "test/actions/put_test.rb",
"test/helpers/cookie_values_test.rb",
"test/helpers/json_path_test.rb",
"test/riotparty_proxy_methods_test.rb",
@@ -0,0 +1,48 @@
+require 'teststrap'
+
+context "Sending a DELETE request" do
+ teardown { reset_webmock }
+
+ asserts("not defining a base_uri") do
+ Riot::Context.new("foo") { delete "/foo" }.run(Riot::SilentReporter.new)
+ end.raises(HTTParty::UnsupportedURIScheme)
+
+ context "with a base-uri" do
+ context "without params" do
+ hookup do
+ stub_request(:delete, 'http://test.local/foo').with(:body => {"foo" => "bar"}.to_json).
+ to_return(:body => "Foo", :status => 200)
+ end
+
+ base_uri "http://test.local"
+ delete "/foo", :body => {"foo" => "bar"}.to_json
+
+ asserts("status code") { response.code }.equals(200)
+ asserts("response body") { response.body }.equals("Foo")
+ end # without params
+
+ context "with params" do
+ hookup do
+ stub_request(:delete, 'http://test.local/foo?bar=baz').
+ with(:body => {"goo" => "car"}.to_json).
+ to_return(:body => "", :status => 203)
+ end
+
+ context "through default_params" do
+ base_uri "http://test.local"
+ default_params({"bar" => "baz"})
+ delete "/foo", :body => {"goo" => "car"}.to_json
+
+ asserts("status code") { response.code }.equals(203)
+ end # through default_params
+
+ context "through query string" do
+ base_uri "http://test.local"
+ delete "/foo?bar=baz", :body => {"goo" => "car"}.to_json
+
+ asserts("status code") { response.code }.equals(203)
+ end # through query string
+ end # with params
+ end # with a base-uri
+
+end # Sending a DELETE request
View
@@ -0,0 +1,48 @@
+require 'teststrap'
+
+context "Sending a PUT request" do
+ teardown { reset_webmock }
+
+ asserts("not defining a base_uri") do
+ Riot::Context.new("foo") { put "/foo" }.run(Riot::SilentReporter.new)
+ end.raises(HTTParty::UnsupportedURIScheme)
+
+ context "with a base-uri" do
+ context "without params" do
+ hookup do
+ stub_request(:put, 'http://test.local/foo').with(:body => {"foo" => "bar"}.to_json).
+ to_return(:body => "Foo", :status => 200)
+ end
+
+ base_uri "http://test.local"
+ put "/foo", :body => {"foo" => "bar"}.to_json
+
+ asserts("status code") { response.code }.equals(200)
+ asserts("response body") { response.body }.equals("Foo")
+ end # without params
+
+ context "with params" do
+ hookup do
+ stub_request(:put, 'http://test.local/foo?bar=baz').
+ with(:body => {"goo" => "car"}.to_json).
+ to_return(:body => "", :status => 203)
+ end
+
+ context "through default_params" do
+ base_uri "http://test.local"
+ default_params({"bar" => "baz"})
+ put "/foo", :body => {"goo" => "car"}.to_json
+
+ asserts("status code") { response.code }.equals(203)
+ end # through default_params
+
+ context "through query string" do
+ base_uri "http://test.local"
+ put "/foo?bar=baz", :body => {"goo" => "car"}.to_json
+
+ asserts("status code") { response.code }.equals(203)
+ end # through query string
+ end # with params
+ end # with a base-uri
+
+end # Sending a PUT request

0 comments on commit 96f77f1

Please sign in to comment.