Skip to content

Commit 7aad0e1

Browse files
francesco-loretirafaelfranca
authored andcommitted
Add render json to health
1 parent 1fcd079 commit 7aad0e1

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

actionpack/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Add `render json` into `HealthController`.
2+
3+
*Francesco Loreti*
4+
15
* Allow to open source file with a crash from the browser.
26

37
*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"), status: 200 }
48+
format.json { render json: { status: 200 }, status: 200 }
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: 500 }, status: 500 }
56+
end
5157
end
5258

5359
def html_status(color:)

railties/test/rails_health_controller_test.rb

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,37 @@ 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 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+
27+
test "health controller renders red internal server error page in HTML" do
2228
@controller.instance_eval do
2329
def render_up
2430
raise Exception, "some exception"
2531
end
2632
end
27-
get :show
33+
get :show, format: :html
2834
assert_response :internal_server_error
2935
assert_match(/background-color: red/, @response.body)
3036
end
37+
38+
test "health controller returns JSON internal server error response" do
39+
@controller.instance_eval do
40+
def render_up
41+
raise Exception, "some exception"
42+
end
43+
end
44+
get :show, format: :json
45+
assert_response :internal_server_error
46+
assert_equal({ "status" => 500 }, JSON.parse(@response.body))
47+
end
3148
end

0 commit comments

Comments
 (0)