strelka 0.0.1.pre.187 → 0.0.1.pre.193

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.
Files changed (47) hide show
  1. data.tar.gz.sig +0 -0
  2. data/ChangeLog +94 -26
  3. data/Manifest.txt +4 -2
  4. data/examples/apps/ws-echo +17 -0
  5. data/lib/strelka/app.rb +26 -24
  6. data/lib/strelka/app/auth.rb +2 -1
  7. data/lib/strelka/app/errors.rb +1 -1
  8. data/lib/strelka/app/filters.rb +1 -1
  9. data/lib/strelka/app/negotiation.rb +1 -1
  10. data/lib/strelka/app/parameters.rb +2 -2
  11. data/lib/strelka/app/restresources.rb +1 -1
  12. data/lib/strelka/app/routing.rb +3 -2
  13. data/lib/strelka/app/sessions.rb +3 -3
  14. data/lib/strelka/app/templating.rb +3 -3
  15. data/lib/strelka/authprovider.rb +2 -10
  16. data/lib/strelka/behavior/plugin.rb +3 -3
  17. data/lib/strelka/httprequest.rb +5 -2
  18. data/lib/strelka/httprequest/session.rb +3 -2
  19. data/lib/strelka/httpresponse/session.rb +8 -9
  20. data/lib/strelka/mixins.rb +15 -0
  21. data/lib/strelka/plugins.rb +257 -0
  22. data/lib/strelka/router/default.rb +27 -2
  23. data/lib/strelka/session.rb +20 -2
  24. data/lib/strelka/session/db.rb +20 -10
  25. data/lib/strelka/session/default.rb +41 -18
  26. data/spec/lib/helpers.rb +1 -1
  27. data/spec/strelka/app/auth_spec.rb +1 -1
  28. data/spec/strelka/app/errors_spec.rb +1 -1
  29. data/spec/strelka/app/filters_spec.rb +1 -1
  30. data/spec/strelka/app/negotiation_spec.rb +1 -1
  31. data/spec/strelka/app/parameters_spec.rb +1 -1
  32. data/spec/strelka/app/restresources_spec.rb +1 -1
  33. data/spec/strelka/app/routing_spec.rb +4 -1
  34. data/spec/strelka/app/sessions_spec.rb +63 -17
  35. data/spec/strelka/app/templating_spec.rb +1 -1
  36. data/spec/strelka/app_spec.rb +13 -5
  37. data/spec/strelka/httprequest/session_spec.rb +44 -23
  38. data/spec/strelka/httprequest_spec.rb +21 -0
  39. data/spec/strelka/httpresponse/session_spec.rb +143 -0
  40. data/spec/strelka/{app/plugins_spec.rb → plugins_spec.rb} +64 -53
  41. data/spec/strelka/router/default_spec.rb +15 -0
  42. data/spec/strelka/router/exclusive_spec.rb +14 -0
  43. data/spec/strelka/session/db_spec.rb +11 -0
  44. data/spec/strelka/session/default_spec.rb +10 -2
  45. metadata +119 -37
  46. metadata.gz.sig +0 -0
  47. data/lib/strelka/app/plugins.rb +0 -284
@@ -25,7 +25,7 @@ require 'strelka/session/default'
25
25
  ### C O N T E X T S
26
26
  #####################################################################
27
27
 
28
- describe Strelka::HTTPRequest::Session do
28
+ describe Strelka::HTTPRequest::Session, "-extended request" do
29
29
 
30
30
  before( :all ) do
31
31
  setup_logging( :fatal )
@@ -48,54 +48,75 @@ describe Strelka::HTTPRequest::Session do
48
48
  end
49
49
 
50
50
 
51
- describe "an HTTPRequest with no session loaded" do
51
+ it "has a session_namespace attribute" do
52
+ @req.should respond_to( :session_namespace )
53
+ end
52
54
 
53
- it "has a session_namespace attribute" do
54
- @req.should respond_to( :session_namespace )
55
- end
56
55
 
57
- it "knows whether or not it has loaded a session" do
58
- @req.session?.should be_false()
56
+ context "with no session ID" do
57
+
58
+ it "knows that it doesn't have a session" do
59
+ @req.should_not have_session()
59
60
  end
60
61
 
61
62
  it "doesn't load the session when the session namespace is set" do
62
63
  @req.session_namespace = 'an_appid'
63
- @req.session?.should be_false()
64
+ @req.should_not have_session()
64
65
  end
65
66
 
66
- it "loads the session as soon as it's accessed" do
67
+ it "creates a new session as soon as it's accessed" do
67
68
  @req.session.should be_a( Strelka::Session::Default )
68
69
  end
69
70
 
70
- it "sets the session's namespace when it's loaded" do
71
+ it "sets its session's namespace when it loads if the session_namespace is set" do
71
72
  @req.session_namespace = 'an_appid'
72
73
  @req.session.namespace.should == :an_appid
73
74
  end
74
75
 
75
- it "sets a session's namespace when it's set directly" do
76
- @req.should respond_to( :session_namespace= )
77
- @req.session_namespace = 'the_appid'
78
76
 
79
- session = mock( "session object" )
80
- session.should_receive( :namespace= ).with( 'the_appid' )
77
+ context "but with a loaded session object" do
78
+
79
+ before( :each ) do
80
+ @session = Strelka::Session.create( :default )
81
+ @req.session = @session
82
+ end
81
83
 
82
- @req.session = session
84
+ it "knows that it has a session" do
85
+ @req.should have_session()
86
+ end
87
+
88
+ it "sets the session's namespace when its session_namespace is set" do
89
+ @req.session_namespace = 'the_appid'
90
+ @session.namespace.should == :the_appid
91
+ end
83
92
  end
84
93
 
85
94
  end
86
95
 
87
96
 
88
- describe "an HTTPRequest with a session loaded" do
97
+ context "with a session ID" do
89
98
 
90
99
  before( :each ) do
91
- @req.session_namespace = 'other_appid'
92
- @req.session
100
+ cookie_name = Strelka::Session::Default.cookie_options[ :name ]
101
+ @sess_id = Strelka::Session::Default.get_session_id
102
+ @req.header.cookie = "#{cookie_name}=#{@sess_id}"
93
103
  end
94
104
 
95
- it "sets its session's namespace when its session_namespace attribute is set" do
96
- @req.session.namespace.should == :other_appid
97
- @req.session_namespace = 'an_appid'
98
- @req.session.namespace.should == :an_appid
105
+ it "knows that it doesn't have a session unless the ID exists" do
106
+ @req.should_not have_session()
107
+ end
108
+
109
+
110
+ context "and a corresponding entry in the database" do
111
+
112
+ before( :each ) do
113
+ Strelka::Session::Default.sessions[ @sess_id ] = {}
114
+ end
115
+
116
+ it "knows that it has a session" do
117
+ @req.should have_session()
118
+ end
119
+
99
120
  end
100
121
 
101
122
  end
@@ -150,6 +150,27 @@ describe Strelka::HTTPRequest do
150
150
  end
151
151
  end
152
152
 
153
+
154
+ context "a POST request without a content type" do
155
+ before( :each ) do
156
+ @req = @request_factory.post( '/directory/path', '' )
157
+ end
158
+
159
+
160
+ it "responds with a 400 (BAD_REQUEST)" do
161
+ expected_info = {
162
+ status: 400,
163
+ message: "Malformed request (no content type?)",
164
+ headers: {}
165
+ }
166
+
167
+ expect {
168
+ @req.params
169
+ }.to throw_symbol( :finish, expected_info )
170
+ end
171
+ end
172
+
173
+
153
174
  context "a POST request with a 'application/x-www-form-urlencoded' body" do
154
175
 
155
176
  before( :each ) do
@@ -0,0 +1,143 @@
1
+ # -*- rspec -*-
2
+ # vim: set nosta noet ts=4 sw=4:
3
+
4
+ BEGIN {
5
+ require 'pathname'
6
+ basedir = Pathname.new( __FILE__ ).dirname.parent.parent.parent
7
+
8
+ libdir = basedir + "lib"
9
+
10
+ $LOAD_PATH.unshift( basedir ) unless $LOAD_PATH.include?( basedir )
11
+ $LOAD_PATH.unshift( libdir ) unless $LOAD_PATH.include?( libdir )
12
+ }
13
+
14
+ require 'rspec'
15
+
16
+ require 'spec/lib/helpers'
17
+
18
+ require 'strelka'
19
+ require 'strelka/app/sessions'
20
+ require 'strelka/httprequest/session'
21
+ require 'strelka/httpresponse/session'
22
+ require 'strelka/session/default'
23
+
24
+
25
+ #####################################################################
26
+ ### C O N T E X T S
27
+ #####################################################################
28
+
29
+ describe Strelka::HTTPResponse::Session, "-extended response" do
30
+
31
+ before( :all ) do
32
+ setup_logging( :fatal )
33
+ @request_factory = Mongrel2::RequestFactory.new( route: '/service/user' )
34
+ Strelka::App::Sessions.configure( session_class: 'default' )
35
+ end
36
+
37
+ before( :each ) do
38
+ @req = @request_factory.get( '/service/user/estark' )
39
+ @req.extend( Strelka::HTTPRequest::Session )
40
+ @res = @req.response
41
+ @res.extend( described_class )
42
+ end
43
+
44
+ after( :each ) do
45
+ Strelka::Session::Default.sessions.clear
46
+ end
47
+
48
+ after( :all ) do
49
+ reset_logging()
50
+ end
51
+
52
+
53
+ it "has a session_namespace attribute" do
54
+ @res.should respond_to( :session_namespace )
55
+ end
56
+
57
+ it "sets its request's session when its session is set" do
58
+ @res.session = Strelka::Session.create( :default )
59
+ pending "not sure if it should do this or not" do
60
+ @req.session.should be( @res.session )
61
+ end
62
+ end
63
+
64
+ context "for a request with no session ID" do
65
+
66
+ it "knows that it doesn't have a session" do
67
+ @res.should_not have_session()
68
+ end
69
+
70
+ it "doesn't load the session when the session namespace is set" do
71
+ @res.session_namespace = 'an_appid'
72
+ @res.should_not have_session()
73
+ end
74
+
75
+ it "creates a new session as soon as it's accessed" do
76
+ @res.session.should be_a( Strelka::Session::Default )
77
+ end
78
+
79
+ it "sets its session's namespace when it loads if the session_namespace is set" do
80
+ @res.session_namespace = 'an_appid'
81
+ @res.session.namespace.should == :an_appid
82
+ end
83
+
84
+
85
+ context "but with a loaded session object" do
86
+
87
+ before( :each ) do
88
+ @session = Strelka::Session.create( :default )
89
+ @res.request.session = @session
90
+ end
91
+
92
+ it "knows that it has a session" do
93
+ @res.should have_session()
94
+ end
95
+
96
+ it "copies the session from its request when accessed" do
97
+ @res.session.should be( @session )
98
+ end
99
+
100
+ it "sets the session's namespace when its session_namespace is set" do
101
+ @res.session_namespace = 'the_appid'
102
+ @res.session.namespace.should == :the_appid
103
+ end
104
+
105
+ end
106
+
107
+ end
108
+
109
+
110
+ context "for a request with a session ID" do
111
+
112
+ before( :each ) do
113
+ @cookie_name = Strelka::Session::Default.cookie_options[ :name ]
114
+ @sess_id = Strelka::Session::Default.get_session_id
115
+ @req.header.cookie = "#{@cookie_name}=#{@sess_id}"
116
+ end
117
+
118
+ it "knows that it doesn't have a session unless the ID exists" do
119
+ @res.should_not have_session()
120
+ end
121
+
122
+
123
+ context "and a corresponding entry in the database" do
124
+
125
+ before( :each ) do
126
+ Strelka::Session::Default.sessions[ @sess_id ] = {}
127
+ end
128
+
129
+ it "knows that it has a session" do
130
+ @res.should have_session()
131
+ end
132
+
133
+ it "saves the session via itself when told to do so" do
134
+ @res.cookies.should_not include( @cookie_name )
135
+ @res.save_session
136
+ @res.cookies[ @cookie_name ].value.should == @sess_id
137
+ end
138
+
139
+ end
140
+
141
+ end
142
+
143
+ end
@@ -4,7 +4,7 @@
4
4
 
5
5
  BEGIN {
6
6
  require 'pathname'
7
- basedir = Pathname.new( __FILE__ ).dirname.parent.parent.parent
7
+ basedir = Pathname.new( __FILE__ ).dirname.parent.parent
8
8
  $LOAD_PATH.unshift( basedir ) unless $LOAD_PATH.include?( basedir )
9
9
  }
10
10
 
@@ -13,30 +13,36 @@ require 'rspec'
13
13
  require 'spec/lib/helpers'
14
14
 
15
15
  require 'strelka'
16
- require 'strelka/app/plugins'
16
+ require 'strelka/plugins'
17
17
 
18
18
 
19
19
  #####################################################################
20
20
  ### C O N T E X T S
21
21
  #####################################################################
22
22
 
23
- describe Strelka::App::Plugins do
23
+ class Strelka::Pluggable
24
+ extend Strelka::PluginLoader
25
+ end
26
+
27
+
28
+ describe "Strelka plugin system" do
24
29
 
25
30
  before( :all ) do
26
31
  setup_logging( :fatal )
27
- @original_plugin_registry = Strelka::App.loaded_plugins.dup
32
+ @original_registry = Strelka::App.loaded_plugins.dup
28
33
  end
29
34
 
30
- after( :all ) do
35
+ after( :each ) do
31
36
  Strelka::App.loaded_plugins.clear
32
- Strelka::App.loaded_plugins.replace( @original_plugin_registry )
33
- reset_logging()
34
37
  end
35
38
 
36
- after( :each ) do
37
- Strelka::App.loaded_plugins.delete_if {|mod| mod =~ /anonymous/ }
39
+ after( :all ) do
40
+ Strelka::App.loaded_plugins = @original_registry
41
+ reset_logging()
38
42
  end
39
43
 
44
+
45
+
40
46
  RSpec::Matchers.define( :order ) do |item|
41
47
  match do |enumerable|
42
48
  raise "%p doesn't include %p" % [ enumerable, item ] unless
@@ -44,12 +50,12 @@ describe Strelka::App::Plugins do
44
50
  if defined?( @before )
45
51
  raise "%p doesn't include %p" % [ enumerable, @before ] unless
46
52
  enumerable.include?( @before )
47
- enumerable.index( @before ) < enumerable.index( item )
53
+ enumerable.index( @before ) > enumerable.index( item )
48
54
  elsif defined?( @after )
49
55
  raise "%p doesn't include %p" % [ enumerable, @after ] unless
50
56
  enumerable.include?( @after )
51
57
  Strelka.log.debug "Enumerable is: %p" % [ enumerable ]
52
- enumerable.index( @after ) > enumerable.index( item )
58
+ enumerable.index( @after ) < enumerable.index( item )
53
59
  else
54
60
  raise "No .before or .after to compare against!"
55
61
  end
@@ -67,31 +73,25 @@ describe Strelka::App::Plugins do
67
73
 
68
74
  describe "Plugin module" do
69
75
 
70
- it "registers itself with a plugin registry" do
71
- plugin = Module.new do
72
- extend Strelka::App::Plugin
76
+ before( :each ) do
77
+ @plugin = Module.new do
78
+ def self::name; "Strelka::App::TestPlugin"; end
79
+ extend Strelka::Plugin
73
80
  end
74
-
75
- Strelka::App.loaded_plugins.should include( plugin.plugin_name => plugin )
76
81
  end
77
82
 
78
-
79
- it "extends the object even if included" do
80
- plugin = Module.new do
81
- include Strelka::App::Plugin
82
- end
83
-
84
- Strelka::App.loaded_plugins.should include( plugin.plugin_name => plugin )
83
+ it "registers itself with a plugin registry" do
84
+ Strelka::App.loaded_plugins.should include( @plugin.plugin_name => @plugin )
85
85
  end
86
86
 
87
87
 
88
88
  context "that declares that it should run before another" do
89
89
 
90
90
  before( :each ) do
91
- @other_mod = Module.new { include Strelka::App::Plugin }
92
- modname = @other_mod.plugin_name
91
+ modname = @plugin.plugin_name
93
92
  @before_mod = Module.new do
94
- include Strelka::App::Plugin
93
+ def self::name; "Strelka::App::BeforeTestPlugin"; end
94
+ extend Strelka::Plugin
95
95
  run_before( modname )
96
96
  end
97
97
  end
@@ -99,7 +99,7 @@ describe Strelka::App::Plugins do
99
99
 
100
100
  it "sorts before it in the plugin registry" do
101
101
  Strelka::App.loaded_plugins.tsort.
102
- should order( @other_mod.plugin_name ).before( @before_mod.plugin_name )
102
+ should order( @plugin.plugin_name ).after( @before_mod.plugin_name )
103
103
  end
104
104
 
105
105
  end
@@ -107,10 +107,10 @@ describe Strelka::App::Plugins do
107
107
  context "that declares that it should run after another" do
108
108
 
109
109
  before( :each ) do
110
- @other_mod = Module.new { include Strelka::App::Plugin }
111
- modname = @other_mod.plugin_name
110
+ modname = @plugin.plugin_name
112
111
  @after_mod = Module.new do
113
- include Strelka::App::Plugin
112
+ def self::name; "Strelka::App::AfterTestPlugin"; end
113
+ extend Strelka::Plugin
114
114
  run_after( modname )
115
115
  end
116
116
  end
@@ -118,7 +118,7 @@ describe Strelka::App::Plugins do
118
118
 
119
119
  it "sorts after it in the plugin registry" do
120
120
  Strelka::App.loaded_plugins.tsort.
121
- should order( @other_mod.plugin_name ).after( @after_mod.plugin_name )
121
+ should order( @plugin.plugin_name ).before( @after_mod.plugin_name )
122
122
  end
123
123
 
124
124
  end
@@ -129,7 +129,8 @@ describe Strelka::App::Plugins do
129
129
  context "loading" do
130
130
  it "appends class methods if the plugin has them" do
131
131
  plugin = Module.new do
132
- include Strelka::App::Plugin
132
+ def self::name; "Strelka::App::ClassMethodsTestPlugin"; end
133
+ include Strelka::Plugin
133
134
  module ClassMethods
134
135
  def a_class_method; return "yep."; end
135
136
  end
@@ -143,7 +144,8 @@ describe Strelka::App::Plugins do
143
144
 
144
145
  it "adds class-instance variables to the class if the plugin has them" do
145
146
  plugin = Module.new do
146
- include Strelka::App::Plugin
147
+ def self::name; "Strelka::App::ClassInstanceMethodsTestPlugin"; end
148
+ include Strelka::Plugin
147
149
  module ClassMethods
148
150
  @testing_value = :default
149
151
  attr_accessor :testing_value
@@ -163,29 +165,45 @@ describe Strelka::App::Plugins do
163
165
  context "plugin/plugins declarative" do
164
166
 
165
167
  before( :each ) do
166
- @including_class = Class.new { include Strelka::App::Plugins }
168
+ @pluggable_class = Strelka::Pluggable
169
+ @routing_plugin = Module.new do
170
+ def self::name; "Strelka::Pluggable::Routing"; end
171
+ extend Strelka::Plugin
172
+ module ClassMethods
173
+ @routed = false
174
+ attr_reader :routed
175
+ def route_some_stuff
176
+ @routed = true
177
+ end
178
+ end
179
+ end
180
+ @templating_plugin = Module.new do
181
+ def self::name; "Strelka::Pluggable::Templating"; end
182
+ extend Strelka::Plugin
183
+ run_before :routing
184
+ end
167
185
  end
168
186
 
187
+
169
188
  it "can declare a single plugin to load" do
170
- klass = Class.new( @including_class ) do
189
+ klass = Class.new( @pluggable_class ) do
171
190
  plugin :routing
172
191
  end
173
192
  klass.install_plugins
174
193
 
175
- klass.ancestors.should include( Strelka::App::Routing )
194
+ klass.ancestors.should include( @routing_plugin )
176
195
  end
177
196
 
178
197
  it "can declare a list of plugins to load" do
179
- klass = Class.new( @including_class ) do
198
+ klass = Class.new( @pluggable_class ) do
180
199
  plugins :templating, :routing
181
200
  end
182
201
  klass.install_plugins
183
-
184
- klass.ancestors.should include( Strelka::App::Routing, Strelka::App::Templating )
202
+ klass.ancestors.should include( @routing_plugin, @templating_plugin )
185
203
  end
186
204
 
187
205
  it "installs the plugins in the right order even if they're loaded at separate times" do
188
- superclass = Class.new( @including_class ) do
206
+ superclass = Class.new( @pluggable_class ) do
189
207
  plugin :routing
190
208
  end
191
209
  subclass = Class.new( superclass ) do
@@ -193,11 +211,11 @@ describe Strelka::App::Plugins do
193
211
  end
194
212
  subclass.install_plugins
195
213
 
196
- subclass.ancestors.should order( Strelka::App::Templating ).after( Strelka::App::Routing )
214
+ subclass.ancestors.should order( @templating_plugin ).before( @routing_plugin )
197
215
  end
198
216
 
199
- it "adds information about where plugins were installed from when they're installed" do
200
- klass = Class.new( @including_class ) do
217
+ it "adds information about where plugins were installed" do
218
+ klass = Class.new( @pluggable_class ) do
201
219
  plugin :routing
202
220
  end
203
221
  klass.plugins_installed_from.should be_nil()
@@ -211,8 +229,7 @@ describe Strelka::App::Plugins do
211
229
  context "Plugins loaded in a superclass" do
212
230
 
213
231
  before( :each ) do
214
- @base_class = Class.new { include Strelka::App::Plugins }
215
- @superclass = Class.new( @base_class ) do
232
+ @superclass = Class.new( Strelka::Pluggable ) do
216
233
  plugin :routing
217
234
  end
218
235
  end
@@ -220,16 +237,10 @@ describe Strelka::App::Plugins do
220
237
 
221
238
  it "are inherited by subclasses" do
222
239
  subclass = Class.new( @superclass ) do
223
- get 'foom' do |req|
224
- res = req.response
225
- res.puts( "Yep, it worked." )
226
- return res
227
- end
240
+ route_some_stuff
228
241
  end
229
242
 
230
- subclass.routes.should == [
231
- [ :GET, ['foom'], {action: subclass.instance_method(:GET_foom), options: {}} ]
232
- ]
243
+ subclass.routed.should be_true()
233
244
  end
234
245
 
235
246
  end