|
| 1 | +/* |
| 2 | + Copyright 2024 The CloudEvents Authors |
| 3 | + SPDX-License-Identifier: Apache-2.0 |
| 4 | +*/ |
| 5 | + |
| 6 | +package extensions |
| 7 | + |
| 8 | +import ( |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/cloudevents/sdk-go/v2/event" |
| 12 | +) |
| 13 | + |
| 14 | +func TestAddDataRefExtension(t *testing.T) { |
| 15 | + // MUST HAVE AT LEAST ONE FAILING TEST as that'll also test to make sure |
| 16 | + // that the failed add() call won't actually set anything |
| 17 | + |
| 18 | + tests := []struct { |
| 19 | + dataref string |
| 20 | + pass bool |
| 21 | + }{ |
| 22 | + {"https://example.com/data", true}, |
| 23 | + {"://invalid-url", false}, |
| 24 | + } |
| 25 | + |
| 26 | + for _, test := range tests { |
| 27 | + e := event.New() |
| 28 | + err := AddDataRefExtension(&e, test.dataref) |
| 29 | + |
| 30 | + // Make sure adding it passed/fails appropriately |
| 31 | + if test.pass && err != nil { |
| 32 | + t.Fatalf("Failed to add DataRefExtension with valid URL(%s): %s", |
| 33 | + test.dataref, err) |
| 34 | + } |
| 35 | + if !test.pass && err == nil { |
| 36 | + t.Fatalf("Expected not to find DataRefExtension (%s), but did", |
| 37 | + test.dataref) |
| 38 | + } |
| 39 | + |
| 40 | + // Now make sure it's actually there in the 'pass' cases, but |
| 41 | + // missing in the failed cases. |
| 42 | + dr, ok := GetDataRefExtension(e) |
| 43 | + if test.pass { |
| 44 | + if !ok || dr.DataRef == "" { |
| 45 | + t.Fatalf("Dataref (%s) is missing after being set", |
| 46 | + test.dataref) |
| 47 | + } |
| 48 | + if dr.DataRef != test.dataref { |
| 49 | + t.Fatalf("Retrieved dataref(%v) doesn't match set value(%s)", |
| 50 | + dr, test.dataref) |
| 51 | + } |
| 52 | + } else { |
| 53 | + if ok || dr.DataRef != "" { |
| 54 | + t.Fatalf("Expected not to find DataRefExtension, but did(%s)", |
| 55 | + test.dataref) |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func TestGetDataRefExtensionNotFound(t *testing.T) { |
| 62 | + e := event.New() |
| 63 | + |
| 64 | + // Make sure there's no dataref by default |
| 65 | + dr, ok := GetDataRefExtension(e) |
| 66 | + if ok || dr.DataRef != "" { |
| 67 | + t.Fatal("Expected not to find DataRefExtension, but did") |
| 68 | + } |
| 69 | +} |
0 commit comments