sbsm 1.3.5 → 1.3.6

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 098df1b1f3f9be3383dc1adbc44087a8662ac7e7
4
- data.tar.gz: 3836d50af930294d0fa5bdacec1bcc04085880a4
3
+ metadata.gz: 90ce9f2b3daacd1a7a8e0cc6623a41d7fb42cc0d
4
+ data.tar.gz: 269a319d2fc83df3301b1d42ef7ff11e90b455f7
5
5
  SHA512:
6
- metadata.gz: cefada511b938ff19624f587ccc16feca6a33ac34b8875f24d9881c9504bd7e6a87df46bef3492d18b3ed9bb2d409d68c7230f83d5d9f38e060ce20888bdd3af
7
- data.tar.gz: ccf6801e741324e0fc11df4f53a103e43085996a1793d6ad9a5207843440621c1f4fd778aeb3146ca1f2d81e92e2b55cdc9699db0132d2ab95a69e8206efae35
6
+ metadata.gz: 207aaf6069ba9c21809fcd8c033d21efdf44b57ea7ed78bea51a4bbefd0d7cda3e34069f6173a3064b590b33b313a098261d276525aafe9c4e61d5d514afe102
7
+ data.tar.gz: e294f1c2010f4c9655bee932f5a83a7350d4f4a0f94f8baa6518146e905974be052af06bd2733b5889710e07b070930b273929c12c5407beac99851491295730
data/History.txt CHANGED
@@ -1,3 +1,10 @@
1
+ === 1.3.621.3.2017
2
+ * Zeno wants a release when when we still have 10 failing unit tests
3
+ * Made SBSM multithreading optional. Defaults to off
4
+ * Answer with doc_root + '/index.html' if request-path is '/'
5
+ * Introduce RackInterface to separate concerns between SBSM::App which is member variable of the SBSM::Session
6
+ * An the RackInterface which is served as a Rack App object
7
+
1
8
  === 1.3.5 /14.12.2016
2
9
  * Refactored sbsm to work without DRb
3
10
  * Added test/config.ru to be able to run tests via wrk
data/Rakefile CHANGED
@@ -28,10 +28,10 @@ desc "create RDoc documentation"
28
28
 
29
29
  task :rdoc do
30
30
  cmd = "bundle exec rdoc --exclude='/coverage|vendor|test|data|etc|Manifest|.*.lock|.*.css|.*.js|.*.gemspec|.*.patch/' --include=lib" +
31
- " --main=lib/sbsm.rb --title='SBSM: a framework for state based session management'"
31
+ " --main=lib/sbsm.rb --title='SBSM: a framework for state based session management' --op=rdoc"
32
32
  puts cmd
33
33
  res = system(cmd)
34
- puts "Running test/suite.rb returned #{res.inspect}. Output is found in the doc sub-directory"
34
+ puts "Running test/suite.rb returned #{res.inspect}. Output is found in the rdoc sub-directory"
35
35
  exit 1 unless res
36
36
  end
37
37
 
data/lib/sbsm/app.rb CHANGED
@@ -33,11 +33,17 @@ require 'mimemagic'
33
33
 
34
34
  module SBSM
35
35
  ###
36
- # App a base class for Webrick server
36
+ # App as a member of session
37
37
  class App
38
38
 
39
- OPTIONS = [ :app, :config_file, :trans_handler, :validator, :persistence_layer, :server_uri, :unknown_user]
40
- OPTIONS.each{ |opt| eval "attr_reader :#{opt}" }
39
+ def initialize()
40
+ SBSM.info "initialize"
41
+ end
42
+ end
43
+
44
+ class RackInterface
45
+ attr_accessor :session # thread variable!
46
+ SESSION_ID = '_session_id'
41
47
 
42
48
  # Base class for a SBSM based WebRick HTTP server
43
49
  # * offer a call(env) method form handling the WebRick requests
@@ -45,36 +51,41 @@ module SBSM
45
51
  #
46
52
  # === optional arguments
47
53
  #
54
+ # * +app+ - A Ruby class used by the session
48
55
  # * +validator+ - A Ruby class overriding the SBSM::Validator class
49
56
  # * +trans_handler+ - A Ruby class overriding the SBSM::TransHandler class
50
57
  # * +session_class+ - A Ruby class overriding the SBSM::Session class
51
58
  # * +unknown_user+ - A Ruby class overriding the SBSM::UnknownUser class
52
59
  # * +persistence_layer+ - Persistence Layer to use
53
60
  # * +cookie_name+ - The cookie to save persistent user data
61
+ # * +multi_threaded+ - Allow multi_threaded SBSM (default is false)
54
62
  #
55
63
  # === Examples
56
64
  # Look at steinwies.ch
57
65
  # * https://github.com/zdavatz/steinwies.ch (simple, mostly static files, one form, no persistence layer)
58
66
  #
59
- def initialize(validator: nil,
67
+ def initialize(app:,
68
+ validator: nil,
60
69
  trans_handler: nil,
61
70
  session_class: nil,
62
71
  persistence_layer: nil,
63
72
  unknown_user: nil,
64
- cookie_name: nil)
73
+ cookie_name: nil,
74
+ multi_threaded: nil
75
+ )
65
76
  @@last_session = nil
66
- SBSM.info "initialize validator #{validator} th #{trans_handler} cookie #{cookie_name} session #{session_class}"
67
- @session_store = SessionStore.new(persistence_layer: persistence_layer,
77
+ @app = app
78
+ SBSM.info "initialize validator #{validator} th #{trans_handler} cookie #{cookie_name} session #{session_class} app #{app} multi_threaded #{multi_threaded}"
79
+ @session_store = SessionStore.new(app: app,
80
+ persistence_layer: persistence_layer,
68
81
  trans_handler: trans_handler,
69
82
  session_class: session_class,
70
83
  cookie_name: cookie_name,
71
84
  unknown_user: unknown_user,
72
- app: self,
73
- validator: validator)
85
+ validator: validator,
86
+ multi_threaded: multi_threaded)
74
87
  end
75
88
 
76
- SESSION_ID = '_session_id'
77
-
78
89
  def last_session
79
90
  @@last_session
80
91
  end
@@ -87,7 +98,11 @@ module SBSM
87
98
  else
88
99
  session_id = rand((2**(0.size * 8 -2) -1)*10240000000000).to_s(16)
89
100
  end
90
- file_name = File.expand_path(File.join('doc', request.path))
101
+ if '/'.eql?(request.path)
102
+ file_name = File.expand_path(File.join('doc', 'index.html'))
103
+ else
104
+ file_name = File.expand_path(File.join('doc', request.path))
105
+ end
91
106
  if File.file?(file_name)
92
107
  mime_type = MimeMagic.by_extension(File.extname(file_name)).type
93
108
  SBSM.info "file_name is #{file_name} checkin base #{File.basename(file_name)} MIME #{mime_type}"
@@ -97,17 +112,10 @@ module SBSM
97
112
  end
98
113
 
99
114
  return [400, {}, []] if /favicon.ico/i.match(request.path)
100
- # https://www.tutorialspoint.com/ruby/ruby_cgi_sessions.htm
101
- args = {
102
- 'database_manager' => @session_store,
103
- 'session_path' => '/',
104
- @cookie_name => session_id,
105
- }
106
- session = @session_store[session_id]
107
- session.app ||= self
115
+
116
+ Thread.current.thread_variable_set(:session, @session_store[session_id])
117
+ session = Thread.current.thread_variable_get(:session)
108
118
  SBSM.debug "starting session_id #{session_id} session #{session.class} #{request.path}: cookies #{@cookie_name} are #{request.cookies} @cgi #{@cgi.class}"
109
- @cgi = CGI.initialize_without_offline_prompt('html4') unless @cgi
110
- session = CGI::Session.new(@cgi, args) unless session
111
119
  res = session.process_rack(rack_request: request)
112
120
  response.write res
113
121
  response.headers['Content-Type'] ||= 'text/html; charset=utf-8'
@@ -116,10 +124,18 @@ module SBSM
116
124
  response.status = result.last.to_i
117
125
  response.headers.delete(result.first)
118
126
  end
119
- response.set_cookie(session.cookie_name, :value => session.cookie_input)
120
- response.set_cookie(SESSION_ID, :value => session_id)
127
+ session.cookie_input.each do |key, value|
128
+ response.set_cookie(key, { :value => value, :path => '/' })
129
+ end
130
+ response.set_cookie(SESSION_ID, { :value => session_id, :path => '/' }) unless request.cookies[SESSION_ID]
131
+
132
+ # response.set_cookie(SBSM::Session.get_cookie_name, session_id)
121
133
  @@last_session = session
122
- SBSM.debug "finish session_id #{session_id}: header with cookies #{response.headers} from #{session.cookie_input}"
134
+ if response.headers['Set-Cookie'].to_s.index(session_id)
135
+ SBSM.debug "finish session_id.1 #{session_id}: matches response.headers['Set-Cookie']"
136
+ else
137
+ SBSM.debug "finish session_id.2 #{session_id}: headers #{response.headers}"
138
+ end
123
139
  response.finish
124
140
  end
125
141
 
data/lib/sbsm/session.rb CHANGED
@@ -50,6 +50,7 @@ module SBSM
50
50
  MAX_STATES = 4
51
51
  SERVER_NAME = nil
52
52
  UNKNOWN_USER = UnknownUser
53
+ @@mutex = Mutex.new
53
54
  def Session.reset_stats
54
55
  @@stats = {}
55
56
  end
@@ -106,8 +107,9 @@ module SBSM
106
107
  trans_handler: nil,
107
108
  validator: nil,
108
109
  unknown_user: nil,
109
- cookie_name: nil)
110
- SBSM.info "initialize th #{trans_handler} validator #{validator}"
110
+ cookie_name: nil,
111
+ multi_threaded: false)
112
+ SBSM.info "initialize th #{trans_handler} validator #{validator} app #{app.class}"
111
113
  @app = app
112
114
  @unknown_user = unknown_user
113
115
  @unknown_user ||= self.class::UNKNOWN_USER
@@ -119,6 +121,7 @@ module SBSM
119
121
  fail "invalid trans_handler #{@trans_handler}" unless @trans_handler.is_a?(SBSM::TransHandler)
120
122
  @cookie_name = cookie_name
121
123
  @cookie_name ||= self.class::PERSISTENT_COOKIE_NAME
124
+ @@cookie_name = @cookie_name
122
125
  @attended_states = {}
123
126
  @persistent_user_input = {}
124
127
  touch()
@@ -128,9 +131,17 @@ module SBSM
128
131
  @unknown_user_class
129
132
  @unknown_user_class = @unknown_user.class
130
133
  @variables = {}
131
- @mutex = Mutex.new
132
134
  @cgi = CGI.initialize_without_offline_prompt('html4')
133
- SBSM.debug "session initialized #{self} with @cgi #{@cgi}"
135
+ @multi_threaded = multi_threaded
136
+ @mutex = multi_threaded ? Mutex.new: @@mutex
137
+ @active_thread = nil
138
+ SBSM.debug "session initialized #{self} with @cgi #{@cgi} multi_threaded #{multi_threaded} app #{app.object_id}"
139
+ end
140
+ def self.get_cookie_name
141
+ @@cookie_name
142
+ end
143
+ def method_missing(symbol, *args, &block) # Replaces old dispatch to DRb
144
+ @app.send(symbol, *args, &block)
134
145
  end
135
146
  def unknown_user
136
147
  @unknown_user_class.new
@@ -159,7 +170,6 @@ module SBSM
159
170
  @persistent_user_input.store(:language, lang)
160
171
  @valid_input.clear
161
172
  @unsafe_input.clear
162
- @active_thread = nil
163
173
  true
164
174
  end
165
175
  @@msie_ptrn = /MSIE/
@@ -259,17 +269,17 @@ module SBSM
259
269
  age(now) > EXPIRES
260
270
  end
261
271
  def force_login(user)
262
- binding.pry
263
272
  @user = user
264
273
  end
265
274
  def import_cookies(request)
266
275
  reset_cookie()
267
276
  if(cuki_str = request.cookies[self::class::PERSISTENT_COOKIE_NAME])
268
277
  SBSM.debug "cuki_str #{self::class::PERSISTENT_COOKIE_NAME} #{cuki_str}"
269
- eval(cuki_str).each { |key, val|
270
- valid = @validator.validate(key, val)
271
- @cookie_input.store(key, valid)
272
- }
278
+ request.cookies.each do |key, val|
279
+ key_sym = key.intern
280
+ valid = @validator.validate(key_sym, val)
281
+ @cookie_input.store(key_sym, valid) if valid
282
+ end
273
283
  SBSM.debug "@cookie_input now #{@cookie_input}"
274
284
  end
275
285
  end
@@ -281,7 +291,7 @@ module SBSM
281
291
  hash = rack_req.env.merge rack_req.params
282
292
  hash.merge! rack_req.POST if rack_req.POST
283
293
  hash.delete('rack.request.form_hash')
284
- SBSM.debug "hash has #{hash.size } items #{hash.keys}"
294
+ # SBSM.debug "hash has #{hash.size } items #{hash.keys}"
285
295
  hash.each do |key, value|
286
296
  next if /^rack\./.match(key)
287
297
  index = nil
@@ -56,13 +56,15 @@ module SBSM
56
56
  session_class: nil,
57
57
  validator: nil,
58
58
  cookie_name: nil,
59
- unknown_user: nil)
59
+ unknown_user: nil,
60
+ multi_threaded: nil)
60
61
  fail "You must specify an app!" unless app
61
62
  @sessions = {}
62
63
  @mutex = Mutex.new
63
64
  @cleaner = run_cleaner if(self.class.const_get(:RUN_CLEANER))
64
65
  @admin_threads = ThreadGroup.new
65
66
  @async = ThreadGroup.new
67
+ @app = app
66
68
  @system = persistence_layer
67
69
  @persistence_layer = persistence_layer
68
70
  @cookie_name = cookie_name
data/lib/sbsm/state.rb CHANGED
@@ -124,7 +124,6 @@ module SBSM
124
124
  end
125
125
  end
126
126
  def extend(mod)
127
- binding.pry
128
127
  if(mod.constants.include?(:VIRAL))
129
128
  @viral_modules.push(mod)
130
129
  end
data/lib/sbsm/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module SBSM
2
- VERSION = '1.3.5'
2
+ VERSION = '1.3.6'
3
3
  end
data/test/simple_sbsm.rb CHANGED
@@ -86,7 +86,7 @@ module Demo
86
86
  super(session, user)
87
87
  end
88
88
  def http_headers
89
- {
89
+ {
90
90
  'Status' => '303 See Other',
91
91
  'Location' => 'feedback',
92
92
  }
@@ -218,14 +218,25 @@ module Demo
218
218
  DEFAULT_STATE = HomeState
219
219
  end
220
220
 
221
- class SimpleSBSM < SBSM::App
222
- SESSION = Session
223
- def initialize(cookie_name: nil)
221
+ class SimpleSBSM < SBSM::RackInterface
222
+ def initialize
224
223
  SBSM.info "SimpleSBSM.new"
225
- super(validator: Validator.new,
226
- trans_handler: SBSM::TransHandler.instance,
227
- session_class: SESSION,
228
- cookie_name: cookie_name)
224
+ super(app: self)
225
+ end
226
+ end
227
+ class SimpleRackInterface < SBSM::RackInterface
228
+ SESSION = Session
229
+
230
+ def initialize(validator: SBSM::Validator.new,
231
+ trans_handler: SBSM::TransHandler.instance,
232
+ cookie_name: nil,
233
+ session_class: SESSION)
234
+ SBSM.info "SimpleRackInterface.new SESSION #{SESSION}"
235
+ super(app: SimpleSBSM,
236
+ validator: validator,
237
+ trans_handler: trans_handler,
238
+ cookie_name: cookie_name,
239
+ session_class: session_class)
229
240
  end
230
241
  end
231
242
  end
@@ -13,66 +13,55 @@ require 'sbsm/session'
13
13
  require 'simple_sbsm'
14
14
  require 'nokogiri'
15
15
 
16
- RUN_ALL_TESTS=true
16
+ RUN_ALL_TESTS=true unless defined?(RUN_ALL_TESTS)
17
+
17
18
  # Here we test, whether setting various class constant have the desired effect
18
19
 
19
20
  class AppVariantTest < Minitest::Test
20
21
  include Rack::Test::Methods
21
22
  attr_reader :app
22
23
  def setup
23
- @app = Demo::SimpleSBSM.new(cookie_name: TEST_COOKIE_NAME)
24
+ # @app = Demo::SimpleSBSM.new(cookie_name: TEST_COOKIE_NAME)
25
+ @app = Demo::SimpleRackInterface.new
24
26
  end
25
27
  def test_post_feedback
26
28
  get '/de/page' do # needed to set cookie
27
29
  last_response.set_cookie(TEST_COOKIE_NAME, :value => Hash.new('anrede' => 'value2'))
28
30
  end
31
+ clear_cookies
32
+ set_cookie 'anrede=value2'
33
+ set_cookie "_session_id=#{TEST_COOKIE_NAME}"
29
34
  get '/de/page/feedback' do
30
35
  end
31
- assert_equal ["_session_id", TEST_COOKIE_NAME], last_request.cookies.keys
32
- skip "Cannot content for test cookie_input"
33
- assert_equal ['anrede', 'name'], @app.cookie_input.keys
34
- assert_equal 'xxx', @app.persistent_user_input(:anrede)
35
-
36
- assert_equal ['value2', 'value3'], @app.cookie_input.values
37
- assert_match /anrede=value2/, CGI.unescape(last_response.headers['Set-Cookie'])
36
+ assert_equal ["_session_id", 'anrede'], last_request.cookies.keys
37
+ expected = {"_session_id"=>"test-cookie", "anrede"=>"value2"}
38
+ assert_equal expected, last_request.cookies
39
+ assert_equal 'value2', @app.last_session.persistent_user_input('anrede')
38
40
  end
39
41
  end if RUN_ALL_TESTS
40
42
 
41
- class AppTest < Minitest::Test
43
+ class AppTestSimple < Minitest::Test
42
44
  include Rack::Test::Methods
43
45
  attr_reader :app
44
46
 
45
47
  def setup
46
- @app = Demo::SimpleSBSM.new
48
+ @app = Demo::SimpleRackInterface.new
47
49
  end
48
-
49
- def test_session_redirect
50
- get '/de/page/redirect'
51
- assert_equal 303,last_response.status
52
- assert_equal 'feedback',last_response.headers['Location']
53
- assert_match REDIRECT_HTML_CONTENT, last_response.body
54
- assert_match /utf-8/i, last_response.headers['Content-Type']
55
- end if RUN_ALL_TESTS
56
50
  def test_post_feedback
57
- get '/de/page' do # needed to set cookie
58
- last_response.set_cookie(SBSM::Session::PERSISTENT_COOKIE_NAME, :value => Hash.new('anrede' => 'value2', 'name' => 'values'))
59
- end
51
+ set_cookie "_session_id=#{TEST_COOKIE_NAME}"
52
+ set_cookie "#{SBSM::Session::PERSISTENT_COOKIE_NAME}=dummy"
60
53
  get '/de/page/feedback' do
61
54
  end
62
55
  # assert_match /anrede.*=.*value2/, CGI.unescape(last_response.headers['Set-Cookie'])
63
56
  assert last_response.ok?
64
57
  assert_equal ["_session_id", SBSM::Session::PERSISTENT_COOKIE_NAME], last_request.cookies.keys
65
- skip "Cannot test cookie_input"
66
- assert_match /anrede.*=.*value2/, CGI.unescape(last_response.headers['Set-Cookie'])
67
- assert_match FEEDBACK_HTML_CONTENT, last_response.body
68
- assert_equal ['anrede'], @app.cookie_input.keys
69
- assert_equal ['value2'], @app.cookie_input.values
70
- assert_equal ['anrede', 'name'], @app.cookie_input.keys
71
- assert_equal ['value2', 'value3'], @app.cookie_input.values
72
- page = Nokogiri::HTML(last_response.body)
73
- x = page.css('div')
74
- skip 'We must add here an input form or we cannot continue testing'
58
+ assert_equal(FEEDBACK_HTML_CONTENT, last_response.body)
75
59
 
60
+ set_cookie "anrede=Herr"
61
+ post '/de/page/feedback', { anrede: 'Herr', msg: 'SBSM rocks!', state_id: '1245'} do
62
+ end
63
+ page = Nokogiri::HTML(last_response.body)
64
+ puts page.text
76
65
  assert page.css('input').find{|x| x.attributes['name'].value.eql?('state_id') }.attributes['value'].value
77
66
  state_id = page.css('input').find{|x| x.attributes['name'].value.eql?('state_id') }.attributes['value'].value.to_i
78
67
  assert state_id > 0
@@ -93,14 +82,6 @@ if RUN_ALL_TESTS
93
82
  assert_match /utf-8/i, last_response.headers['Content-Type']
94
83
  end
95
84
 
96
- def test_session_redirect
97
- get '/de/page/redirect'
98
- assert_equal 303,last_response.status
99
- assert_equal 'feedback',last_response.headers['Location']
100
- assert_match REDIRECT_HTML_CONTENT, last_response.body
101
- assert_match /utf-8/i, last_response.headers['Content-Type']
102
- end
103
-
104
85
  def test_css_file
105
86
  css_content = "html { max-width: 960px; margin: 0 auto; }"
106
87
  css_file = File.join('doc/sbsm.css')
@@ -184,4 +165,14 @@ if RUN_ALL_TESTS
184
165
  ::SBSM::Session.show_stats '/de/page'
185
166
  end if RUN_ALL_TESTS
186
167
  end
168
+ def test_session_home_then_fr_about
169
+ puts 888
170
+ get '/home'
171
+ assert last_response.ok?
172
+ assert_match /^request_path is \/home$/, last_response.body
173
+ assert_match HOME_HTML_CONTENT, last_response.body
174
+ get '/fr/page/about'
175
+ assert last_response.ok?
176
+ assert_match ABOUT_HTML_CONTENT, last_response.body
177
+ end
187
178
  end
@@ -12,7 +12,7 @@ require 'simple_sbsm'
12
12
 
13
13
  ENV['RACK_ENV'] = 'test'
14
14
  ENV['REQUEST_METHOD'] = 'GET'
15
- RUN_ALL_TESTS = true
15
+ RUN_ALL_TESTS=true unless defined?(RUN_ALL_TESTS)
16
16
 
17
17
  # Overriding some stuff from the simple_sbsm
18
18
  module Demo
@@ -106,21 +106,28 @@ module Demo
106
106
  defined?(@@login_visited) ? @@login_visited : :not_initialized
107
107
  end
108
108
  end
109
-
110
109
  class CustomizedSBSM < SBSM::App
110
+ def initialize
111
+ SBSM.info "CustomizedSBSM.new"
112
+ end
113
+ end
114
+
115
+ class CustomizedRackInterface < SBSM::RackInterface
111
116
  SESSION = CustomizedSession
117
+
112
118
  def initialize(validator: CustomizedValidator.new,
113
119
  trans_handler: SBSM::TransHandler.instance,
114
120
  cookie_name: nil,
115
121
  session_class: SESSION)
116
- SBSM.info "CustomizedSBSM.new SESSION #{SESSION}"
117
-
118
- super(validator: validator,
122
+ SBSM.info "CustomizedRackInterface.new SESSION #{SESSION}"
123
+ super(app: CustomizedSBSM.new,
124
+ validator: validator,
119
125
  trans_handler: trans_handler,
120
126
  cookie_name: cookie_name,
121
127
  session_class: session_class)
122
128
  end
123
129
  end
130
+
124
131
  end
125
132
 
126
133
  class CustomizedAppInvalidValidator < Minitest::Test
@@ -128,12 +135,12 @@ class CustomizedAppInvalidValidator < Minitest::Test
128
135
  attr_reader :app
129
136
 
130
137
  def test_raise_exeption_if_class
131
- @app = Demo::CustomizedSBSM.new(validator: ::SBSM::Validator)
138
+ @app = Demo::CustomizedRackInterface.new(validator: ::SBSM::Validator)
132
139
  assert_raises { get '/' do end }
133
140
  end
134
141
 
135
142
  def test_valid_if_validator
136
- @app = Demo::CustomizedSBSM.new(validator: ::SBSM::Validator.new)
143
+ @app = Demo::CustomizedRackInterface.new(validator: ::SBSM::Validator.new)
137
144
  get '/' do end
138
145
  end
139
146
  end if RUN_ALL_TESTS
@@ -143,12 +150,12 @@ class CustomizedAppInvalidTranshandler < Minitest::Test
143
150
  attr_reader :app
144
151
 
145
152
  def test_raise_exeption_if_class
146
- @app = Demo::CustomizedSBSM.new(trans_handler: ::SBSM::TransHandler)
153
+ @app = Demo::CustomizedRackInterface.new(trans_handler: ::SBSM::TransHandler)
147
154
  assert_raises { get '/' do end }
148
155
  end
149
156
 
150
157
  def test_valid_if_validator_instance
151
- @app = Demo::CustomizedSBSM.new(trans_handler: ::SBSM::TransHandler.instance)
158
+ @app = Demo::CustomizedRackInterface.new(trans_handler: ::SBSM::TransHandler.instance)
152
159
  get '/' do end
153
160
  end
154
161
  end if RUN_ALL_TESTS
@@ -157,7 +164,7 @@ class CustomizedAppSessionValidatorLnf < Minitest::Test
157
164
  include Rack::Test::Methods
158
165
  attr_reader :app
159
166
  def setup
160
- @app = Demo::CustomizedSBSM.new
167
+ @app = Demo::CustomizedRackInterface.new
161
168
  end
162
169
 
163
170
  def test_customized_active_state
@@ -207,12 +214,15 @@ class CustomizedAppSessionValidatorLnf < Minitest::Test
207
214
  end
208
215
 
209
216
  def test_customized_cookie_name
210
- @app = Demo::CustomizedSBSM.new(cookie_name: TEST_COOKIE_NAME)
217
+ my_cookey_name = 'my-cookie-name'
218
+ @app = Demo::CustomizedRackInterface.new(cookie_name: my_cookey_name)
211
219
  get '/' do
212
220
  end
213
221
  assert last_response.ok?
214
222
  # TEST_COOKIE_NAME set via param to app
215
- assert_equal TEST_COOKIE_NAME, last_response.get_header('Set-Cookie').split("\n").first.split('=').first
223
+ cookie = last_response.get_header('Set-Cookie').split("\n").find_all{|x| x.index(my_cookey_name)}
224
+ assert_equal 1, cookie.size
225
+ assert_match my_cookey_name, cookie.first
216
226
  end if RUN_ALL_TESTS
217
227
  end
218
228
 
@@ -220,13 +230,16 @@ class CustomizedAppCookieName < Minitest::Test
220
230
  include Rack::Test::Methods
221
231
  attr_reader :app
222
232
  def setup
223
- @app = SBSM::App.new(cookie_name: Demo::DEMO_PERSISTENT_COOKIE_NAME)
233
+ @sbsm_app = SBSM::App.new
234
+ @app = SBSM::RackInterface.new(app: @sbsm_app, cookie_name: Demo::DEMO_PERSISTENT_COOKIE_NAME)
224
235
  end
225
236
  def test_customized_cookie_name
226
237
  get '/' do
227
238
  end
228
239
  assert last_response.ok?
229
- assert_equal Demo::DEMO_PERSISTENT_COOKIE_NAME, last_response.get_header('Set-Cookie').split("\n").first.split('=').first
240
+ cookie = last_response.get_header('Set-Cookie').split("\n").find_all{|x| x.index(Demo::DEMO_PERSISTENT_COOKIE_NAME)}
241
+ assert_equal 1, cookie.size
242
+ assert_match Demo::DEMO_PERSISTENT_COOKIE_NAME, cookie.first
230
243
  end if RUN_ALL_TESTS
231
244
  end
232
245
 
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+ $: << File.dirname(__FILE__)
4
+ $:.unshift File.expand_path('../lib', File.dirname(__FILE__))
5
+
6
+ ENV['RACK_ENV'] = 'test'
7
+ ENV['REQUEST_METHOD'] = 'GET'
8
+
9
+ require 'minitest/autorun'
10
+ require 'rack/test'
11
+ require 'sbsm/app'
12
+ require 'sbsm/session'
13
+ require 'simple_sbsm'
14
+ require 'nokogiri'
15
+
16
+ RUN_ALL_TESTS=true unless defined?(RUN_ALL_TESTS)
17
+
18
+ # Here we test, whether setting various class constant have the desired effect
19
+
20
+ class AppTestRedirect < Minitest::Test
21
+ include Rack::Test::Methods
22
+ attr_reader :app
23
+
24
+ def setup
25
+ @app = Demo::SimpleRackInterface.new
26
+ end
27
+ def test_session_redirect
28
+ get '/de/page/redirect'
29
+ assert_equal 303,last_response.status
30
+ assert_equal 'feedback',last_response.headers['Location']
31
+ assert_match REDIRECT_HTML_CONTENT, last_response.body
32
+ assert_match /utf-8/i, last_response.headers['Content-Type']
33
+ end
34
+
35
+ end
data/test/test_session.rb CHANGED
@@ -46,10 +46,7 @@ class StubSessionApp < SBSM::App
46
46
  attr_accessor :trans_handler, :validator
47
47
  SESSION = StubSessionSession
48
48
  def initialize(args = {})
49
- args[:session_class] ||= StubSessionSession
50
- args[:validator] ||= StubSessionValidator.new
51
- args[:unknown_user] ||= StubSessionUnknownUser.new
52
- super(args)
49
+ super()
53
50
  end
54
51
  def login(session)
55
52
  false
@@ -136,10 +133,7 @@ class StubSessionSession < SBSM::Session
136
133
  'sbb' => 'bbs',
137
134
  }
138
135
  def initialize(app: app)
139
- args = { :app => app}
140
- args[:app] ||= StubSessionApp.new
141
- args[:validator] ||= StubSessionValidator.new
142
- super(args)
136
+ super(app: app, validator: StubSessionValidator.new)
143
137
  persistent_user_input = {}
144
138
  end
145
139
  def persistent_user_input(key)
@@ -155,10 +149,24 @@ class TestSession < Minitest::Test
155
149
  @session = StubSessionWithView.new(app: @app,
156
150
  validator: StubSessionValidator.new,
157
151
  unknown_user: StubSessionUnknownUser.new)
158
- @request = StubSessionRequest.new
152
+ @request = StubSessionRequest.new
159
153
  @state = StubSessionState.new(@session, nil)
160
154
  end
161
- if true
155
+
156
+ def test_cookies
157
+ c_name = SBSM::Session::PERSISTENT_COOKIE_NAME
158
+ c_value = "remember=63488f94c90813200f29e1a60de9a479ad52e71758f48e612e9f6390f80c7b7c\nname=juerg%40davaz.com\nlanguage=en"
159
+ @request.cookies[:remember] = 'my_remember_value'
160
+ @request.cookies[:language] = 'en'
161
+ @request.cookies['_session_id'] = '10e524151d7f0da819f4222ecc1'
162
+ @request.cookies[c_name] = 'my_cookie_id'
163
+ @request.set_header('Set-Cookie', c_value)
164
+ @session.process_rack(rack_request: @request)
165
+ assert_equal([:remember, :language, :_session_id, c_name.to_sym], @session.cookie_input.keys)
166
+ skip "Don't know how to test persistent_user_input"
167
+ assert_equal('@session.valid_input', @session.persistent_user_input(:language))
168
+ assert_equal('@session.valid_input', @session.valid_input)
169
+ end
162
170
  def test_user_input
163
171
  @request["foo"] = "bar"
164
172
  @request["baz"] = "zuv"
@@ -420,4 +428,3 @@ if true
420
428
  assert_equal('gcc', session.flavor)
421
429
  end
422
430
  end
423
- end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sbsm
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.5
4
+ version: 1.3.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masaomi Hatakeyama, Zeno R.R. Davatz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-14 00:00:00.000000000 Z
11
+ date: 2017-03-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -290,7 +290,6 @@ files:
290
290
  - lib/sbsm/version.rb
291
291
  - lib/sbsm/viralstate.rb
292
292
  - sbsm.gemspec
293
- - test/config.ru
294
293
  - test/data/dos_file.txt
295
294
  - test/data/lnf_file.txt
296
295
  - test/data/mac_file.txt
@@ -302,6 +301,7 @@ files:
302
301
  - test/test_index.rb
303
302
  - test/test_logger.rb
304
303
  - test/test_lookandfeel.rb
304
+ - test/test_redirect.rb
305
305
  - test/test_session.rb
306
306
  - test/test_state.rb
307
307
  - test/test_trans_handler.rb
@@ -329,12 +329,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
329
329
  version: '0'
330
330
  requirements: []
331
331
  rubyforge_project:
332
- rubygems_version: 2.5.1
332
+ rubygems_version: 2.6.8
333
333
  signing_key:
334
334
  specification_version: 4
335
335
  summary: Application framework for state based session management from ywesee
336
336
  test_files:
337
- - test/config.ru
338
337
  - test/data/dos_file.txt
339
338
  - test/data/lnf_file.txt
340
339
  - test/data/mac_file.txt
@@ -346,6 +345,7 @@ test_files:
346
345
  - test/test_index.rb
347
346
  - test/test_logger.rb
348
347
  - test/test_lookandfeel.rb
348
+ - test/test_redirect.rb
349
349
  - test/test_session.rb
350
350
  - test/test_state.rb
351
351
  - test/test_trans_handler.rb
data/test/config.ru DELETED
@@ -1,11 +0,0 @@
1
- #\ -w -p 6789
2
- # 6789 must be in sync with TEST_APP_URI from test_application.rb
3
- root_dir = File.expand_path(File.join(__FILE__, '..', '..'))
4
- $LOAD_PATH << File.join(root_dir, 'test')
5
- require 'simple_sbsm'
6
- use Rack::CommonLogger, TEST_LOGGER
7
- use Rack::Reloader, 0
8
- use Rack::ContentLength
9
- use(Rack::Static, urls: ["/doc/"])
10
- app = Rack::ShowExceptions.new(Rack::Lint.new(Demo::SimpleSBSM.new(cookie_name: ::TEST_COOKIE_NAME)))
11
- run app