Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'rubygems'
- require 'spec'
- require 'sinatra'
- require 'sinatra/test/rspec'
- require 'auth' # from the above snippet
- set :app_file, __FILE__
- set :views, File.join(File.dirname(__FILE__), 'views')
- get '/' do
- "OK"
- end
- get '/sekret' do
- protect!(:realm => "FOO!") { |user, pass| user == "user" and pass == "pass" }
- "SEKRET!"
- end
- describe "app" do
- attr_reader :response
- def get_with_auth(path, user, pass)
- get_it path, :env => {
- 'HTTP_AUTHORIZATION' => 'Basic ' + ["#{user}:#{pass}"].pack("m*")
- }
- end
- it "gets ok" do
- get_it '/'
- response.should be_ok
- end
- it "protects protected" do
- get_it '/sekret'
- response.headers['Www-Authenticate'].should match(/FOO!/)
- response.status.should == 401
- response.body.should_not include("SEKRET!")
- end
- describe "authenticating" do
- it "allows valid requests" do
- get_with_auth '/sekret', 'user', 'pass'
- response.should be_ok
- response.body.should == "SEKRET!"
- end
- it "denies invalid requests" do
- get_with_auth '/sekret', 'wrong', 'pass'
- response.status.should == 401
- response.body.should_not include("SEKRET!")
- end
- end
- end
Add Comment
Please, Sign In to add comment