@@ -5,45 +5,83 @@ import (
5
5
"encoding/json"
6
6
"net"
7
7
"net/http"
8
+ "sync"
8
9
"testing"
9
10
10
11
"github.com/stretchr/testify/require"
11
12
12
13
"github.com/livekit/protocol/auth"
14
+ "github.com/livekit/protocol/livekit"
13
15
"github.com/livekit/protocol/webhook"
14
16
)
15
17
18
+ const (
19
+ apiKey = "mykey"
20
+ apiSecret = "mysecret"
21
+ )
22
+
16
23
func TestWebHook (t * testing.T ) {
17
24
s := newServer (":8765" )
18
25
require .NoError (t , s .Start ())
19
26
defer s .Stop ()
20
27
21
- authProvider := auth .NewFileBasedKeyProviderFromMap (map [string ]string {
22
- "mykey" : "mysecret" ,
28
+ authProvider := auth .NewSimpleKeyProvider (
29
+ apiKey , apiSecret ,
30
+ )
31
+ notifier := webhook .NewNotifier (apiKey , apiSecret , []string {
32
+ "http://localhost:8765" ,
23
33
})
24
34
25
- payload := map [string ]interface {}{
26
- "test" : "payload" ,
27
- "nested" : map [string ]interface {}{
28
- "structure" : true ,
29
- },
30
- }
35
+ t .Run ("test json payload" , func (t * testing.T ) {
36
+ payload := map [string ]interface {}{
37
+ "test" : "payload" ,
38
+ "nested" : map [string ]interface {}{
39
+ "structure" : true ,
40
+ },
41
+ }
31
42
32
- s .handler = func (r * http.Request ) {
33
- // receive logic
34
- data , err := webhook .Receive (r , authProvider )
35
- require .NoError (t , err )
43
+ wg := sync.WaitGroup {}
44
+ wg .Add (1 )
45
+ s .handler = func (r * http.Request ) {
46
+ defer wg .Done ()
47
+ // receive logic
48
+ data , err := webhook .Receive (r , authProvider )
49
+ require .NoError (t , err )
36
50
37
- var decoded map [string ]interface {}
38
- require .NoError (t , json .Unmarshal (data , & decoded ))
51
+ var decoded map [string ]interface {}
52
+ require .NoError (t , json .Unmarshal (data , & decoded ))
39
53
40
- require .EqualValues (t , decoded , payload )
41
- }
54
+ require .EqualValues (t , decoded , payload )
55
+ }
42
56
43
- notifier := webhook . NewNotifier ( "mykey" , "mysecret" , [] string {
44
- "http://localhost:8765" ,
57
+ require . NoError ( t , notifier . Notify ( context . Background (), payload ))
58
+ wg . Wait ()
45
59
})
46
- require .NoError (t , notifier .Notify (context .Background (), payload ))
60
+
61
+ t .Run ("test event payload" , func (t * testing.T ) {
62
+ event := & livekit.WebhookEvent {
63
+ Event : webhook .EventTrackPublished ,
64
+ Participant : & livekit.ParticipantInfo {
65
+ Identity : "test" ,
66
+ },
67
+ Track : & livekit.TrackInfo {
68
+ Sid : "TR_abcde" ,
69
+ },
70
+ }
71
+
72
+ wg := sync.WaitGroup {}
73
+ wg .Add (1 )
74
+ s .handler = func (r * http.Request ) {
75
+ defer wg .Done ()
76
+ decodedEvent , err := webhook .ReceiveWebhookEvent (r , authProvider )
77
+ require .NoError (t , err )
78
+
79
+ require .EqualValues (t , event , decodedEvent )
80
+ }
81
+ require .NoError (t , notifier .Notify (context .Background (), event ))
82
+ wg .Wait ()
83
+ })
84
+
47
85
}
48
86
49
87
type testServer struct {
0 commit comments