|
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | 15 | require "helper" |
| 16 | +require "json" |
| 17 | +require "stringio" |
16 | 18 |
|
17 | 19 | describe FunctionsFramework::LegacyEventConverter do |
18 | 20 | let(:data_dir) { File.join __dir__, "legacy_events_data" } |
19 | 21 |
|
20 | | - def load_legacy_event filename, url_path: nil, encoding: "utf-8" |
21 | | - path = File.join data_dir, filename |
22 | | - File.open path, encoding: encoding do |io| |
| 22 | + def load_legacy_event filename_or_json, url_path: nil, encoding: "utf-8" |
| 23 | + converter = FunctionsFramework::LegacyEventConverter.new |
| 24 | + case filename_or_json |
| 25 | + when String |
| 26 | + path = File.join data_dir, filename_or_json |
| 27 | + File.open path, encoding: encoding do |io| |
| 28 | + env = { "rack.input" => io, "CONTENT_TYPE" => "application/json", "PATH_INFO" => url_path } |
| 29 | + converter.decode_rack_env env |
| 30 | + end |
| 31 | + when Hash |
| 32 | + io = StringIO.new JSON.dump filename_or_json |
23 | 33 | env = { "rack.input" => io, "CONTENT_TYPE" => "application/json", "PATH_INFO" => url_path } |
24 | | - converter = FunctionsFramework::LegacyEventConverter.new |
25 | 34 | converter.decode_rack_env env |
| 35 | + else |
| 36 | + raise ArgumentError, filename_or_json.class.name |
26 | 37 | end |
27 | 38 | end |
28 | 39 |
|
@@ -218,4 +229,26 @@ def load_legacy_event filename, url_path: nil, encoding: "utf-8" |
218 | 229 | assert_equal "refs/gcf-test/abc", event.subject |
219 | 230 | assert_equal "2020-05-21T11:56:12+00:00", event.time.rfc3339 |
220 | 231 | end |
| 232 | + |
| 233 | + it "declines to convert firebasedatabase without a domain" do |
| 234 | + json = { |
| 235 | + "eventType" => "providers/google.firebase.database/eventTypes/ref.write", |
| 236 | + "params" => { |
| 237 | + "child" => "xyz" |
| 238 | + }, |
| 239 | + "auth" => { |
| 240 | + "admin" => true |
| 241 | + }, |
| 242 | + "data" => { |
| 243 | + "data" => nil, |
| 244 | + "delta" => { |
| 245 | + "grandchild" => "other" |
| 246 | + } |
| 247 | + }, |
| 248 | + "resource" => "projects/_/instances/my-project-id/refs/gcf-test/xyz", |
| 249 | + "timestamp" => "2020-05-21T11:15:34.178Z", |
| 250 | + "eventId" => "/SnHth9OSlzK1Puj85kk4tDbF90=" |
| 251 | + } |
| 252 | + assert_nil load_legacy_event json |
| 253 | + end |
221 | 254 | end |
0 commit comments