Skip to content

Commit f87ce40

Browse files
authored
feat: Return 204 from GET requests to an event function, to support health checks (#128)
1 parent a23620f commit f87ce40

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

functions_framework.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ version = ::FunctionsFramework::VERSION
4545
spec.executables = ["functions-framework", "functions-framework-ruby"]
4646

4747
spec.required_ruby_version = ">= 2.5.0"
48-
spec.add_dependency "cloud_events", ">= 0.6.0", "< 2.a"
48+
spec.add_dependency "cloud_events", ">= 0.7.0", "< 2.a"
4949
spec.add_dependency "puma", ">= 4.3.0", "< 6.a"
5050
spec.add_dependency "rack", "~> 2.1"
5151

lib/functions_framework/server.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,10 @@ def notfound_response
364364
string_response "Not found", 404
365365
end
366366

367+
def no_content_response
368+
[204, [], []]
369+
end
370+
367371
def string_response string, status, content_type: nil
368372
string.force_encoding ::Encoding::ASCII_8BIT unless string.valid_encoding?
369373
if string.encoding == ::Encoding::ASCII_8BIT
@@ -403,7 +407,7 @@ def call env
403407
return notfound_response if excluded_path? env
404408
response =
405409
begin
406-
logger = env["rack.logger"] ||= @config.logger
410+
logger = env[::Rack::RACK_LOGGER] ||= @config.logger
407411
request = ::Rack::Request.new env
408412
logger.info "FunctionsFramework: Handling HTTP #{request.request_method} request"
409413
@function.call request, globals: @globals, logger: logger
@@ -426,7 +430,8 @@ def initialize function, globals, config
426430

427431
def call env
428432
return notfound_response if excluded_path? env
429-
logger = env["rack.logger"] ||= @config.logger
433+
return no_content_response if env[::Rack::REQUEST_METHOD] == "GET"
434+
logger = env[::Rack::RACK_LOGGER] ||= @config.logger
430435
event = decode_event env
431436
response =
432437
case event
@@ -448,9 +453,8 @@ def decode_event env
448453
begin
449454
@cloud_events.decode_event env
450455
rescue ::CloudEvents::NotCloudEventError
451-
env["rack.input"].rewind rescue nil
452-
@legacy_events.decode_rack_env(env) ||
453-
raise(::CloudEvents::CloudEventsError, "Unrecognized event format")
456+
env[::Rack::RACK_INPUT].rewind rescue nil
457+
@legacy_events.decode_rack_env(env) || ::CloudEvents::CloudEventsError.new("Unrecognized event format")
454458
end
455459
rescue ::CloudEvents::CloudEventsError => e
456460
e

test/test_server.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,4 +287,13 @@ def query_server_with_retry server
287287
assert_match(/Unrecognized event format/, response.body)
288288
assert_equal "text/plain; charset=utf-8", response["Content-Type"]
289289
end
290+
291+
it "returns no content when an event server receives a get" do
292+
response = query_server_with_retry event_server do
293+
::Net::HTTP.get_response URI("#{server_url}/")
294+
end
295+
refute_nil response
296+
assert_equal "204", response.code
297+
assert_nil response.body
298+
end
290299
end

0 commit comments

Comments
 (0)