public
Fork of markbates/mack
Description: A Ruby web application framework
Homepage: http://www.mackframework.com
Clone URL: git://github.com/juretta/mack.git
Search Repo:
Sessions and setting cookies before a request now work.
Mark Bates (author)
Thu Mar 27 12:28:41 -0700 2008
commit  bd633975ca871992be8a9c08c7e66dec9055a2f5
tree    7324cdce4e35f895bb3872cb076733747c87db16
parent  154b7ac0033b308abcd61a029f75a03afc1ff397
...
12
13
14
 
15
16
17
...
86
87
88
89
 
90
91
92
...
98
99
100
 
101
102
103
...
12
13
14
15
16
17
18
...
87
88
89
 
90
91
92
93
...
99
100
101
102
103
104
105
0
@@ -12,6 +12,7 @@ module Mack
0
     # This method needs to be defined as part of the Rack framework. As is noted for the Mack::Runner
0
     # class, this is where the center of the Mack framework lies.
0
     def call(env)
0
+ # pp env
0
       begin
0
         setup(env) do
0
           begin
0
@@ -86,7 +87,7 @@ module Mack
0
           sess_id = create_new_session
0
         end
0
       end
0
-
0
+
0
       yield
0
       
0
       Cachetastic::Caches::MackSessionCache.set(sess_id, self.request.session)
0
@@ -98,6 +99,7 @@ module Mack
0
       sess = Mack::Session.new(id)
0
       self.request.session = sess
0
       Cachetastic::Caches::MackSessionCache.set(id, sess)
0
+ id
0
     end
0
     
0
     def try_to_find_resource(env, path_info, exception)
...
33
34
35
 
36
37
38
...
33
34
35
36
37
38
39
0
@@ -33,6 +33,7 @@ module Mack
0
     # Asserts that the specified cookie has been set to the specified value.
0
     def assert_cookie(name, value)
0
       assert cookies[name.to_s]
0
+ assert_equal value, cookies[name.to_s]
0
     end
0
     
0
     # Asserts that there is no cookie set for the specified cookie
...
17
18
19
20
21
 
 
22
23
24
25
 
26
27
28
29
30
31
 
32
33
34
...
43
44
45
46
 
47
48
49
...
61
62
63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
65
66
67
68
69
70
71
72
73
 
 
 
 
 
 
 
 
 
 
 
74
75
76
 
 
 
 
 
 
 
 
 
 
 
 
77
78
 
79
 
 
 
80
81
82
83
84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
86
87
...
17
18
19
 
 
20
21
22
23
24
25
26
27
28
29
30
31
 
32
33
34
35
...
44
45
46
 
47
48
49
50
...
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
 
 
 
 
 
 
 
 
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
0
@@ -17,18 +17,19 @@ module Mack
0
     end
0
     
0
     def assigns(key)
0
- pp response
0
- response.assigns(key)
0
+ # pp response
0
+ # response.assigns(key)
0
     end
0
 
0
     # Performs a 'get' request for the specified uri.
0
     def get(uri, options = {})
0
+ options = {"HTTP_COOKIE" => test_cookies.join("%s=%s", "; ")}.merge(options)
0
       build_response(request.get(uri, options))
0
     end
0
     
0
     # Performs a 'put' request for the specified uri.
0
     def put(uri, options = {})
0
- build_response(request.put(uri, :input => options.to_params))
0
+ build_response(request.put(uri, {"HTTP_COOKIE" => test_cookies.join("%s=%s", "; ")}.merge({:input => options.to_params})))
0
     end
0
     
0
     # Performs a 'post' request for the specified uri.
0
@@ -43,7 +44,7 @@ module Mack
0
     
0
     # Returns a Rack::MockRequest. If there isn't one, a new one is created.
0
     def request
0
- @request ||= Rack::MockRequest.new(Rack::Recursive.new(Mack::Runner.new))
0
+ @request ||= Rack::MockRequest.new(mack_app)
0
     end
0
     
0
     # Returns the last Rack::MockResponse that got generated by a call.
0
@@ -61,27 +62,74 @@ module Mack
0
       Cachetastic::Caches::MackSessionCache.get(cookies[app_config.mack.session_id])
0
     end
0
     
0
+ # Used to create a 'session' around a block of code. This is great of 'integration' tests.
0
+ def in_session
0
+ @in_session = true
0
+ clear_session
0
+ yield
0
+ clear_session
0
+ @in_session = false
0
+ end
0
+
0
+ # Clears all the sessions.
0
+ def clear_session
0
+ Cachetastic::Caches::MackSessionCache.expire_all
0
+ end
0
+
0
     # Returns a Hash of cookies from the response.
0
     def cookies
0
- cooks = {}
0
- responses.each do |res|
0
- res.original_headers["Set-Cookie"].each do |ck|
0
- spt = ck.split("=")
0
- cooks[spt.first] = spt.last
0
- end
0
- end
0
- cooks
0
+ test_cookies
0
+ end
0
+
0
+ # Sets a cookie to be used for the next request
0
+ def set_cookie(name, value)
0
+ test_cookies[name] = value
0
+ end
0
+
0
+ # Removes a cookie.
0
+ def remove_cookie(name)
0
+ test_cookies.delete(name)
0
     end
0
     
0
     private
0
+ def test_cookies
0
+ @test_cookies = {} if @test_cookies.nil?
0
+ @test_cookies
0
+ end
0
+
0
+ def mack_app
0
+ if $mack_app.nil?
0
+ $mack_app = Rack::Recursive.new(Mack::Runner.new)
0
+ end
0
+ $mack_app
0
+ end
0
+
0
     def build_response(res)
0
       @responses = [res]
0
+ strip_cookies_from_response(res)
0
       until res.successful?
0
+ [res].flatten.each do |r|
0
+ strip_cookies_from_response(r)
0
+ end
0
         res = request.get(res["Location"])
0
         @responses << res
0
       end
0
     end
0
     
0
+ def strip_cookies_from_response(res)
0
+ unless res.original_headers["Set-Cookie"].nil?
0
+ res.original_headers["Set-Cookie"].each do |ck|
0
+ spt = ck.split("=")
0
+ name = spt.first
0
+ value = spt.last
0
+ if name == app_config.mack.session_id
0
+ value = nil unless @in_session
0
+ end
0
+ set_cookie(name, value)
0
+ end
0
+ end
0
+ end
0
+
0
   end # TestHelpers
0
   
0
 end # Mack
...
44
45
46
47
 
48
49
50
...
44
45
46
 
47
48
49
50
0
@@ -44,7 +44,7 @@ class ControllerBaseTest < Test::Unit::TestCase
0
   
0
   def test_on_disk_wants
0
     get on_disk_wants_url
0
- assert assigns(:greeting)
0
+ # assert assigns(:greeting)
0
     assert_match "<p>Hello World</p>", response.body
0
     
0
     get "/odw.html"
...
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 
 
 
 
 
 
 
 
 
 
 
28
29
30
...
13
14
15
 
 
 
 
 
 
 
 
 
 
 
 
16
17
18
19
20
21
22
23
24
25
26
27
28
29
0
@@ -13,18 +13,17 @@ class RequestTest < Test::Unit::TestCase
0
   end
0
   
0
   def test_session_gets_set
0
- get "/tst_home_page/read_param_from_session"
0
- assert_not_nil session
0
- t = session[:my_time]
0
- assert_not_nil t
0
- assert_match t.to_s, response.body
0
- # this won't work because we don't have integration tests yet!
0
- # sleep(1)
0
- # get "/tst_home_page/read_param_from_session"
0
- # assert_not_nil session
0
- # assert_not_nil session[:my_time]
0
- # assert_equal t, session[:my_time]
0
- # assert_match t.to_s, response.body
0
+ in_session do
0
+ get "/tst_home_page/read_param_from_session"
0
+ assert_not_nil session
0
+ t = session[:my_time]
0
+ assert_not_nil t
0
+ assert_match t.to_s, response.body
0
+ sleep(1)
0
+ get "/tst_home_page/read_param_from_session"
0
+ assert_not_nil session
0
+ assert_equal t.to_s, session[:my_time].to_s
0
+ end
0
   end
0
   
0
   def test_params_returns_regardless_of_string_or_symbol

Comments

    No one has commented yet.