forked from jimmykuu/gopher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
comment_test.go
123 lines (112 loc) · 2.88 KB
/
comment_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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package gopher
import (
"bufio"
"fmt"
"io"
"net/http"
"net/http/httptest"
Url "net/url"
"reflect"
"strings"
"testing"
"time"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
// Post是辅助函数,适用于一般方法的post,支持url.Values和符合json或者url-encoded格式的io.Reader.
func Post(url string, data interface{}) (*http.Request, error) {
if data == nil {
return http.NewRequest("POST", url, nil)
}
switch form := data.(type) {
case Url.Values:
body := strings.NewReader(form.Encode())
req, err := http.NewRequest("POST", url, body)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
if err != nil {
return nil, err
}
return req, err
case io.Reader:
bufReader := bufio.NewReader(form)
b, err := bufReader.Peek(1)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", url, bufReader)
if err != nil {
return nil, err
}
// json-encoded
if b[0] == '{' || b[0] == '[' {
req.Header.Set("Content-Type", "application/json")
} else {
// url-encoded
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}
return req, nil
default:
return nil, fmt.Errorf("data type %#v is not supported.", reflect.TypeOf(data))
}
}
func TestCommentAt(t *testing.T) {
db, _ := mgo.Dial(Config.DB)
//插入一条主题,一个回复.
//和两个用户.
topicId := bson.NewObjectId()
contentId := bson.NewObjectId()
nodeId := bson.NewObjectId()
userId := bson.NewObjectId()
commenterId := bson.NewObjectId()
// 插入主题
contentsC := db.DB("gopher").C(CONTENTS)
contentsC.Insert(&Topic{
Content: Content{
Id_: contentId,
CreatedBy: userId,
Type: TypeTopic,
},
Id_: topicId,
NodeId: nodeId,
LatestRepliedAt: time.Now(),
},
)
defer func() {
contentsC.Remove(bson.M{"_id": topicId})
}()
// 插入用户
usersC := db.DB("gopher").C(USERS)
usersC.Insert(bson.M{"_id": userId, "username": "user"})
usersC.Insert(bson.M{"_id": commenterId, "username": "commenter"})
usersC.Insert(bson.M{"username": "3rd_user"})
defer func() {
usersC.Remove(bson.M{"username": "commenter"})
usersC.Remove(bson.M{"username": "user"})
usersC.Remove(bson.M{"username": "3rd_user"})
}()
form := Url.Values{}
form.Add("content", "@3rd_user comments")
req, err := Post(Config.Host+"/comment/"+topicId.Hex(), form)
if err != nil {
t.Fatal(err)
}
session, _ := store.Get(req, "user")
session.Values["username"] = "commenter"
res := httptest.NewRecorder()
r := getRoute()
r.ServeHTTP(res, req)
defer func() {
c := db.DB("gopher").C(COMMENTS)
c.Remove(bson.M{"createdby": commenterId})
}()
t.Log(res.Code)
t.Log(res.Body.String())
t.Log(res.Header().Get("Location"))
user, err := getUserByName(usersC, "3rd_user")
if err != nil {
t.Fatal(err)
}
if len(user.RecentAts) != 1 || user.RecentAts[0].User != "commenter" {
t.Fatal("at update failed")
}
}