From 2c43727f8e689ef5998d773feb4cbb2f58009391 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Fri, 21 Jan 2011 08:53:56 +0000 Subject: delegate method_missing calls to the response body 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. --- ext/clogger_ext/clogger.c | 7 +++++++ lib/clogger.rb | 4 ++++ test/test_clogger.rb | 28 ++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+) 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 -- cgit v1.2.3-24-ge0c7