Skip to content

Commit

Permalink
rb: Add support for common/basicAuth in specs
Browse files Browse the repository at this point in the history
  • Loading branch information
p0deje committed Mar 1, 2016
1 parent a68b9aa commit 15462b5
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions rb/spec/integration/selenium/webdriver/spec_support/rack_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ def start_windows
end

class TestApp
BASIC_AUTH_CREDENTIALS = %w[test test].freeze

def initialize(file_root)
@static = Rack::File.new(file_root)
end
Expand All @@ -120,16 +122,32 @@ def call(env)
case env['PATH_INFO']
when "/common/upload"
req = Rack::Request.new(env)

status = 200
header = {"Content-Type" => "text/html"}
body = req['upload'][:tempfile].read
body = req['upload'][:tempfile].read

[200, {"Content-Type" => "text/html"}, [body]]
when "/basicAuth"
if authorized?(env)
status = 200
header = {"Content-Type" => "text/html"}
body = "<h1>authorized</h1>"
else
status = 401
header = {"WWW-Authenticate" => 'Basic realm="basic-auth-test"'}
body = "Login please"
end

[status, header, [body]]
else
@static.call env
end
end

private

def authorized?(env)
auth = Rack::Auth::Basic::Request.new(env)
auth.provided? && auth.basic? && auth.credentials && auth.credentials == BASIC_AUTH_CREDENTIALS
end
end

end # RackServer
Expand Down

0 comments on commit 15462b5

Please sign in to comment.