-
Notifications
You must be signed in to change notification settings - Fork 0
/
comment_test.go
117 lines (90 loc) · 2.52 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
package docbase
import (
"fmt"
"github.com/hayashiki/docbase-go/testutil"
"net/http"
"reflect"
"testing"
"time"
)
func TestCommentService_Create(t *testing.T) {
setup()
defer teardown()
post := &Post{
ID: 1,
}
mux.HandleFunc(fmt.Sprintf("/posts/%d/comments", post.ID), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
fmt.Fprint(w, testutil.LoadFixture(t, "comment-response.json"))
})
cReq := &CommentCreateRequest{}
ti, err := time.Parse(time.RFC3339, "2020-03-27T09:25:09+09:00")
want := &Comment{
ID: 1,
Body: "コメント",
CreatedAt: ti,
SimpleUser: SimpleUser{
ID: 1,
Name: "danny",
ProfileImageURL: "https://image.docbase.io/uploads/aaa.gif",
},
}
comment, resp, err := client.Comments.Create(post.ID, cReq)
if err != nil {
t.Errorf("Shouldn't have returned an error: %+v", err)
}
if resp.StatusCode != http.StatusOK {
t.Errorf("Comment Create request code = %v, expected %v", resp.StatusCode, http.StatusOK)
}
if !reflect.DeepEqual(want, comment) {
t.Errorf("Request response: %+v, want %+v", comment, want)
}
}
func TestCommentCli_Create_Error(t *testing.T) {
setup()
defer teardown()
post := &Post{
ID: 1,
}
mux.HandleFunc(fmt.Sprintf("/posts/%d/comments", post.ID), func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, `{
"error": "bad_request",
"messages": [
"Nameを入力してください"
]
}`)
})
cReq := &CommentCreateRequest{}
_, resp, err := client.Comments.Create(post.ID, cReq)
errResp, ok := err.(*ErrorResponse)
if !ok {
t.Errorf("Error should be of type ErrorResponse but is %v: %+v", reflect.TypeOf(err), err)
}
want := "bad_request"
if got := errResp.ErrorStr; want != got {
t.Errorf("Error message: %v, want %v", got, want)
}
if got, want := resp.StatusCode, http.StatusBadRequest; want != got {
t.Errorf("Status code: %d, want %d", got, want)
}
}
func TestCommentService_Delete(t *testing.T) {
setup()
defer teardown()
comment := &Comment{
ID: 1,
}
mux.HandleFunc(fmt.Sprintf("/comments/%d", comment.ID), func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent)
testMethod(t, r, "DELETE")
fmt.Fprint(w, `{}`)
})
resp, err := client.Comments.Delete(comment.ID)
if err != nil {
t.Errorf("Shouldn't have returned an error: %+v", err)
}
if resp.StatusCode != http.StatusNoContent {
t.Errorf("Comment Delete request code = %v, expected %v", resp.StatusCode, http.StatusOK)
}
}