diff --git a/lib/functions_framework/legacy_event_converter.rb b/lib/functions_framework/legacy_event_converter.rb index 84c15160..5d0af0a0 100644 --- a/lib/functions_framework/legacy_event_converter.rb +++ b/lib/functions_framework/legacy_event_converter.rb @@ -135,18 +135,22 @@ def convert_source service, resource, domain match = CE_SERVICE_TO_RESOURCE_RE[service].match resource return [nil, nil] unless match + resource_fragment = match[1] + subject = match[2] if service == "firebasedatabase.googleapis.com" - return [nil, nil] if domain.nil? - location = "us-central1" - if domain != "firebaseio.com" - location_match = domain.match(/^([\w-]+)\.firebasedatabase\.app$/) - return [nil, nil] unless location_match - location = location_match[1] - end - ["//#{service}/projects/_/locations/#{location}/#{match[1]}", match[2]] + location = + case domain + when "firebaseio.com" + "us-central1" + when /^([\w-]+)\./ + Regexp.last_match[1] + else + return [nil, nil] + end + ["//#{service}/projects/_/locations/#{location}/#{resource_fragment}", subject] else - ["//#{service}/#{match[1]}", match[2]] + ["//#{service}/#{resource_fragment}", subject] end end diff --git a/test/test_legacy_event_converter.rb b/test/test_legacy_event_converter.rb index 4c0fd0f1..cd2a9baa 100644 --- a/test/test_legacy_event_converter.rb +++ b/test/test_legacy_event_converter.rb @@ -13,16 +13,27 @@ # limitations under the License. require "helper" +require "json" +require "stringio" describe FunctionsFramework::LegacyEventConverter do let(:data_dir) { File.join __dir__, "legacy_events_data" } - def load_legacy_event filename, url_path: nil, encoding: "utf-8" - path = File.join data_dir, filename - File.open path, encoding: encoding do |io| + def load_legacy_event filename_or_json, url_path: nil, encoding: "utf-8" + converter = FunctionsFramework::LegacyEventConverter.new + case filename_or_json + when String + path = File.join data_dir, filename_or_json + File.open path, encoding: encoding do |io| + env = { "rack.input" => io, "CONTENT_TYPE" => "application/json", "PATH_INFO" => url_path } + converter.decode_rack_env env + end + when Hash + io = StringIO.new JSON.dump filename_or_json env = { "rack.input" => io, "CONTENT_TYPE" => "application/json", "PATH_INFO" => url_path } - converter = FunctionsFramework::LegacyEventConverter.new converter.decode_rack_env env + else + raise ArgumentError, filename_or_json.class.name end end @@ -218,4 +229,26 @@ def load_legacy_event filename, url_path: nil, encoding: "utf-8" assert_equal "refs/gcf-test/abc", event.subject assert_equal "2020-05-21T11:56:12+00:00", event.time.rfc3339 end + + it "declines to convert firebasedatabase without a domain" do + json = { + "eventType" => "providers/google.firebase.database/eventTypes/ref.write", + "params" => { + "child" => "xyz" + }, + "auth" => { + "admin" => true + }, + "data" => { + "data" => nil, + "delta" => { + "grandchild" => "other" + } + }, + "resource" => "projects/_/instances/my-project-id/refs/gcf-test/xyz", + "timestamp" => "2020-05-21T11:15:34.178Z", + "eventId" => "/SnHth9OSlzK1Puj85kk4tDbF90=" + } + assert_nil load_legacy_event json + end end