File tree Expand file tree Collapse file tree 2 files changed +26
-1
lines changed Expand file tree Collapse file tree 2 files changed +26
-1
lines changed Original file line number Diff line number Diff line change @@ -1104,9 +1104,15 @@ func (e Event) Origin() Module { return e.origin } // Returns the module t
1104
1104
// CloudEvents spec.
1105
1105
func (e Event ) CloudEvent () CloudEvent {
1106
1106
dataJSON , _ := json .Marshal (e .Data )
1107
+ var source string
1108
+ if e .Origin () == nil {
1109
+ source = "caddy"
1110
+ } else {
1111
+ source = string (e .Origin ().CaddyModule ().ID )
1112
+ }
1107
1113
return CloudEvent {
1108
1114
ID : e .id .String (),
1109
- Source : e . origin . CaddyModule (). String () ,
1115
+ Source : source ,
1110
1116
SpecVersion : "1.0" ,
1111
1117
Type : e .name ,
1112
1118
Time : e .ts ,
Original file line number Diff line number Diff line change 15
15
package caddy
16
16
17
17
import (
18
+ "context"
18
19
"testing"
19
20
"time"
20
21
)
@@ -72,3 +73,21 @@ func TestParseDuration(t *testing.T) {
72
73
}
73
74
}
74
75
}
76
+
77
+ func TestEvent_CloudEvent_NilOrigin (t * testing.T ) {
78
+ ctx , _ := NewContext (Context {Context : context .Background ()}) // module will be nil by default
79
+ event , err := NewEvent (ctx , "started" , nil )
80
+ if err != nil {
81
+ t .Fatalf ("NewEvent() error = %v" , err )
82
+ }
83
+
84
+ // This should not panic
85
+ ce := event .CloudEvent ()
86
+
87
+ if ce .Source != "caddy" {
88
+ t .Errorf ("Expected CloudEvent Source to be 'caddy', got '%s'" , ce .Source )
89
+ }
90
+ if ce .Type != "started" {
91
+ t .Errorf ("Expected CloudEvent Type to be 'started', got '%s'" , ce .Type )
92
+ }
93
+ }
You can’t perform that action at this time.
0 commit comments