test/server.rb in rufus-jig-0.1.21 vs test/server.rb in rufus-jig-0.1.22

- old
+ new

@@ -214,5 +214,56 @@ sleep 7 'later' end + +# +# BASIC AUTH + +helpers do + + def basic_auth_required + + return if authorized? + + response['WWW-Authenticate'] = 'Basic realm="rufus-jig test"' + throw :halt, [ 401, "Not authorized\n" ] + end + + def authorized? + + @auth ||= Rack::Auth::Basic::Request.new(request.env) + @auth.provided? && @auth.basic? && @auth.credentials == [ 'admin', 'nimda' ] + end +end + +get '/protected' do + + basic_auth_required + + content_type 'application/json' + '{ "info": "secretive" }' +end + + +# +# AUTH COUCH +# +# simulating a basic authentified couchdb instance + +get '/tcouch' do + + basic_auth_required + + content_type 'application/json' + '{ "id": "nada" }' +end + +get '/tcouch/_changes' do + + basic_auth_required + + content_type 'application/json' + '{ "id": "x", "deleted": false, "doc": { "hello": "world" }' + "\r\n" +end +