Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

bug #20362 : Items test server was not RESTful enough. Fixed.

  • Loading branch information...
commit 9a7742c3eec0613de6e1ffce746a7c3b91236727 1 parent 53ef8e9
@jmettraux jmettraux authored
Showing with 31 additions and 8 deletions.
  1. +12 −8 test/items.rb
  2. +19 −0 test/simple_test.rb
View
20 test/items.rb
@@ -58,6 +58,7 @@ def clear
class ItemServlet < WEBrick::HTTPServlet::AbstractServlet
@@items = {}
+ @@last_item_id = -1
@@lastmod = LastModifiedHash.new
@@ -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
@@ -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
@@ -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
@@ -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
@@ -172,7 +176,7 @@ def self.flush
def items
@@items
end
-
+
def lastmod
@@lastmod
end
@@ -202,7 +206,7 @@ def fetch_representation (key=nil)
items
end
- [ representation,
+ [ representation,
representation.inspect.hash.to_s,
lastmod.last_modified(key) ]
end
@@ -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
@@ -281,7 +285,7 @@ def initialize (args={})
port = args[:port] || 7777
- #al = [
+ #al = [
# [ "", WEBrick::AccessLog::COMMON_LOG_FORMAT ],
# [ "", WEBrick::AccessLog::REFERER_LOG_FORMAT ]]
@@ -298,7 +302,7 @@ class << @server
@server.mount "/cookie", CookieServlet
[ 'INT', 'TERM' ].each do |signal|
- trap(signal) { shutdown }
+ trap(signal) { shutdown }
end
end
View
19 test/simple_test.rb
@@ -7,6 +7,8 @@
# Sun Jan 13 12:33:03 JST 2008
#
+require 'rubygems'
+
require 'test/unit'
require 'testbase'
@@ -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)
Please sign in to comment.
Something went wrong with that request. Please try again.