This repository has been archived by the owner on Mar 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wehook_test.go
76 lines (70 loc) · 1.52 KB
/
wehook_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package pay
import (
"fmt"
"net/http"
"reflect"
"strings"
"testing"
)
func TestWebhookService_GetNotification(t *testing.T) {
type fields struct {
client *Client
}
type args struct {
r *http.Request
}
tests := []struct {
name string
fields fields
args args
want Notification
wantErr bool
}{
{
"Fail: key does not match",
fields{payClient},
args{newWebhookRequest(payClient.webhookSecret, false)},
Notification{},
true,
},
{
"succeed: key should match",
fields{payClient},
args{newWebhookRequest(payClient.webhookSecret, true)},
Notification{},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := &WebhookService{
client: tt.fields.client,
}
got, err := w.GetNotification(tt.args.r)
if (err != nil) != tt.wantErr {
t.Errorf("WebhookService.GetNotification() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("WebhookService.GetNotification() = %v, want %v", got, tt.want)
}
})
}
}
// newWebhookRequest creates a new request webhook
func newWebhookRequest(key string, pass bool) *http.Request {
body := `{"message":"test"}`
buf := strings.NewReader(body)
req, err := http.NewRequest("GET", "", buf)
if err != nil {
fmt.Println(err)
return nil
}
if !pass {
// invalidate the key
key = "lalalala"
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-BP-Webhook-Signature", genHash([]byte(body), []byte(key)))
return req
}