Updated README.rdoc again
[feedcatcher.git] / vendor / rails / actionpack / test / controller / session / mem_cache_store_test.rb
1 require 'abstract_unit'
2
3 # You need to start a memcached server inorder to run these tests
4 class MemCacheStoreTest < ActionController::IntegrationTest
5 class TestController < ActionController::Base
6 def no_session_access
7 head :ok
8 end
9
10 def set_session_value
11 session[:foo] = "bar"
12 head :ok
13 end
14
15 def get_session_value
16 render :text => "foo: #{session[:foo].inspect}"
17 end
18
19 def get_session_id
20 session[:foo]
21 render :text => "#{request.session_options[:id]}"
22 end
23
24 def call_reset_session
25 session[:bar]
26 reset_session
27 session[:bar] = "baz"
28 head :ok
29 end
30
31 def rescue_action(e) raise end
32 end
33
34 begin
35 DispatcherApp = ActionController::Dispatcher.new
36 MemCacheStoreApp = ActionController::Session::MemCacheStore.new(
37 DispatcherApp, :key => '_session_id')
38
39
40 def setup
41 @integration_session = open_session(MemCacheStoreApp)
42 end
43
44 def test_setting_and_getting_session_value
45 with_test_route_set do
46 get '/set_session_value'
47 assert_response :success
48 assert cookies['_session_id']
49
50 get '/get_session_value'
51 assert_response :success
52 assert_equal 'foo: "bar"', response.body
53 end
54 end
55
56 def test_getting_nil_session_value
57 with_test_route_set do
58 get '/get_session_value'
59 assert_response :success
60 assert_equal 'foo: nil', response.body
61 end
62 end
63
64 def test_setting_session_value_after_session_reset
65 with_test_route_set do
66 get '/set_session_value'
67 assert_response :success
68 assert cookies['_session_id']
69 session_id = cookies['_session_id']
70
71 get '/call_reset_session'
72 assert_response :success
73 assert_not_equal [], headers['Set-Cookie']
74
75 get '/get_session_value'
76 assert_response :success
77 assert_equal 'foo: nil', response.body
78
79 get '/get_session_id'
80 assert_response :success
81 assert_not_equal session_id, response.body
82 end
83 end
84
85 def test_getting_session_id
86 with_test_route_set do
87 get '/set_session_value'
88 assert_response :success
89 assert cookies['_session_id']
90 session_id = cookies['_session_id']
91
92 get '/get_session_id'
93 assert_response :success
94 assert_equal session_id, response.body
95 end
96 end
97
98 def test_prevents_session_fixation
99 with_test_route_set do
100 get '/get_session_value'
101 assert_response :success
102 assert_equal 'foo: nil', response.body
103 session_id = cookies['_session_id']
104
105 reset!
106
107 get '/set_session_value', :_session_id => session_id
108 assert_response :success
109 assert_equal nil, cookies['_session_id']
110 end
111 end
112 rescue LoadError, RuntimeError
113 $stderr.puts "Skipping MemCacheStoreTest tests. Start memcached and try again."
114 end
115
116 private
117 def with_test_route_set
118 with_routing do |set|
119 set.draw do |map|
120 map.with_options :controller => "mem_cache_store_test/test" do |c|
121 c.connect "/:action"
122 end
123 end
124 yield
125 end
126 end
127 end