mack 0.4.0.1 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,13 @@
1
+ ===0.4.1
2
+ * Improved testing support.
3
+ * Added ability to do 'session' based tests.
4
+ * Added assert_difference method.
5
+ * Added ability to set/remove cookies before requests are made.
6
+ * gem: mack_ruby_core_extensions 0.1.4
7
+
8
+ ===0.4.0.1
9
+ * gem: cachetastic 1.4.2
10
+
1
11
  ===0.4.0
2
12
  * Added Distributed Routes!
3
13
  * gem: mack_ruby_core_extensions 0.1.3
@@ -12,6 +12,7 @@ module Mack
12
12
  # This method needs to be defined as part of the Rack framework. As is noted for the Mack::Runner
13
13
  # class, this is where the center of the Mack framework lies.
14
14
  def call(env)
15
+ # pp env
15
16
  begin
16
17
  setup(env) do
17
18
  begin
@@ -22,6 +23,7 @@ module Mack
22
23
  else
23
24
  # let's handle a normal request:
24
25
  c = "#{route[:controller].to_s.camelcase}Controller".constantize.new(self.request, self.response, self.cookies)
26
+ self.response.controller = c
25
27
  self.response.write(c.run)
26
28
  end
27
29
  rescue Mack::Errors::ResourceNotFound, Mack::Errors::UndefinedRoute => e
@@ -85,7 +87,7 @@ module Mack
85
87
  sess_id = create_new_session
86
88
  end
87
89
  end
88
-
90
+
89
91
  yield
90
92
 
91
93
  Cachetastic::Caches::MackSessionCache.set(sess_id, self.request.session)
@@ -97,6 +99,7 @@ module Mack
97
99
  sess = Mack::Session.new(id)
98
100
  self.request.session = sess
99
101
  Cachetastic::Caches::MackSessionCache.set(id, sess)
102
+ id
100
103
  end
101
104
 
102
105
  def try_to_find_resource(env, path_info, exception)
@@ -2,5 +2,12 @@ module Mack
2
2
  # Right now Mack::Response is just a wrapper around Rack::Response.
3
3
  # Down the line this may be used to spice up the response.
4
4
  class Response < Rack::Response
5
+
6
+ attr_accessor :controller
7
+
8
+ def assigns(key)
9
+ self.controller.instance_variable_get("@#{key}")
10
+ end
11
+
5
12
  end
6
13
  end
@@ -33,6 +33,7 @@ module Mack
33
33
  # Asserts that the specified cookie has been set to the specified value.
34
34
  def assert_cookie(name, value)
35
35
  assert cookies[name.to_s]
36
+ assert_equal value, cookies[name.to_s]
36
37
  end
37
38
 
38
39
  # Asserts that there is no cookie set for the specified cookie
@@ -40,6 +41,13 @@ module Mack
40
41
  assert !cookies[name.to_s]
41
42
  end
42
43
 
44
+ def assert_difference(object, method = nil, difference = 1)
45
+ start_count = object.send(method)
46
+ yield
47
+ object.reload if object.respond_to? :reload
48
+ assert_equal start_count + difference, object.send(method)
49
+ end
50
+
43
51
  end # TestAssertions
44
52
 
45
53
  end # Mack
@@ -15,30 +15,35 @@ module Mack
15
15
  yield
16
16
  end
17
17
  end
18
+
19
+ # Retrieves an instance variable from the controller from a request.
20
+ def assigns(key)
21
+ $mack_app.instance_variable_get("@app").instance_variable_get("@response").instance_variable_get("@controller").instance_variable_get("@#{key}")
22
+ end
18
23
 
19
24
  # Performs a 'get' request for the specified uri.
20
25
  def get(uri, options = {})
21
- build_response(request.get(uri, options))
26
+ build_response(request.get(uri, build_request_options(options)))
22
27
  end
23
28
 
24
29
  # Performs a 'put' request for the specified uri.
25
30
  def put(uri, options = {})
26
- build_response(request.put(uri, :input => options.to_params))
31
+ build_response(request.put(uri, build_request_options({:input => options.to_params})))
27
32
  end
28
33
 
29
34
  # Performs a 'post' request for the specified uri.
30
35
  def post(uri, options = {})
31
- build_response(request.post(uri, :input => options.to_params))
36
+ build_response(request.post(uri, build_request_options({:input => options.to_params})))
32
37
  end
33
38
 
34
39
  # Performs a 'delete' request for the specified uri.
35
40
  def delete(uri, options = {})
36
- build_response(request.delete(uri, options))
41
+ build_response(request.delete(uri, build_request_options(options)))
37
42
  end
38
43
 
39
44
  # Returns a Rack::MockRequest. If there isn't one, a new one is created.
40
45
  def request
41
- @request ||= Rack::MockRequest.new(Rack::Recursive.new(Mack::Runner.new))
46
+ @request ||= Rack::MockRequest.new(mack_app)
42
47
  end
43
48
 
44
49
  # Returns the last Rack::MockResponse that got generated by a call.
@@ -56,27 +61,78 @@ module Mack
56
61
  Cachetastic::Caches::MackSessionCache.get(cookies[app_config.mack.session_id])
57
62
  end
58
63
 
64
+ # Used to create a 'session' around a block of code. This is great of 'integration' tests.
65
+ def in_session
66
+ @in_session = true
67
+ clear_session
68
+ yield
69
+ clear_session
70
+ @in_session = false
71
+ end
72
+
73
+ # Clears all the sessions.
74
+ def clear_session
75
+ Cachetastic::Caches::MackSessionCache.expire_all
76
+ end
77
+
59
78
  # Returns a Hash of cookies from the response.
60
79
  def cookies
61
- cooks = {}
62
- responses.each do |res|
63
- res.original_headers["Set-Cookie"].each do |ck|
64
- spt = ck.split("=")
65
- cooks[spt.first] = spt.last
66
- end
67
- end
68
- cooks
80
+ test_cookies
81
+ end
82
+
83
+ # Sets a cookie to be used for the next request
84
+ def set_cookie(name, value)
85
+ test_cookies[name] = value
86
+ end
87
+
88
+ # Removes a cookie.
89
+ def remove_cookie(name)
90
+ test_cookies.delete(name)
69
91
  end
70
92
 
71
93
  private
94
+ def test_cookies
95
+ @test_cookies = {} if @test_cookies.nil?
96
+ @test_cookies
97
+ end
98
+
99
+ def mack_app
100
+ if $mack_app.nil?
101
+ $mack_app = Rack::Recursive.new(Mack::Runner.new)
102
+ end
103
+ $mack_app
104
+ end
105
+
106
+ def build_request_options(options)
107
+ {"HTTP_COOKIE" => test_cookies.join("%s=%s", "; ")}.merge(options)
108
+ end
109
+
72
110
  def build_response(res)
73
111
  @responses = [res]
112
+ strip_cookies_from_response(res)
74
113
  until res.successful?
114
+ [res].flatten.each do |r|
115
+ strip_cookies_from_response(r)
116
+ end
75
117
  res = request.get(res["Location"])
76
118
  @responses << res
77
119
  end
78
120
  end
79
121
 
122
+ def strip_cookies_from_response(res)
123
+ unless res.original_headers["Set-Cookie"].nil?
124
+ res.original_headers["Set-Cookie"].each do |ck|
125
+ spt = ck.split("=")
126
+ name = spt.first
127
+ value = spt.last
128
+ if name == app_config.mack.session_id
129
+ value = nil unless @in_session
130
+ end
131
+ set_cookie(name, value)
132
+ end
133
+ end
134
+ end
135
+
80
136
  end # TestHelpers
81
137
 
82
138
  end # Mack
@@ -10,7 +10,7 @@ class GemHelper # :nodoc:
10
10
  self.project = "magrathea"
11
11
  self.package = "mack"
12
12
  self.gem_name = "mack"
13
- self.version = "0.4.0.1"
13
+ self.version = "0.4.1"
14
14
  end
15
15
 
16
16
  def gem_name_with_version
@@ -47,7 +47,7 @@ namespace :gem do
47
47
  s.rdoc_options << '--title' << 'Mack' << '--main' << 'README' << '--line-numbers' << "--inline-source"
48
48
 
49
49
  s.add_dependency("rack", "0.3.0")
50
- s.add_dependency("mack_ruby_core_extensions", "0.1.3")
50
+ s.add_dependency("mack_ruby_core_extensions", "0.1.4")
51
51
  s.add_dependency("application_configuration", "1.2.1")
52
52
  s.add_dependency("cachetastic", "1.4.2")
53
53
  s.add_dependency("log4r", "1.0.5")
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0.1
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - markbates
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-03-26 00:00:00 -04:00
12
+ date: 2008-03-27 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -28,7 +28,7 @@ dependencies:
28
28
  requirements:
29
29
  - - "="
30
30
  - !ruby/object:Gem::Version
31
- version: 0.1.3
31
+ version: 0.1.4
32
32
  version:
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: application_configuration