File tree 2 files changed +77
-0
lines changed
2 files changed +77
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ Copyright 2024 The CloudEvents Authors
3
+ SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ package extensions
7
+
8
+ import (
9
+ "github.com/cloudevents/sdk-go/v2/event"
10
+ "net/url"
11
+ )
12
+
13
+ const DataRefExtensionKey = "dataref"
14
+
15
+ type DataRefExtension struct {
16
+ DataRef string `json:"dataref"`
17
+ }
18
+
19
+ func AddDataRefExtension (e * event.Event , dataRef string ) error {
20
+ if _ , err := url .Parse (dataRef ); err != nil {
21
+ return err
22
+ }
23
+ e .SetExtension (DataRefExtensionKey , dataRef )
24
+ return nil
25
+ }
26
+
27
+ func GetDataRefExtension (e event.Event ) (DataRefExtension , bool ) {
28
+ if dataRefValue , ok := e .Extensions ()[DataRefExtensionKey ]; ok {
29
+ dataRefStr , ok := dataRefValue .(string )
30
+ if ! ok {
31
+ return DataRefExtension {}, false
32
+ }
33
+ return DataRefExtension {DataRef : dataRefStr }, true
34
+ }
35
+ return DataRefExtension {}, false
36
+ }
Original file line number Diff line number Diff line change
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 TestAddDataRefExtensionValidURL (t * testing.T ) {
15
+ e := event .New ()
16
+ expectedDataRef := "https://example.com/data"
17
+
18
+ err := AddDataRefExtension (& e , expectedDataRef )
19
+ if err != nil {
20
+ t .Fatalf ("Failed to add DataRefExtension with valid URL: %s" , err )
21
+ }
22
+ }
23
+
24
+ func TestAddDataRefExtensionInvalidURL (t * testing.T ) {
25
+ e := event .New ()
26
+ invalidDataRef := "://invalid-url"
27
+
28
+ err := AddDataRefExtension (& e , invalidDataRef )
29
+ if err == nil {
30
+ t .Fatal ("Expected error when adding DataRefExtension with invalid URL, but got none" )
31
+ }
32
+ }
33
+
34
+ func TestGetDataRefExtensionNotFound (t * testing.T ) {
35
+ e := event .New ()
36
+
37
+ _ , ok := GetDataRefExtension (e )
38
+ if ok {
39
+ t .Fatal ("Expected not to find DataRefExtension, but did" )
40
+ }
41
+ }
You can’t perform that action at this time.
0 commit comments