about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2011-01-21 08:53:56 +0000
committerEric Wong <normalperson@yhbt.net>2011-01-21 08:57:07 +0000
commit2c43727f8e689ef5998d773feb4cbb2f58009391 (patch)
treea1f79f5595c0612e45a045fbf78b91fdd90ffa43
parentd3a182dd0238c964ff5191642d53fce3d2e64be9 (diff)
Since we delegated response_to?, we also need to delegate
method_missing to the response body in case there are
non-standard methods defined outside of Rack.
-rw-r--r--ext/clogger_ext/clogger.c7
-rw-r--r--lib/clogger.rb4
-rw-r--r--test/test_clogger.rb28
3 files changed, 39 insertions, 0 deletions
diff --git a/ext/clogger_ext/clogger.c b/ext/clogger_ext/clogger.c
index 94301fc..c605c1a 100644
--- a/ext/clogger_ext/clogger.c
+++ b/ext/clogger_ext/clogger.c
@@ -926,6 +926,12 @@ static VALUE to_io(VALUE self)
         return io;
 }
 
+/* :nodoc: */
+static VALUE body(VALUE self)
+{
+        return clogger_get(self)->body;
+}
+
 void Init_clogger_ext(void)
 {
         VALUE tmp;
@@ -958,6 +964,7 @@ void Init_clogger_ext(void)
         rb_define_method(cClogger, "to_path", to_path, 0);
         rb_define_method(cClogger, "to_io", to_io, 0);
         rb_define_method(cClogger, "respond_to?", respond_to, 1);
+        rb_define_method(cClogger, "body", body, 0);
         CONST_GLOBAL_STR(REMOTE_ADDR);
         CONST_GLOBAL_STR(HTTP_X_FORWARDED_FOR);
         CONST_GLOBAL_STR(REQUEST_METHOD);
diff --git a/lib/clogger.rb b/lib/clogger.rb
index 1769f48..246381f 100644
--- a/lib/clogger.rb
+++ b/lib/clogger.rb
@@ -140,6 +140,10 @@ private
     end
   end
 
+private
+  def method_missing(*args, &block)
+    body.__send__(*args, &block)
+  end
   # :startdoc:
 end
 
diff --git a/test/test_clogger.rb b/test/test_clogger.rb
index 40c9190..425a4e1 100644
--- a/test/test_clogger.rb
+++ b/test/test_clogger.rb
@@ -697,4 +697,32 @@ class TestClogger < Test::Unit::TestCase
     status, headers, body = cl.call(@req)
     assert %r!\A\d+/\w+/\d{4}:\d\d:\d\d:\d\d \+0000\n\z! =~ s[0], s.inspect
   end
+
+  def test_method_missing
+    s = []
+    body = []
+    def body.foo_bar(foo)
+      [ foo.to_s ]
+    end
+    def body.noargs
+      :hello
+    end
+    def body.omg(&block)
+      yield :PONIES
+    end
+    app = lambda { |env| [200, [], body ] }
+    cl = Clogger.new(app, :logger => s, :format => '$body_bytes_sent')
+    status, headers, body = cl.call(@req)
+    assert_nothing_raised do
+      body.each { |x| s << x }
+      body.close
+    end
+    assert_equal "0\n", s[0], s.inspect
+    assert_kind_of Clogger, body
+    assert_equal %w(1), body.foo_bar(1)
+    assert_equal :hello, body.noargs
+    body.omg { |x| s << x }
+    assert_equal :PONIES, s[1]
+    assert_equal 2, s.size
+  end
 end