Skip to content

Commit 5601888

Browse files
JuanVqzrafaelfranca
authored andcommitted
Add JSON support to health controller
The health controller now responds to JSON requests, providing a structured response with status and timestamp information. This enhancement facilitates easier integration with monitoring tools and load balancers. Additionally, tests have been added to verify the JSON responses for both success and error scenarios.
1 parent 7aad0e1 commit 5601888

File tree

3 files changed

+35
-13
lines changed

3 files changed

+35
-13
lines changed

actionpack/CHANGELOG.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
1-
* Add `render json` into `HealthController`.
1+
* Add JSON support to the built-in health controller.
22

3-
*Francesco Loreti*
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*
416

517
* Allow to open source file with a crash from the browser.
618

railties/lib/rails/health_controller.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ def show
4444
private
4545
def render_up
4646
respond_to do |format|
47-
format.html { render html: html_status(color: "green"), status: 200 }
48-
format.json { render json: { status: 200 }, status: 200 }
47+
format.html { render html: html_status(color: "green") }
48+
format.json { render json: { status: "up", timestamp: Time.current.iso8601 } }
4949
end
5050
end
5151

5252
def render_down
5353
respond_to do |format|
5454
format.html { render html: html_status(color: "red"), status: 500 }
55-
format.json { render json: { status: 500 }, status: 500 }
55+
format.json { render json: { status: "down", timestamp: Time.current.iso8601 }, status: 500 }
5656
end
5757
end
5858

railties/test/rails_health_controller_test.rb

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,6 @@ def setup
1818
assert_match(/background-color: green/, @response.body)
1919
end
2020

21-
test "health controller returns JSON success response" do
22-
get :show, format: :json
23-
assert_response :success
24-
assert_equal({ "status" => 200 }, JSON.parse(@response.body))
25-
end
26-
2721
test "health controller renders red internal server error page in HTML" do
2822
@controller.instance_eval do
2923
def render_up
@@ -35,14 +29,30 @@ def render_up
3529
assert_match(/background-color: red/, @response.body)
3630
end
3731

38-
test "health controller returns JSON internal server error response" do
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
3944
@controller.instance_eval do
4045
def render_up
4146
raise Exception, "some exception"
4247
end
4348
end
4449
get :show, format: :json
4550
assert_response :internal_server_error
46-
assert_equal({ "status" => 500 }, JSON.parse(@response.body))
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"])
4757
end
4858
end

0 commit comments

Comments
 (0)