-
Notifications
You must be signed in to change notification settings - Fork 24
/
messenger_test.go
61 lines (49 loc) · 1.36 KB
/
messenger_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
package messenger_test
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/mileusna/facebook-messenger"
)
var fs *httptest.Server
var ts *httptest.Server
const (
verifyToken = "my_secret_token"
)
func TestMain(m *testing.M) {
// fs will mock up fb messenger server
fs = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rec := messenger.FacebookResponse{
RecipientID: 12123213123,
MessageID: "mid00000TEST00000TEST00000TEST",
}
b, _ := json.Marshal(rec)
w.Write(b)
}))
defer fs.Close()
// setup chatbot
messenger.TestURL = fs.URL + "/"
msng := &messenger.Messenger{
AccessToken: "XXXXXXX",
VerifyToken: verifyToken,
}
//chatbot.Messenger.MessageReceived = chatbot.MessageReceived
//chatbot.Messenger.DeliveryReceived = chatbot.DeliveryReceived
//chatbot.Messenger.PostbackReceived = chatbot.PostbackReceived
// ts is our test chatbot
ts = httptest.NewServer(msng)
defer ts.Close()
m.Run()
}
func TestVerify(t *testing.T) {
challenge := "1122334455"
verifyReq := ts.URL + "/?test=1&hub.mode=subscribe&hub.challenge=" + challenge + "&hub.verify_token=" + verifyToken
resp, _ := http.Get(verifyReq)
defer resp.Body.Close()
s, _ := ioutil.ReadAll(resp.Body)
if string(s) != challenge {
t.Error("Challenge failed, expected", challenge, "returned", string(s))
}
}