Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d1fadd9
Showing
7 changed files
with
243 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Copyright (c) 2008 Fingertips, Eloy Duran <e.duran@fngtps.com> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
== RequestStubber | ||
|
||
Allows you to stub a request by either delaying the request time, | ||
returning a specific response code, or a combination of both. | ||
|
||
Note that the RequestStubber module is only mixed-in in development mode. | ||
|
||
=== Example | ||
|
||
Delays the process time of the request, by n seconds. | ||
|
||
class UsersController | ||
def index | ||
# ... | ||
end | ||
|
||
# Any request send to :index will be delayed 2 seconds. | ||
delay :index, 2 | ||
end | ||
|
||
Returns the specified response code. | ||
|
||
class UsersController | ||
def index | ||
# ... | ||
end | ||
|
||
# Any request send to :index will return a 404 response code. | ||
respond :index, 404 | ||
end | ||
|
||
Copyright (c) 2008 Fingertips, Eloy Duran <e.duran@fngtps.com>, released under the MIT license |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require 'rake' | ||
require 'rake/testtask' | ||
require 'rake/rdoctask' | ||
|
||
desc 'Default: run unit tests.' | ||
task :default => :test | ||
|
||
desc 'Test the request_stubber plugin.' | ||
Rake::TestTask.new(:test) do |t| | ||
t.libs << 'lib' | ||
t.pattern = 'test/**/*_test.rb' | ||
t.verbose = true | ||
end | ||
|
||
desc 'Generate documentation for the request_stubber plugin.' | ||
Rake::RDocTask.new(:rdoc) do |rdoc| | ||
rdoc.rdoc_dir = 'rdoc' | ||
rdoc.title = 'RequestStubber' | ||
rdoc.options << '--line-numbers' << '--inline-source' | ||
rdoc.rdoc_files.include('README') | ||
rdoc.rdoc_files.include('lib/**/*.rb') | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
if RAILS_ENV == 'development' | ||
require File.expand_path('../lib/request_stubber', __FILE__) | ||
ActionController::Base.send :extend, RequestStubber | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Allows you to stub a request by either delaying the request time, | ||
# returning a specific response code, or a combination of both. | ||
# | ||
# Note that the RequestStubber module is only mixed-in in development mode. | ||
module RequestStubber | ||
# Delays the process time of the request, by n seconds. | ||
# | ||
# class UsersController | ||
# def index | ||
# # ... | ||
# end | ||
# | ||
# # Any request send to :index will be delayed 2 seconds. | ||
# delay :index, 2 | ||
# end | ||
def delay(action, duration) | ||
add_request_stub action, :delay => duration | ||
end | ||
|
||
# Returns the specified response code. | ||
# | ||
# class UsersController | ||
# def index | ||
# # ... | ||
# end | ||
# | ||
# # Any request send to :index will return a 404 response code. | ||
# respond :index, 404 | ||
# end | ||
def respond(action, status) | ||
add_request_stub action, :respond => status | ||
end | ||
|
||
private | ||
|
||
def add_request_stub(action, stub) | ||
request_stubs[action] ||= {} | ||
request_stubs[action].merge!(stub) | ||
end | ||
|
||
def self.extended(klass) | ||
klass.class_inheritable_accessor :request_stubs | ||
klass.request_stubs = HashWithIndifferentAccess.new | ||
|
||
klass.prepend_before_filter do |controller| | ||
controller.instance_eval do | ||
if stubs = request_stubs[params[:action]] | ||
if delay = stubs['delay'] | ||
logger.debug(" \e[44mRequestStubber delayed the response by #{delay} seconds.\e[0m") | ||
sleep delay | ||
end | ||
if status = stubs['respond'] | ||
logger.debug(" \e[44mRequestStubber forced a #{status} response.\e[0m") | ||
head status | ||
false | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# desc "Explaining what the task does" | ||
# task :request_stubber do | ||
# # Task goes here | ||
# end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
$: << File.expand_path('../../', __FILE__) | ||
|
||
require 'rubygems' | ||
require 'active_support' | ||
require 'action_controller' | ||
require 'action_controller/test_case' | ||
require 'action_controller/test_process' | ||
|
||
require 'test/unit' | ||
require 'test/spec' | ||
|
||
RAILS_ENV = 'development' | ||
require 'init' | ||
|
||
logger = Object.new | ||
%w{ debug info }.each do |attr| | ||
eval "def logger.#{attr}(str); end" | ||
eval "def logger.#{attr}?; false; end" | ||
end | ||
ActionController::Base.logger = logger | ||
ActionController::Routing::Routes.reload rescue nil | ||
|
||
class ApplicationController < ActionController::Base | ||
def rescue_action(e) raise e end; | ||
end | ||
|
||
class StubbedController < ApplicationController | ||
|
||
def delayed_action | ||
render :text => 'this action was delayed!' | ||
end | ||
delay :delayed_action, 2 | ||
|
||
def respond_with_404 | ||
render :text => 'this text should not arrive!' | ||
end | ||
respond :respond_with_404, 404 | ||
|
||
def delayed_and_respond_with_500 | ||
render :text => 'this text should not arrive!' | ||
end | ||
delay :delayed_and_respond_with_500, 3 | ||
respond :delayed_and_respond_with_500, 500 | ||
end | ||
|
||
class NonStubbedController < ApplicationController | ||
def index | ||
render :nothing => true | ||
end | ||
end | ||
|
||
describe "A controller with stubbed requests", ActionController::TestCase do | ||
tests StubbedController | ||
|
||
it "should add the defined stubs for the controller to the request_stubs hash" do | ||
StubbedController.request_stubs.should == { | ||
'delayed_action' => { 'delay' => 2 }, | ||
'respond_with_404' => { 'respond' => 404 }, | ||
'delayed_and_respond_with_500' => { 'delay' => 3, 'respond' => 500 } | ||
} | ||
end | ||
|
||
it "should be able to delay the duration of a request" do | ||
should_be_delayed_by(2) do | ||
get :delayed_action | ||
@response.body.should == 'this action was delayed!' | ||
end | ||
end | ||
|
||
it "should be able to respond with a specific status code" do | ||
get :respond_with_404 | ||
@response.response_code.should == 404 | ||
@response.body.should.be.blank | ||
end | ||
|
||
it "should be to mix a delay and respond stub" do | ||
should_be_delayed_by(3) do | ||
get :delayed_and_respond_with_500 | ||
@response.response_code.should == 500 | ||
@response.body.should.be.blank | ||
end | ||
end | ||
|
||
private | ||
|
||
def should_be_delayed_by(seconds) | ||
start = Time.now | ||
yield | ||
Time.now.should >= start + seconds | ||
end | ||
end | ||
|
||
describe "A controller without stubbed requests", ActionController::TestCase do | ||
tests NonStubbedController | ||
|
||
it "should still work" do | ||
get :index | ||
@response.response_code.should == 200 | ||
end | ||
end |