-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathnews_test.go
72 lines (69 loc) · 2.43 KB
/
news_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
package wxworkbot
import (
"encoding/json"
"github.com/stretchr/testify/assert"
"strings"
"testing"
)
func TestUnmarshalNewsMessage(t *testing.T) {
jsonString := `
{
"msgtype": "news",
"news": {
"articles" : [
{
"title" : "中秋节礼品领取",
"description" : "今年中秋节公司有豪礼相送",
"url" : "URL",
"picurl" : "http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png"
}
]
}
}
`
var newsMsg newsMessage
err := json.Unmarshal([]byte(jsonString), &newsMsg)
assert.Nil(t, err)
assert.Equal(t, newsMsg.MsgType, "news")
assert.NotEmpty(t, newsMsg.News.Articles)
article := newsMsg.News.Articles[0]
assert.Equal(t, article.Title, "中秋节礼品领取")
assert.Equal(t, article.Description, "今年中秋节公司有豪礼相送")
assert.Equal(t, article.URL, "URL")
assert.Equal(t, article.PicURL, "http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png")
}
func TestMarshalNews(t *testing.T) {
news := News{
Articles: []NewsArticle{
{
Title: "中秋节礼品领取",
Description: "今年中秋节公司有豪礼相送",
URL: "URL",
PicURL: "http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png",
},
},
}
msgBytes, err := marshalMessage(news)
assert.Nil(t, err)
expected := `{"msgtype":"news","news":{"articles":[{"title":"中秋节礼品领取","description":"今年中秋节公司有豪礼相送","url":"URL","picurl":"http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png"}]}}`
msg := strings.TrimSuffix(string(msgBytes), "\n")
assert.Equal(t, expected, msg)
}
func TestMarshalNewsMessage(t *testing.T) {
newsMsg := newsMessage{
News: News{
Articles: []NewsArticle{
{
Title: "中秋节礼品领取",
Description: "今年中秋节公司有豪礼相送",
URL: "URL",
PicURL: "http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png",
},
}},
}
msgBytes, err := marshalMessage(newsMsg)
assert.Nil(t, err)
expected := `{"msgtype":"news","news":{"articles":[{"title":"中秋节礼品领取","description":"今年中秋节公司有豪礼相送","url":"URL","picurl":"http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png"}]}}`
msg := strings.TrimSuffix(string(msgBytes), "\n")
assert.Equal(t, expected, msg)
}