-
Notifications
You must be signed in to change notification settings - Fork 0
/
article_handlers_test.go
186 lines (146 loc) · 3.91 KB
/
article_handlers_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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/eyan422/Blog/CommonStruct"
"log"
"net/http"
"net/http/httptest"
"net/url"
"path"
"strconv"
"testing"
)
var expected = map[int]CommonStruct.Article{
1: CommonStruct.Article{
Id: 1,
Title: "Hello World",
Content: "Lorem ipsum dolor sit amet.",
Author: "John",
},
2: {
Id: 2,
Title: "Brave World",
Content: "To be or not to be.",
Author: "Frankie",
},
}
func TestGetArticleHandler(t *testing.T) {
mockStore := InitMockStore()
mockStore.On("GetArticle").Return(&CommonStruct.Article{
Id: 2,
Title: "Brave World",
Content: "To be or not to be.",
Author: "Frankie",
}, nil).Once()
req, err := http.NewRequest("GET", "/articles/2", nil)
if err != nil {
t.Fatal(err)
}
recorder := httptest.NewRecorder()
hf := http.HandlerFunc(getArticleHandler)
hf.ServeHTTP(recorder, req)
if status := recorder.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
var b CommonStruct.GetArticlesReply
err = json.NewDecoder(recorder.Body).Decode(&b)
if err != nil {
t.Fatal(err)
}
targetUrl := req.URL.Path
fmt.Printf("url : %v\n", targetUrl)
myUrl, err := url.Parse(targetUrl)
if err != nil {
log.Fatal(err)
}
articleId, err := strconv.Atoi(path.Base(myUrl.Path))
if err != nil {
return
}
article := articles[articleId]
if article != expected[articleId] {
t.Errorf("handler returned unexpected body: got %v want %v", article, expected)
}
}
func TestGetArticlesHandler(t *testing.T) {
mockStore := InitMockStore()
mockStore.On("GetArticles").Return([]*CommonStruct.Article{
{
Id: 1,
Title: "Hello World",
Content: "Lorem ipsum dolor sit amet.",
Author: "John",
},
}, nil).Once()
req, err := http.NewRequest("GET", "/articles", nil)
if err != nil {
t.Fatal(err)
}
recorder := httptest.NewRecorder()
hf := http.HandlerFunc(getArticlesHandler)
hf.ServeHTTP(recorder, req)
if status := recorder.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
fmt.Printf("response: %v", recorder)
var b CommonStruct.GetArticlesReply
err = json.NewDecoder(recorder.Body).Decode(&b)
if err != nil {
t.Fatal(err)
}
for _, value := range b.Data {
actual := value
if actual != expected[int(actual.Id)] {
t.Errorf("handler returned unexpected body: got %v want %v", actual, expected)
}
}
}
func TestCreateArticleHandler(t *testing.T) {
mockStore := InitMockStore()
mockStore.On("CreateArticle", &CommonStruct.Article{
Title: "Hello",
Content: "Lost World.",
Author: "Feng",
},
).Return(nil)
jsonBody := []byte(`{"title": "Hello", "Content": "Lost World.", "Author": "Feng"}`)
bodyReader := bytes.NewReader(jsonBody)
//fmt.Printf("jsonBody: %v", jsonBody)
req, err := http.NewRequest("POST", "/articles", bodyReader)
req.Header.Add("Content-Type", "application/json")
if err != nil {
t.Fatal(err)
}
recorder := httptest.NewRecorder()
hf := http.HandlerFunc(createArticleHandler)
hf.ServeHTTP(recorder, req)
if status := recorder.Code; status != http.StatusCreated {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusCreated)
}
expected := CommonStruct.Article{
Id: 1,
}
var actual CommonStruct.CreateArticlesReply
err = json.NewDecoder(recorder.Body).Decode(&actual)
if err != nil {
t.Fatal(err)
}
if err != nil {
t.Fatal(err)
}
if actual.Data.Id != expected.Id {
t.Errorf("handler returned unexpected id: got %v want %v", actual.Data.Id, expected.Id)
}
if actual.Message != CommonStruct.SuccessStatus {
t.Errorf("handler returned unexpected status: got %v want %v", actual.Message, CommonStruct.SuccessStatus)
}
code := recorder.Code
if code != http.StatusCreated {
t.Errorf("handler returned unexpected code: got %v want %v", code, http.StatusCreated)
}
}