test/items.rb in rufus-verbs-0.2 vs test/items.rb in rufus-verbs-0.3
- old
+ new
@@ -9,10 +9,16 @@
require 'date'
require 'webrick'
+$dcount = 0 # tracking the number of hits when doing digest auth
+
+
+#
+# the hash for the /items resource (collection)
+#
class LastModifiedHash
def initialize
@hash = {}
@@ -50,12 +56,16 @@
# This servlet provides a RESTful in-memory resource "/items".
#
class ItemServlet < WEBrick::HTTPServlet::AbstractServlet
@@items = {}
+
@@lastmod = LastModifiedHash.new
+ @@authenticator = WEBrick::HTTPAuth::DigestAuth.new(
+ :UserDB => WEBrick::HTTPAuth::Htdigest.new('test/test.htdigest'),
+ :Realm => 'test_realm')
def initialize (server, *options)
super
@auth = server.auth
@@ -64,14 +74,22 @@
#
# Overriding the service() method to perform a potential auth check
#
def service (req, res)
- WEBrick::HTTPAuth.basic_auth(req, res, "items") do |u, p|
- (u != nil and u == p)
- end if @auth
+ if @auth == :basic
+ WEBrick::HTTPAuth.basic_auth(req, res, "items") do |u, p|
+ (u != nil and u == p)
+ end
+
+ elsif @auth == :digest
+
+ $dcount += 1
+ @@authenticator.authenticate(req, res)
+ end
+
super
end
def do_GET (req, res)
@@ -164,13 +182,13 @@
since = req['If-Modified-Since']
match = req['If-None-Match']
return true unless since or match
- puts
- p [ since, match ]
- puts
+ #puts
+ #p [ since, match ]
+ #puts
(since or match)
end
#
@@ -216,10 +234,46 @@
"http://localhost:7777/items")
end
end
#
+# testing Rufus::Verbs cookies...
+#
+class CookieServlet < WEBrick::HTTPServlet::AbstractServlet
+
+ @@sessions = {}
+
+ def do_GET (req, res)
+
+ res.body = get_session(req, res).inspect
+ end
+
+ def do_POST (req, res)
+
+ get_session(req, res) << req.body.strip
+ res.body = "ok."
+ end
+
+ protected
+
+ def get_session (req, res)
+
+ c = req.cookies.find { |c| c.name == 'tcookie' }
+
+ if c
+ @@sessions[c.value]
+ else
+ s = []
+ key = (Time.now.to_f * 100000).to_i.to_s
+ @@sessions[key] = s
+ res.cookies << WEBrick::Cookie.new('tcookie', key)
+ s
+ end
+ end
+end
+
+#
# Serving items, a dummy resource...
# Also serving things, which just redirect to items...
#
class ItemServer
@@ -239,9 +293,10 @@
@server.auth = args[:auth]
@server.mount "/items", ItemServlet
@server.mount "/things", ThingServlet
+ @server.mount "/cookie", CookieServlet
[ 'INT', 'TERM' ].each do |signal|
trap(signal) { shutdown }
end
end