Skip to content

Commit

Permalink
Made all unittests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
ngiger committed Oct 18, 2016
1 parent 1ec86f5 commit bcd80bf
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 99 deletions.
20 changes: 12 additions & 8 deletions lib/sbsm/request.rb
Expand Up @@ -38,10 +38,19 @@ class Request < SimpleDelegator
include DRbUndumped
attr_reader :cgi, :response, :env, :server_name, :trans_handler, :request
attr_accessor :document_root, :uri, :notes, :request_path
def [] key
@env[key]
end
def []= key, value
@env[key] = value
end
alias :store :[]=

def initialize(drb_uri, trans_handler, env, document_root = 'doc')
@cgi = CGI.initialize_without_offline_prompt('html4')
# @request = defined? MiniTest ? Rack::Request.new(Rack::MockRequest.env_for("http://example.com:8080/", {})) : ::Rack::Request.new(env)
@request = ::Rack::Request.new(env)
@env = env
@response = ::Rack::Response.new
@trans_handler = trans_handler
@notes = {}
Expand All @@ -60,7 +69,6 @@ def cookies
unless @request.cookies.eql?(@cgi.cookies)
puts "sbsm/request #{__LINE__}: cookies from cgi #{@cgi.cookies} from request #{@request.cookies} match? #{ @request.cookies.eql?(@cgi.cookies)}"
end
puts "sbsm/request #{__LINE__}: cookies from @request #{@request.cookies}"
@request.cookies
end
def is_crawler?
Expand All @@ -76,6 +84,7 @@ def passthru(path, disposition='attachment')
end
def process
begin
puts "process #{@request.path} #{@request.request_method}"
file_name = File.expand_path(File.join('doc', @request.path))
if File.file?(file_name)
if /css/i.match(File.basename(file_name))
Expand All @@ -89,10 +98,9 @@ def process
@request.params.store('default_flavor', ENV['DEFAULT_FLAVOR'])
@cgi.params.store('default_flavor', ENV['DEFAULT_FLAVOR'])
@request.env.each do |key, val|
next if /^rack\./.match(key)
next if /^rack\./.match(key) && !/^rack\.request/.match(key)
@cgi.params.store(key, val)
end
# puts "sbsm/request #{__LINE__}: @cgi.params are #{@cgi.params}"
drb_process()
@response
ensure
Expand All @@ -102,9 +110,6 @@ def process
def remote_host(lookup_type=Apache::REMOTE_NOLOOKUP)
@request.env['REMOTE_HOST']
end
def uri
return @request.path
end
def unparsed_uri
@request.env['REQUEST_URI']
end
Expand Down Expand Up @@ -190,9 +195,8 @@ def drb_process
end
@session = CGI::Session.new(@cgi, args)
@server_name = @request.env['SERVER_NAME']
puts "sbsm/request #{__LINE__}: servername is #{@server_name} from @request.env and #{@request.env['REQUEST_PATH']}"
# puts "sbsm/request #{__LINE__}: servername is #{@server_name} from @request.env #{@request.request_method} #{@request.env['REQUEST_PATH']}"
@request_path = @request.env['REQUEST_PATH']

@proxy = @session[:proxy]
res = @proxy.drb_process(self)
@response.write(CGI::pretty(res, ' '))
Expand Down
3 changes: 2 additions & 1 deletion lib/sbsm/session.rb
Expand Up @@ -220,7 +220,8 @@ def import_user_input(request)
# attempting to read the cgi-params more than once results in a
# DRbConnectionRefused Exception. Therefore, do it only once...
return if(@user_input_imported)
request.request.params.each do |key, value|
hash = /DRbObject/.match(request.class.to_s) ? request.request.params: request.env
hash.each do |key, value|
next if /^rack\./.match(key)
#puts "importing #{key} -> #{value}"
index = nil
Expand Down
12 changes: 7 additions & 5 deletions lib/sbsm/trans_handler.rb
Expand Up @@ -33,7 +33,8 @@ def config(request)
begin
path = File.expand_path(CONFIG_PATH.untaint) #, request.server.document_root.untaint)
path.untaint
config.update(YAML.load(File.read(path)))
return config unless File.exist?(path.untaint)
config.update(YAML.load(File.read(path.untaint)))
config
rescue StandardError => err
fmt = 'Unable to load url configuration: %s'
Expand All @@ -47,8 +48,8 @@ def handle_shortcut(request, config)
if(params = config['shortcut'][request.request.path])
params.each do |key, val|
request.notes[key.to_s] = val ? val : ''
request.request.update_param(key, val)
end
request.uri = HANDLER_URI
end
end
def simple_parse(uri)
Expand Down Expand Up @@ -100,12 +101,12 @@ def parse_uri(request)
def translate_uri(request)
@@empty_check ||= Regexp.new('^/?$')
@@lang_check ||= Regexp.new('^/[a-z]{2}(/|$)')
request.uri = request.request.path
config = config(request)
handle_shortcut(request, config)
uri = request.request.path
case uri
case request.uri
when @@empty_check
request.uri = config['redirect']['/'] || HANDLER_URI
request.uri = config['redirect']['/'] ||= HANDLER_URI
when @@lang_check
begin
self.parse_uri(request)
Expand All @@ -114,6 +115,7 @@ def translate_uri(request)
end
request.uri = HANDLER_URI
end
# puts "#{__LINE__}: translate_uri #{request.request.path} => #{request.uri}"
0 # Apache::DECLINED
end
def uri_parser(grammar_path=@grammar_path, parser_path=@parser_path)
Expand Down
36 changes: 16 additions & 20 deletions test/test_session.rb
Expand Up @@ -32,7 +32,6 @@
class StubSessionUnknownUser
end
class StubSessionApp
attr_accessor :request
def unknown_user
StubSessionUnknownUser.new
end
Expand All @@ -57,28 +56,11 @@ def error?
false
end
end
class StubSessionRequest < Hash
attr_accessor :unparsed_uri, :request
def params
@request.env
end
def cookies
{}
end
def request_method
'GET'
end
class StubSessionRequest < SBSM::Request
def initialize(path='')
super
super('dummy_drb', nil, {})
@request = Rack::Request.new(Rack::MockRequest.env_for("http://example.com:8080/#{path}", {}))
end
def store key, value
@request.env[key]= value
end
def [] key
@request.env[key]
end
alias :[]= :store
end
class StubSessionView
def http_headers
Expand Down Expand Up @@ -150,6 +132,19 @@ def setup
@request = StubSessionRequest.new
@state = StubSessionState.new(@session, nil)
end
def test_user_input_hash
@request["hash[1]"] = "4"
@request["hash[2]"] = "5"
@request["hash[3]"] = "6"
@session.process(@request)
hash = @session.user_input(:hash)
assert_equal(Hash, hash.class)
assert_equal(3, hash.size)
assert_equal("4", hash["1"])
assert_equal("5", hash["2"])
assert_equal("6", hash["3"])
end
if true
def test_attended_states_store
@session.process(@request)
state = @session.state
Expand Down Expand Up @@ -387,3 +382,4 @@ def test_lookandfeel3
assert_equal('gcc', session.flavor)
end
end
end

0 comments on commit bcd80bf

Please sign in to comment.