about summary refs log tree commit
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2010-11-23 12:51:38 -0800
committerEric Wong <normalperson@yhbt.net>2010-11-23 12:51:54 -0800
commite232eef239952d94220d87ae9be5b27690fe6699 (patch)
tree9ac421eab03cf39852abf6ef372d3e3fdb03004a
parente83353f18f4813365581be81672eaf2e3c2e3d3a (diff)
downloadmetropolis-e232eef239952d94220d87ae9be5b27690fe6699.tar.gz
add generic Rack read/write test
This fixes 404s on HEAD requests which do not return a body.
-rw-r--r--lib/metropolis/common.rb4
-rw-r--r--lib/metropolis/tc/hdb.rb2
-rw-r--r--test/rack_read_write.rb41
-rw-r--r--test/test_tc_hdb.rb9
4 files changed, 48 insertions, 8 deletions
diff --git a/lib/metropolis/common.rb b/lib/metropolis/common.rb
index b86102d..3bfbf77 100644
--- a/lib/metropolis/common.rb
+++ b/lib/metropolis/common.rb
@@ -2,8 +2,8 @@
 module Metropolis::Common
   include Rack::Utils # unescape
 
-  def r(code)
-    body = "#{HTTP_STATUS_CODES[code]}\n"
+  def r(code, body = nil)
+    body ||= "#{HTTP_STATUS_CODES[code]}\n"
     [ code,
       { 'Content-Length' => body.size.to_s, 'Content-Type' => 'text/plain' },
       [ body ] ]
diff --git a/lib/metropolis/tc/hdb.rb b/lib/metropolis/tc/hdb.rb
index 11bc65d..eed46ef 100644
--- a/lib/metropolis/tc/hdb.rb
+++ b/lib/metropolis/tc/hdb.rb
@@ -109,7 +109,7 @@ module Metropolis::TC::HDB
 
   def head(key)
     size = reader(key) { |hdb| hdb.vsiz(key) or ex!(:vsiz, hdb) }
-    0 > size and return r(404)
+    0 > size and return r(404, "")
     [ 200, { 'Content-Length' => size.to_s }.merge!(@headers), [] ]
   end
 
diff --git a/test/rack_read_write.rb b/test/rack_read_write.rb
new file mode 100644
index 0000000..f9b0de2
--- /dev/null
+++ b/test/rack_read_write.rb
@@ -0,0 +1,41 @@
+# -*- encoding: binary -*-
+require 'test/unit'
+require 'stringio'
+require 'tempfile'
+require 'rack'
+
+module TestRackReadWrite
+  def test_rack_read_write
+    app = Metropolis.new(:uri => uri)
+    o = { :lint => true, :fatal => true }
+    req = Rack::MockRequest.new(app)
+
+    r = req.put("/asdf", o.merge(:input=>"ASDF"))
+    assert_equal 201, r.status
+    assert_equal "Created\n", r.body
+
+    r = req.get("/asdf")
+    assert_equal 200, r.status
+    assert_equal "ASDF", r.body
+
+    r = req.request("HEAD", "/asdf", {})
+    assert_equal 200, r.status
+    assert_equal "", r.body
+
+    r = req.delete("/asdf", {})
+    assert_equal 200, r.status
+    assert_equal "OK\n", r.body
+
+    r = req.get("/asdf")
+    assert_equal 404, r.status
+    assert_equal "Not Found\n", r.body
+
+    r = req.delete("/asdf", {})
+    assert_equal 404, r.status
+    assert_equal "Not Found\n", r.body
+
+    r = req.request("HEAD", "/asdf", {})
+    assert_equal 404, r.status
+    assert_equal "", r.body
+  end
+end
diff --git a/test/test_tc_hdb.rb b/test/test_tc_hdb.rb
index 21d89be..0ca7446 100644
--- a/test/test_tc_hdb.rb
+++ b/test/test_tc_hdb.rb
@@ -1,13 +1,12 @@
 # -*- encoding: binary -*-
-require 'test/unit'
-require 'tempfile'
-require 'stringio'
+require './test/rack_read_write.rb'
 require 'tokyocabinet' # FIXME: emits warning with 1.29 gem
 $-w = true
 require 'metropolis'
 
 class Test_TC_HDB < Test::Unit::TestCase
   attr_reader :tmp, :o, :uri
+  include TestRackReadWrite
 
   def setup
     tmp = Tempfile.new('tchdb')
@@ -82,8 +81,8 @@ class Test_TC_HDB < Test::Unit::TestCase
     r = o.head('hellox')
     assert_equal 404, r[0].to_i
     assert_equal 'text/plain', r[1]['Content-Type']
-    assert_equal '10', r[1]['Content-Length']
-    assert_equal "Not Found\n", r[2].join('')
+    assert_equal '0', r[1]['Content-Length']
+    assert_equal "", r[2].join('')
   end
 
   def test_putkeep