Skip to content

Commit a8ad282

Browse files
authored
Merge pull request rails#55092 from francesco-loreti/add_render_json_to_health
Add render json to health
2 parents 1fcd079 + 5601888 commit a8ad282

File tree

3 files changed

+55
-6
lines changed

3 files changed

+55
-6
lines changed

actionpack/CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
* Add JSON support to the built-in health controller.
2+
3+
The health controller now responds to JSON requests with a structured response
4+
containing status and timestamp information. This makes it easier for monitoring
5+
tools and load balancers to consume health check data programmatically.
6+
7+
```ruby
8+
# /up.json
9+
{
10+
"status": "up",
11+
"timestamp": "2025-09-19T12:00:00Z"
12+
}
13+
```
14+
15+
*Francesco Loreti*, *Juan Vásquez*
16+
117
* Allow to open source file with a crash from the browser.
218

319
*Igor Kasyanchuk*

railties/lib/rails/health_controller.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,17 @@ def show
4343

4444
private
4545
def render_up
46-
render html: html_status(color: "green")
46+
respond_to do |format|
47+
format.html { render html: html_status(color: "green") }
48+
format.json { render json: { status: "up", timestamp: Time.current.iso8601 } }
49+
end
4750
end
4851

4952
def render_down
50-
render html: html_status(color: "red"), status: 500
53+
respond_to do |format|
54+
format.html { render html: html_status(color: "red"), status: 500 }
55+
format.json { render json: { status: "down", timestamp: Time.current.iso8601 }, status: 500 }
56+
end
5157
end
5258

5359
def html_status(color:)

railties/test/rails_health_controller_test.rb

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,47 @@ def setup
1212
@routes = Rails.application.routes
1313
end
1414

15-
test "health controller renders green success page" do
16-
get :show
15+
test "health controller renders green success page in HTML" do
16+
get :show, format: :html
1717
assert_response :success
1818
assert_match(/background-color: green/, @response.body)
1919
end
2020

21-
test "health controller renders red internal server error page" do
21+
test "health controller renders red internal server error page in HTML" do
2222
@controller.instance_eval do
2323
def render_up
2424
raise Exception, "some exception"
2525
end
2626
end
27-
get :show
27+
get :show, format: :html
2828
assert_response :internal_server_error
2929
assert_match(/background-color: red/, @response.body)
3030
end
31+
32+
test "health controller returns JSON success response" do
33+
get :show, format: :json
34+
assert_response :success
35+
assert_includes @response.content_type, "application/json"
36+
37+
json_response = JSON.parse(@response.body)
38+
assert_equal "up", json_response["status"]
39+
assert_includes json_response, "timestamp"
40+
assert_match(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/, json_response["timestamp"])
41+
end
42+
43+
test "health controller returns JSON error response" do
44+
@controller.instance_eval do
45+
def render_up
46+
raise Exception, "some exception"
47+
end
48+
end
49+
get :show, format: :json
50+
assert_response :internal_server_error
51+
assert_includes @response.content_type, "application/json"
52+
53+
json_response = JSON.parse(@response.body)
54+
assert_equal "down", json_response["status"]
55+
assert_includes json_response, "timestamp"
56+
assert_match(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/, json_response["timestamp"])
57+
end
3158
end

0 commit comments

Comments
 (0)