Skip to content

Commit

Permalink
Merge pull request #53 from floere/main
Browse files Browse the repository at this point in the history
Add failure_status option and spec.
  • Loading branch information
liamdawson authored Sep 8, 2023
2 parents 16e5e3f + 60d53c0 commit 3808274
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
9 changes: 7 additions & 2 deletions lib/rack/ecg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class ECG
DEFAULT_MOUNT_AT = "/_ecg"
# Checks enabled by default.
DEFAULT_CHECKS = [:http]
# Default failure response status.
DEFAULT_FAILURE_STATUS = 500

# Constructs an instance of ECG Rack middleware with the specified
# options.
Expand All @@ -22,14 +24,17 @@ class ECG
# @param at [String, nil] Path which this ECG instance handles.
# @param hook [#call, nil] Callable which receives the success status and
# check results
def initialize(app = nil, checks: DEFAULT_CHECKS, at: DEFAULT_MOUNT_AT, hook: nil)
def initialize(app = nil, checks: DEFAULT_CHECKS, at: DEFAULT_MOUNT_AT, hook: nil,
failure_status: DEFAULT_FAILURE_STATUS)
@app = app

check_configuration = checks || []
@check_factory = CheckFactory.new(check_configuration, DEFAULT_CHECKS)
@mount_at = at || DEFAULT_MOUNT_AT

@result_hook = hook

@failure_response_status = failure_status
end

# Rack compatible call method. Not intended for direct usage.
Expand All @@ -41,7 +46,7 @@ def call(env)

success = check_results.none? { |check| check[1][:status] == Check::Status::ERROR }

response_status = success ? 200 : 500
response_status = success ? 200 : @failure_response_status

@result_hook&.call(success, check_results)

Expand Down
12 changes: 11 additions & 1 deletion spec/rack_middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,20 @@
let(:options) do
{ checks: [:error] }
end
it "has a success error code" do
it "has a failure error code" do
get "_ecg"
expect(last_response.status).to(eq(500))
end

context "with failure status option" do
let(:options) do
{ checks: [:error], failure_status: 503 }
end
it "has a failure error code" do
get "_ecg"
expect(last_response.status).to(eq(503))
end
end
end

context "when hook config option is set" do
Expand Down

0 comments on commit 3808274

Please sign in to comment.