-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubscriptions_test.go
More file actions
92 lines (83 loc) · 2.48 KB
/
subscriptions_test.go
File metadata and controls
92 lines (83 loc) · 2.48 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package maxigo
import (
"context"
"net/http"
"testing"
)
func TestSubscribe(t *testing.T) {
c, _ := testClient(t, func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("method = %q, want POST", r.Method)
}
if r.URL.Path != "/subscriptions" {
t.Errorf("path = %q, want /subscriptions", r.URL.Path)
}
var body SubscriptionRequestBody
readJSON(t, r, &body)
if body.URL != "https://example.com/webhook" {
t.Errorf("URL = %q, want %q", body.URL, "https://example.com/webhook")
}
if len(body.UpdateTypes) != 2 {
t.Errorf("len(UpdateTypes) = %d, want 2", len(body.UpdateTypes))
}
if body.Secret != "my-secret" {
t.Errorf("Secret = %q, want %q", body.Secret, "my-secret")
}
writeJSON(t, w, SimpleQueryResult{Success: true})
})
result, err := c.Subscribe(context.Background(), "https://example.com/webhook",
[]string{"message_created", "bot_started"}, "my-secret")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !result.Success {
t.Error("Success should be true")
}
}
func TestUnsubscribe(t *testing.T) {
c, _ := testClient(t, func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodDelete {
t.Errorf("method = %q, want DELETE", r.Method)
}
if r.URL.Query().Get("url") != "https://example.com/webhook" {
t.Errorf("url = %q, want %q", r.URL.Query().Get("url"), "https://example.com/webhook")
}
writeJSON(t, w, SimpleQueryResult{Success: true})
})
result, err := c.Unsubscribe(context.Background(), "https://example.com/webhook")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !result.Success {
t.Error("Success should be true")
}
}
func TestGetSubscriptions(t *testing.T) {
c, _ := testClient(t, func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
t.Errorf("method = %q, want GET", r.Method)
}
if r.URL.Path != "/subscriptions" {
t.Errorf("path = %q, want /subscriptions", r.URL.Path)
}
writeJSON(t, w, getSubscriptionsResult{
Subscriptions: []Subscription{
{
URL: "https://example.com/webhook",
Time: 1234567890,
UpdateTypes: []string{"message_created"},
},
},
})
})
subs, err := c.GetSubscriptions(context.Background())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(subs) != 1 {
t.Fatalf("len(subs) = %d, want 1", len(subs))
}
if subs[0].URL != "https://example.com/webhook" {
t.Errorf("URL = %q, want %q", subs[0].URL, "https://example.com/webhook")
}
}