-
Notifications
You must be signed in to change notification settings - Fork 8
/
pusher_test.go
59 lines (51 loc) · 1.18 KB
/
pusher_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"reflect"
"sync"
"testing"
"github.com/tonkeeper/bridge/config"
)
func TestSendWebhook(t *testing.T) {
var wg sync.WaitGroup
wg.Add(2)
urls := make(chan string, 2)
handlerFunc := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer wg.Done()
body, err := io.ReadAll(r.Body)
if err != nil {
t.Fatal(err)
}
urls <- r.URL.String()
if string(body) != `{"topic":"test","hash":"test-hash"}` {
t.Fatalf("bad body: %s", body)
}
w.WriteHeader(http.StatusOK)
})
hook1 := httptest.NewServer(handlerFunc)
hook2 := httptest.NewServer(handlerFunc)
defer hook1.Close()
defer hook2.Close()
data := WebhookData{
Topic: "test",
Hash: "test-hash",
}
config.Config.WebhookURL = fmt.Sprintf("%s/webhook,%s/callback", hook1.URL, hook2.URL)
SendWebhook("SOME-CLIENT-ID", data)
wg.Wait()
close(urls)
calledUrls := make(map[string]struct{})
for url := range urls {
calledUrls[url] = struct{}{}
}
expected := map[string]struct{}{
"/webhook/SOME-CLIENT-ID": {},
"/callback/SOME-CLIENT-ID": {},
}
if !reflect.DeepEqual(calledUrls, expected) {
t.Fatalf("bad urls: %v", calledUrls)
}
}