GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Rubygem
Description: Resource-oriented open source Ruby framework for Web apps.
Homepage: http://rubywaves.com/
Clone URL: git://github.com/dyoder/waves.git
specs for Waves::Session; a little cleanup of other tests
automatthew (author)
Thu Jun 05 09:52:38 -0700 2008
commit  cffdb83e8308f6250d14cfc5ccb424df140b8dff
tree    88f5ed6011084294a2a0a8046052bb4711c72fdd
parent  7ff456148229784ad47a41e643101b9a451794fa
...
5
6
7
 
 
 
 
 
8
9
10
...
14
15
16
 
 
 
 
 
 
 
 
 
17
18
19
...
34
35
36
37
38
39
40
41
42
 
43
44
45
46
 
47
48
49
...
5
6
7
8
9
10
11
12
13
14
15
...
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
...
48
49
50
 
 
 
 
 
 
51
52
53
54
 
55
56
57
58
0
@@ -5,6 +5,11 @@ module Waves
0
   # more information.
0
   #
0
   class Session
0
+
0
+ def self.generate_session_id # from Camping ...
0
+ chars = [*'A'..'Z'] + [*'0'..'9'] + [*'a'..'z']
0
+ (0..48).inject(''){|s,x| s+=chars[ rand(chars.length) ] }
0
+ end
0
 
0
     # Create a new session object using the given request. This is not necessarily the
0
     # same as constructing a new session. The session_id cookie for the request domain
0
@@ -14,6 +19,15 @@ module Waves
0
       @request = request
0
       @data ||= ( File.exist?( session_path ) ? load_session : {} )
0
     end
0
+
0
+ # Return the session data as a hash
0
+ def to_hash
0
+ @data.to_hash
0
+ end
0
+
0
+ def self.base_path
0
+ Waves::Application.instance.config.session[:path]
0
+ end
0
 
0
     # Save the session data. You shouldn't typically have to call this directly, since it
0
     # is called by Waves::Response#finish.
0
@@ -34,16 +48,11 @@ module Waves
0
     private
0
 
0
     def session_id
0
- @session_id ||= ( @request.cookies['session_id'] || generate_session_id )
0
- end
0
-
0
- def generate_session_id # from Camping ...
0
- chars = [*'A'..'Z'] + [*'0'..'9'] + [*'a'..'z']
0
- (0..48).inject(''){|s,x| s+=chars[ rand(chars.length) ] }
0
+ @session_id ||= ( @request.cookies['session_id'] || Waves::Session.generate_session_id )
0
     end
0
 
0
     def session_path
0
- Waves::Application.instance.config.session[:path] / session_id
0
+ Waves::Session.base_path / session_id
0
     end
0
 
0
     def load_session
...
1
2
3
4
 
5
6
7
...
18
19
20
21
 
22
23
24
...
1
2
3
 
4
5
6
7
...
18
19
20
 
21
22
23
24
0
@@ -1,7 +1,7 @@
0
 # require 'test_helper' because RubyMate needs help
0
 require File.join(File.dirname(__FILE__) , "helpers")
0
 
0
-describe "The attributes of a configuration class" do
0
+describe "Configuration attributes" do
0
   
0
   class Basic < Waves::Configurations::Base; end
0
   
0
@@ -18,7 +18,7 @@ describe "The attributes of a configuration class" do
0
   
0
 end
0
 
0
-describe "The default configuration class" do
0
+describe "Waves::Configurations::Default" do
0
   
0
   class Default < Waves::Configurations::Default; end
0
   
...
1
2
3
4
 
5
6
7
...
17
18
19
20
 
21
22
23
...
1
2
3
 
4
5
6
7
...
17
18
19
 
20
21
22
23
0
@@ -1,7 +1,7 @@
0
 # require 'test_helper' because RubyMate needs help
0
 require File.join(File.dirname(__FILE__) , "helpers")
0
 
0
-describe "The default configuration class" do
0
+describe "A Waves Configuration" do
0
   
0
   class Default < Waves::Configurations::Default; end
0
   
0
@@ -17,7 +17,7 @@ describe "The default configuration class" do
0
     Default.mime_types['foo.png'].should == 'image/png'
0
   end
0
   
0
- it "defines the application for use with Rack" do
0
+ it "must define the application for use with Rack" do
0
     app = Rack::Builder.new() {}
0
     Rack::Builder.should.receive(:new).with().and_return(app)
0
     Default.application do
...
4
5
6
7
 
8
9
10
...
4
5
6
 
7
8
9
10
0
@@ -4,7 +4,7 @@ require File.join(File.dirname(__FILE__) , "helpers")
0
 # Nota bene, y'all: These model helper methods are very ORM specific, and should probably
0
 # get moved into the ORM layers.
0
 
0
-describe "An instance of the basic Waves controller" do
0
+describe "The base Waves controller" do
0
   
0
   before do
0
     Waves.application.stub!(:models).and_return(VerifyControllers::Models)
...
1
2
3
4
 
5
6
7
...
1
2
3
 
4
5
6
7
0
@@ -1,7 +1,7 @@
0
 # require 'test_helper' because RubyMate needs help
0
 require File.join(File.dirname(__FILE__) , "helpers")
0
 
0
-describe "A Waves controller instance" do
0
+describe "A Waves controller" do
0
     
0
   before do
0
     Waves.application.stub!(:models).and_return(VerifyControllers::Models)
...
15
16
17
18
 
19
20
21
...
15
16
17
 
18
19
20
21
0
@@ -15,7 +15,7 @@ module DefaultErrorsApp
0
   stub!(:views).and_return(Views)
0
 end
0
 
0
-Waves << DefaultErrorsApp
0
+# Waves << DefaultErrorsApp
0
 Waves::Console.load( :mode => :development )
0
 
0
 
...
14
15
16
17
 
18
19
20
...
26
27
28
29
 
30
31
32
...
34
35
36
 
 
 
37
...
14
15
16
 
17
18
19
20
...
26
27
28
 
29
30
31
32
...
34
35
36
37
38
39
40
0
@@ -14,7 +14,7 @@ module TestApplication
0
 end
0
 
0
 Waves << TestApplication
0
-Waves::Console.load( :mode => :development )
0
+# Waves::Console.load( :mode => :development )
0
 TA = TestApplication
0
 
0
 describe "An application module which includes the Sequel ORM layer" do
0
@@ -26,7 +26,7 @@ describe "An application module which includes the Sequel ORM layer" do
0
   end
0
   
0
   it "sets the dataset to use the snake_case of the class name as the table name" do
0
- TA::Models::Default.dataset.to_table_reference.should == "(SELECT * FROM defaults)"
0
+ TA::Models::Default.dataset.send(:to_table_reference).should =~ /SELECT.+FROM.+defaults+/
0
   end
0
   
0
   it "provides an accessor for database" do
0
@@ -34,3 +34,6 @@ describe "An application module which includes the Sequel ORM layer" do
0
   end
0
   
0
 end
0
+
0
+# Waves.instance_variable_set(:@application, nil)
0
+# raise Waves.application.inspect
0
\ No newline at end of file
...
3
4
5
6
 
 
 
7
8
9
10
11
12
13
14
15
16
17
18
19
20
...
3
4
5
 
6
7
8
9
10
11
12
13
 
 
 
 
 
14
15
16
17
0
@@ -3,17 +3,14 @@ require File.join(File.dirname(__FILE__) , "..", "helpers")
0
 module TestRequests
0
   module Configurations
0
     class Default
0
- stub!(:session).and_return(:duration => 30.minutes, :path => '/tmp/sessions')
0
+ # stub!(:session).and_return(:duration => 30.minutes, :path => '/tmp/sessions')
0
+ ::BasePath = File.dirname(__FILE__) / :tmp
0
+ stub!(:session).and_return(:duration => 30.minutes, :path => ::BasePath)
0
     end
0
   end
0
   
0
 end
0
 
0
-Waves.application.stub!(:instance)
0
-Waves.application.instance.stub!(:config).and_return(TestRequests::Configurations::Default)
0
-
0
-
0
-
0
 def env_for(uri="/", options={})
0
   Rack::MockRequest.env_for(uri,options)
0
 end
0
\ No newline at end of file
...
4
5
6
 
7
8
9
...
37
38
39
 
 
 
 
40
41
42
...
4
5
6
7
8
9
10
...
38
39
40
41
42
43
44
45
46
47
0
@@ -4,6 +4,7 @@ require File.join(File.dirname(__FILE__) , "helpers")
0
 describe "A Waves request instance" do
0
   
0
   before do
0
+ Waves::Session.stub!(:base_path).and_return(BasePath)
0
     @request = Waves::Request.new(env_for)
0
   end
0
   
0
@@ -37,6 +38,10 @@ end
0
 
0
 describe "The HTTP request method" do
0
   
0
+ before do
0
+ Waves::Session.stub!(:base_path).and_return(BasePath)
0
+ end
0
+
0
   it "is determined in a straightforward manner for straightforward requests" do
0
     @get = Waves::Request.new(env_for("/", :method => 'GET'))
0
     @post = Waves::Request.new(env_for("/", :method => 'POST'))
...
4
5
6
 
7
8
9
...
44
45
46
 
47
48
49
...
4
5
6
7
8
9
10
...
45
46
47
48
49
50
51
0
@@ -4,6 +4,7 @@ require File.join(File.dirname(__FILE__) , "helpers")
0
 describe "An instance of Waves::Response" do
0
   
0
   before do
0
+ Waves::Session.stub!(:base_path).and_return(BasePath)
0
     @request = Waves::Request.new(env_for)
0
     @response = Waves::Response.new(@request)
0
   end
0
@@ -44,6 +45,7 @@ end
0
 describe "Waves::Response#finish" do
0
   
0
   before do
0
+ Waves::Session.stub!(:base_path).and_return(BasePath)
0
     @request = Waves::Request.new(env_for)
0
     @response = Waves::Response.new(@request)
0
   end
...
1
2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
0
@@ -1,2 +1,54 @@
0
 # require 'test_helper' because RubyMate needs help
0
 require File.join(File.dirname(__FILE__) , "helpers")
0
+
0
+SessionData = "--- \nmoo: cow\n"
0
+
0
+
0
+
0
+describe "An new instance of Waves::Session" do
0
+
0
+
0
+ before do
0
+ FileUtils.mkdir(BasePath) unless File.exist?(BasePath)
0
+ Waves::Session.stub!(:generate_session_id).and_return("fake_session")
0
+ Waves::Session.stub!(:base_path).and_return(BasePath)
0
+ end
0
+
0
+ after do
0
+ FileUtils.rm Dir["#{BasePath}/fake_session"]
0
+ FileUtils.rmdir BasePath
0
+ end
0
+
0
+ it "loads data from a session file if one exists" do
0
+ File.write(BasePath / :fake_session, SessionData)
0
+ @request = Waves::Request.new(env_for)
0
+ session = @request.session
0
+ session.to_hash.should == { 'moo' => 'cow'}
0
+ end
0
+
0
+ it "is empty when no session file exists" do
0
+ Waves::Session.stub!(:generate_session_id).and_return("other_session")
0
+ @request = Waves::Request.new(env_for)
0
+ session = @request.session
0
+ session.to_hash.should.be.empty
0
+ end
0
+
0
+end
0
+
0
+describe "Session values" do
0
+
0
+ before do
0
+ Waves::Session.stub!(:base_path).and_return(BasePath)
0
+ @request = Waves::Request.new(env_for)
0
+ end
0
+
0
+ it "can be read and written using [] and []=" do
0
+ session = Waves::Session.new(@request)
0
+ session['moo'].should.be.nil
0
+ session['moo'] = 'bull'
0
+ session['moo'].should == 'bull'
0
+ end
0
+
0
+end
0
+
0
+

Comments

    No one has commented yet.