forked from go-lark/lark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_v1_test.go
53 lines (49 loc) · 1.28 KB
/
event_v1_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
package lark
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestPostEventMessage(t *testing.T) {
message := EventMessage{
Timestamp: "",
Token: "",
EventType: "event_callback",
Event: EventBody{
Type: "message",
ChatType: "private",
MsgType: "text",
OpenID: testUserOpenID,
Text: "private event",
Title: "",
OpenMessageID: "",
ImageKey: "",
ImageURL: "",
},
}
w := performRequest(func(w http.ResponseWriter, r *http.Request) {
var m EventMessage
json.NewDecoder(r.Body).Decode(&m)
w.Write([]byte(m.Event.Text))
}, "POST", "/", message)
assert.Equal(t, "private event", string(w.Body.Bytes()))
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
m, _ := json.Marshal(message)
w.Write([]byte(m))
}))
defer ts.Close()
resp, err := PostEvent(http.DefaultClient, ts.URL, message)
if assert.NoError(t, err) {
var event EventMessage
body, err := ioutil.ReadAll(resp.Body)
if assert.NoError(t, err) {
defer resp.Body.Close()
_ = json.Unmarshal(body, &event)
assert.Equal(t, "event_callback", event.EventType)
assert.Equal(t, "message", event.Event.Type)
}
}
}