Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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.
  • Loading branch information...
commit ede98dadfdcb6def9a2fc1ba518f871d8cf426a2 1 parent 208c2f5
Jesse Cooke jc00ke authored
Showing with 37 additions and 7 deletions.
  1. +11 −0 History.txt
  2. +13 −7 lib/puma/app/status.rb
  3. +13 −0 test/test_app_status.rb
11 History.txt
View
@@ -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:
20 lib/puma/app/status.rb
View
@@ -6,6 +6,7 @@ def initialize(server, cli)
@cli = cli
@auth_token = nil
end
+ OK_STATUS = '{ "status": "ok" }'.freeze
attr_accessor :auth_token
@@ -16,34 +17,39 @@ def authenticate(env)
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
13 test/test_app_status.rb
View
@@ -1,4 +1,5 @@
require 'test/unit'
+require 'rack'
require 'puma/app/status'
class TestAppStatus < Test::Unit::TestCase
@@ -27,6 +28,12 @@ def setup
@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 @@ def test_bad_token
status, _, _ = @app.call env
assert_equal 403, status
+ lint(env)
end
def test_good_token
@@ -48,6 +56,7 @@ def test_good_token
status, _, _ = @app.call env
assert_equal 404, status
+ lint(env)
end
def test_unsupported
@@ -56,6 +65,7 @@ def test_unsupported
status, _, _ = @app.call env
assert_equal 404, status
+ lint(env)
end
def test_stop
@@ -66,6 +76,7 @@ def test_stop
assert_equal :stop, @server.status
assert_equal 200, status
assert_equal ['{ "status": "ok" }'], body
+ lint(env)
end
def test_halt
@@ -76,6 +87,7 @@ def test_halt
assert_equal :halt, @server.status
assert_equal 200, status
assert_equal ['{ "status": "ok" }'], body
+ lint(env)
end
def test_stats
@@ -88,6 +100,7 @@ def test_stats
assert_equal 200, status
assert_equal ['{ "backlog": 1, "running": 9 }'], body
+ lint(env)
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.