test/items.rb in rufus-verbs-0.9 vs test/items.rb in rufus-verbs-0.10

- old
+ new

@@ -56,10 +56,11 @@ # This servlet provides a RESTful in-memory resource "/items". # class ItemServlet < WEBrick::HTTPServlet::AbstractServlet @@items = {} + @@last_item_id = -1 @@lastmod = LastModifiedHash.new @@authenticator = WEBrick::HTTPAuth::DigestAuth.new( :UserDB => WEBrick::HTTPAuth::Htdigest.new('test/test.htdigest'), @@ -78,11 +79,11 @@ if @auth == :basic WEBrick::HTTPAuth.basic_auth(req, res, "items") do |u, p| (u != nil and u == p) - end + end elsif @auth == :digest $dcount += 1 @@authenticator.authenticate(req, res) @@ -125,10 +126,13 @@ m = m.downcase if m return do_PUT(req, res) if m == 'put' return do_DELETE(req, res) if m == 'delete' i = item_id req + + i = (@@last_item_id += 1) unless i + items[i] = req.body lastmod.touch i res['Location'] = "#{@host}/items/#{i}" reply res, 201, "item created" @@ -136,11 +140,11 @@ def do_PUT (req, res) i = item_id req - return reply(res, 404, "no item '#{i}'") unless items[i] + return reply(res, 404, "no item '#{i}'") unless items[i] items[i] = req.body lastmod.touch i reply res, 200, "item updated" @@ -148,11 +152,11 @@ def do_DELETE (req, res) i = item_id req - return reply(res, 404, "no item '#{i}'") unless items[i] + return reply(res, 404, "no item '#{i}'") unless items[i] items.delete i lastmod.delete i reply res, 200, "item deleted" @@ -170,11 +174,11 @@ protected def items @@items end - + def lastmod @@lastmod end def is_modified (req, key) @@ -200,11 +204,11 @@ items[key] else items end - [ representation, + [ representation, representation.inspect.hash.to_s, lastmod.last_modified(key) ] end def reply (res, code, message) @@ -228,11 +232,11 @@ class ThingServlet < WEBrick::HTTPServlet::AbstractServlet def do_GET (req, res) res.set_redirect( - WEBrick::HTTPStatus[303], + WEBrick::HTTPStatus[303], "http://localhost:7777/items") end end # @@ -270,20 +274,31 @@ end end end # +# a servlet that doesn't reply (for timeout testing) +# +class LostServlet < WEBrick::HTTPServlet::AbstractServlet + + def do_GET (req, res) + + sleep 200 + end +end + +# # Serving items, a dummy resource... # Also serving things, which just redirect to items... # class ItemServer def initialize (args={}) port = args[:port] || 7777 - #al = [ + #al = [ # [ "", WEBrick::AccessLog::COMMON_LOG_FORMAT ], # [ "", WEBrick::AccessLog::REFERER_LOG_FORMAT ]] @server = WEBrick::HTTPServer.new :Port => port, :AccessLog => nil @@ -294,12 +309,13 @@ @server.auth = args[:auth] @server.mount "/items", ItemServlet @server.mount "/things", ThingServlet @server.mount "/cookie", CookieServlet + @server.mount "/lost", LostServlet [ 'INT', 'TERM' ].each do |signal| - trap(signal) { shutdown } + trap(signal) { shutdown } end end def start