Skip to content

Commit

Permalink
bug #20362 : Items test server was not RESTful enough. Fixed.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmettraux committed May 26, 2008
1 parent 53ef8e9 commit 9a7742c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
20 changes: 12 additions & 8 deletions test/items.rb
Expand Up @@ -58,6 +58,7 @@ def clear
class ItemServlet < WEBrick::HTTPServlet::AbstractServlet

@@items = {}
@@last_item_id = -1

@@lastmod = LastModifiedHash.new

Expand All @@ -80,7 +81,7 @@ def service (req, res)

WEBrick::HTTPAuth.basic_auth(req, res, "items") do |u, p|
(u != nil and u == p)
end
end

elsif @auth == :digest

Expand Down Expand Up @@ -127,6 +128,9 @@ def do_POST (req, res)
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

Expand All @@ -138,7 +142,7 @@ 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
Expand All @@ -150,7 +154,7 @@ 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
Expand All @@ -172,7 +176,7 @@ def self.flush
def items
@@items
end

def lastmod
@@lastmod
end
Expand Down Expand Up @@ -202,7 +206,7 @@ def fetch_representation (key=nil)
items
end

[ representation,
[ representation,
representation.inspect.hash.to_s,
lastmod.last_modified(key) ]
end
Expand Down Expand Up @@ -230,7 +234,7 @@ 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
Expand Down Expand Up @@ -281,7 +285,7 @@ def initialize (args={})

port = args[:port] || 7777

#al = [
#al = [
# [ "", WEBrick::AccessLog::COMMON_LOG_FORMAT ],
# [ "", WEBrick::AccessLog::REFERER_LOG_FORMAT ]]

Expand All @@ -298,7 +302,7 @@ class << @server
@server.mount "/cookie", CookieServlet

[ 'INT', 'TERM' ].each do |signal|
trap(signal) { shutdown }
trap(signal) { shutdown }
end
end

Expand Down
19 changes: 19 additions & 0 deletions test/simple_test.rb
Expand Up @@ -7,6 +7,8 @@
# Sun Jan 13 12:33:03 JST 2008
#

require 'rubygems'

require 'test/unit'
require 'testbase'

Expand Down Expand Up @@ -47,6 +49,23 @@ def test_0
expect 200, { 0 => "Toto3" }, get(:uri => "http://localhost:7777/items")
end

def test_0b

expect 200, {}, get(:uri => "http://localhost:7777/items")

res = post :uri => "http://localhost:7777/items", :d => "Toto"
assert_equal 201, res.code.to_i
assert_equal "http://localhost:7777/items/0", res['Location']

expect 200, "\"Toto\"", get(:uri => "http://localhost:7777/items/0")

res = post :uri => "http://localhost:7777/items", :d => "Smurf"
assert_equal 201, res.code.to_i
assert_equal "http://localhost:7777/items/1", res['Location']

expect 200, "\"Smurf\"", get(:uri => "http://localhost:7777/items/1")
end

def test_1

ep = EndPoint.new(:host => "localhost", :port => 7777)
Expand Down

0 comments on commit 9a7742c

Please sign in to comment.