Fingertips-request_stubber 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README ADDED
@@ -0,0 +1,32 @@
1
+ == RequestStubber
2
+
3
+ Allows you to stub a request by either delaying the request time,
4
+ returning a specific response code, or a combination of both.
5
+
6
+ Note that the RequestStubber module is only mixed-in in development mode.
7
+
8
+ === Example
9
+
10
+ Delays the process time of the request, by n seconds.
11
+
12
+ class UsersController
13
+ def index
14
+ # ...
15
+ end
16
+
17
+ # Any request send to :index will be delayed 2 seconds.
18
+ delay :index, 2
19
+ end
20
+
21
+ Returns the specified response code.
22
+
23
+ class UsersController
24
+ def index
25
+ # ...
26
+ end
27
+
28
+ # Any request send to :index will return a 404 response code.
29
+ respond :index, 404
30
+ end
31
+
32
+ Copyright (c) 2008 Fingertips, Eloy Duran <e.duran@fngtps.com>, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the request_stubber plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = true
13
+ end
14
+
15
+ desc 'Generate documentation for the request_stubber plugin.'
16
+ Rake::RDocTask.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'RequestStubber'
19
+ rdoc.options << '--line-numbers' << '--inline-source'
20
+ rdoc.rdoc_files.include('README')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
23
+
24
+ begin
25
+ require 'jeweler'
26
+ Jeweler::Tasks.new do |s|
27
+ s.name = "request_stubber"
28
+ s.homepage = "http://github.com/Fingertips/request_stubber"
29
+ s.email = "eloy.de.enige@gmail.com"
30
+ s.authors = ["Eloy Duran"]
31
+ s.summary = s.description = "A simple Rails plugin to delay responses or return specific response codes. (Needs a new name.)"
32
+ end
33
+ rescue LoadError
34
+ end
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 1
4
+ :patch: 0
@@ -0,0 +1,61 @@
1
+ # Allows you to stub a request by either delaying the request time,
2
+ # returning a specific response code, or a combination of both.
3
+ #
4
+ # Note that the RequestStubber module is only mixed-in in development mode.
5
+ module RequestStubber
6
+ # Delays the process time of the request, by n seconds.
7
+ #
8
+ # class UsersController
9
+ # def index
10
+ # # ...
11
+ # end
12
+ #
13
+ # # Any request send to :index will be delayed 2 seconds.
14
+ # delay :index, 2
15
+ # end
16
+ def delay(action, duration)
17
+ add_request_stub action, :delay => duration
18
+ end
19
+
20
+ # Returns the specified response code.
21
+ #
22
+ # class UsersController
23
+ # def index
24
+ # # ...
25
+ # end
26
+ #
27
+ # # Any request send to :index will return a 404 response code.
28
+ # respond :index, 404
29
+ # end
30
+ def respond(action, status)
31
+ add_request_stub action, :respond => status
32
+ end
33
+
34
+ private
35
+
36
+ def add_request_stub(action, stub)
37
+ request_stubs[action] ||= {}
38
+ request_stubs[action].merge!(stub)
39
+ end
40
+
41
+ def self.extended(klass)
42
+ klass.class_inheritable_accessor :request_stubs
43
+ klass.request_stubs = HashWithIndifferentAccess.new
44
+
45
+ klass.prepend_before_filter do |controller|
46
+ controller.instance_eval do
47
+ if stubs = request_stubs[params[:action]]
48
+ if delay = stubs['delay']
49
+ logger.debug(" \e[44mRequestStubber delayed the response by #{delay} seconds.\e[0m")
50
+ sleep delay
51
+ end
52
+ if status = stubs['respond']
53
+ logger.debug(" \e[44mRequestStubber forced a #{status} response.\e[0m")
54
+ head status
55
+ false
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,100 @@
1
+ $: << File.expand_path('../../', __FILE__)
2
+
3
+ require 'rubygems'
4
+ require 'active_support'
5
+ require 'action_controller'
6
+ require 'action_controller/test_case'
7
+ require 'action_controller/test_process'
8
+
9
+ require 'test/unit'
10
+ require 'test/spec'
11
+
12
+ RAILS_ENV = 'development'
13
+ require 'init'
14
+
15
+ logger = Object.new
16
+ %w{ debug info }.each do |attr|
17
+ eval "def logger.#{attr}(str); end"
18
+ eval "def logger.#{attr}?; false; end"
19
+ end
20
+ ActionController::Base.logger = logger
21
+ ActionController::Routing::Routes.reload rescue nil
22
+
23
+ class ApplicationController < ActionController::Base
24
+ def rescue_action(e) raise e end;
25
+ end
26
+
27
+ class StubbedController < ApplicationController
28
+
29
+ def delayed_action
30
+ render :text => 'this action was delayed!'
31
+ end
32
+ delay :delayed_action, 2
33
+
34
+ def respond_with_404
35
+ render :text => 'this text should not arrive!'
36
+ end
37
+ respond :respond_with_404, 404
38
+
39
+ def delayed_and_respond_with_500
40
+ render :text => 'this text should not arrive!'
41
+ end
42
+ delay :delayed_and_respond_with_500, 3
43
+ respond :delayed_and_respond_with_500, 500
44
+ end
45
+
46
+ class NonStubbedController < ApplicationController
47
+ def index
48
+ render :nothing => true
49
+ end
50
+ end
51
+
52
+ describe "A controller with stubbed requests", ActionController::TestCase do
53
+ tests StubbedController
54
+
55
+ it "should add the defined stubs for the controller to the request_stubs hash" do
56
+ StubbedController.request_stubs.should == {
57
+ 'delayed_action' => { 'delay' => 2 },
58
+ 'respond_with_404' => { 'respond' => 404 },
59
+ 'delayed_and_respond_with_500' => { 'delay' => 3, 'respond' => 500 }
60
+ }
61
+ end
62
+
63
+ it "should be able to delay the duration of a request" do
64
+ should_be_delayed_by(2) do
65
+ get :delayed_action
66
+ @response.body.should == 'this action was delayed!'
67
+ end
68
+ end
69
+
70
+ it "should be able to respond with a specific status code" do
71
+ get :respond_with_404
72
+ @response.response_code.should == 404
73
+ @response.body.should.be.blank
74
+ end
75
+
76
+ it "should be to mix a delay and respond stub" do
77
+ should_be_delayed_by(3) do
78
+ get :delayed_and_respond_with_500
79
+ @response.response_code.should == 500
80
+ @response.body.should.be.blank
81
+ end
82
+ end
83
+
84
+ private
85
+
86
+ def should_be_delayed_by(seconds)
87
+ start = Time.now
88
+ yield
89
+ Time.now.should >= start + seconds
90
+ end
91
+ end
92
+
93
+ describe "A controller without stubbed requests", ActionController::TestCase do
94
+ tests NonStubbedController
95
+
96
+ it "should still work" do
97
+ get :index
98
+ @response.response_code.should == 200
99
+ end
100
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: Fingertips-request_stubber
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Eloy Duran
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-08-28 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A simple Rails plugin to delay responses or return specific response codes. (Needs a new name.)
17
+ email: eloy.de.enige@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ files:
25
+ - Rakefile
26
+ - VERSION.yml
27
+ - lib/request_stubber.rb
28
+ - test/request_stubber_test.rb
29
+ - README
30
+ has_rdoc: true
31
+ homepage: http://github.com/Fingertips/request_stubber
32
+ licenses:
33
+ post_install_message:
34
+ rdoc_options:
35
+ - --charset=UTF-8
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: "0"
43
+ version:
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ requirements: []
51
+
52
+ rubyforge_project:
53
+ rubygems_version: 1.3.5
54
+ signing_key:
55
+ specification_version: 2
56
+ summary: A simple Rails plugin to delay responses or return specific response codes. (Needs a new name.)
57
+ test_files:
58
+ - test/request_stubber_test.rb