From ede98dadfdcb6def9a2fc1ba518f871d8cf426a2 Mon Sep 17 00:00:00 2001 From: jc00ke Date: Sun, 29 Apr 2012 12:18:21 -0700 Subject: [PATCH] Return valid Rack responses from status server Use Rack::Lint to check the response is valid. Existing code really just needed Content-Length & Content-Type headers. --- History.txt | 11 +++++++++++ lib/puma/app/status.rb | 20 +++++++++++++------- test/test_app_status.rb | 13 +++++++++++++ 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/History.txt b/History.txt index 2f354ace..b9b3c0b6 100644 --- a/History.txt +++ b/History.txt @@ -1,3 +1,14 @@ +=== HEAD + +1 minor feature: + + Return valid Rack responses (passes Lint) from status + server. + +1 new contributer: + + * Jesse Cooke + === 1.2.2 / 2012-04-28 * 4 bug fixes: diff --git a/lib/puma/app/status.rb b/lib/puma/app/status.rb index d3a92429..e57da708 100644 --- a/lib/puma/app/status.rb +++ b/lib/puma/app/status.rb @@ -6,6 +6,7 @@ module Puma @cli = cli @auth_token = nil end + OK_STATUS = '{ "status": "ok" }'.freeze attr_accessor :auth_token @@ -16,34 +17,39 @@ module Puma def call(env) unless authenticate(env) - return [403, {}, ["Invalid auth token"]] + return rack_response(403, "Invalid auth token") end case env['PATH_INFO'] when "/stop" @server.stop - return [200, {}, ['{ "status": "ok" }']] + return rack_response(200, OK_STATUS) when "/halt" @server.halt - return [200, {}, ['{ "status": "ok" }']] + return rack_response(200, OK_STATUS) when "/restart" if @cli and @cli.restart_on_stop! @server.begin_restart - return [200, {}, ['{ "status": "ok" }']] + return rack_response(200, OK_STATUS) else - return [200, {}, ['{ "status": "not configured" }']] + return rack_response(200, '{ "status": "not configured" }') end when "/stats" b = @server.backlog r = @server.running - return [200, {}, ["{ \"backlog\": #{b}, \"running\": #{r} }"]] + return rack_response(200, %Q!{ "backlog": #{b}, "running": #{r} }!) end - [404, {}, ["Unsupported action"]] + rack_response 404, "Unsupported action" + end + + private + def rack_response(status, body) + [status, { 'Content-Type' => 'text/plain', 'Content-Length' => body.length.to_s }, [body]] end end end diff --git a/test/test_app_status.rb b/test/test_app_status.rb index 57df442e..d086b322 100644 --- a/test/test_app_status.rb +++ b/test/test_app_status.rb @@ -1,4 +1,5 @@ require 'test/unit' +require 'rack' require 'puma/app/status' class TestAppStatus < Test::Unit::TestCase @@ -27,6 +28,12 @@ class TestAppStatus < Test::Unit::TestCase @app.auth_token = nil end + def lint(env) + app = Rack::Lint.new @app + mock_env = Rack::MockRequest.env_for env['PATH_INFO'] + app.call mock_env + end + def test_bad_token @app.auth_token = "abcdef" @@ -35,6 +42,7 @@ class TestAppStatus < Test::Unit::TestCase status, _, _ = @app.call env assert_equal 403, status + lint(env) end def test_good_token @@ -48,6 +56,7 @@ class TestAppStatus < Test::Unit::TestCase status, _, _ = @app.call env assert_equal 404, status + lint(env) end def test_unsupported @@ -56,6 +65,7 @@ class TestAppStatus < Test::Unit::TestCase status, _, _ = @app.call env assert_equal 404, status + lint(env) end def test_stop @@ -66,6 +76,7 @@ class TestAppStatus < Test::Unit::TestCase assert_equal :stop, @server.status assert_equal 200, status assert_equal ['{ "status": "ok" }'], body + lint(env) end def test_halt @@ -76,6 +87,7 @@ class TestAppStatus < Test::Unit::TestCase assert_equal :halt, @server.status assert_equal 200, status assert_equal ['{ "status": "ok" }'], body + lint(env) end def test_stats @@ -88,6 +100,7 @@ class TestAppStatus < Test::Unit::TestCase assert_equal 200, status assert_equal ['{ "backlog": 1, "running": 9 }'], body + lint(env) end end