-
Notifications
You must be signed in to change notification settings - Fork 1
/
view_test.go
124 lines (103 loc) · 2.55 KB
/
view_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
package gomek
import (
"io"
"net/http"
"net/http/httptest"
"testing"
)
type Notice struct{}
func (n *Notice) Post(w http.ResponseWriter, request *http.Request, d *Data) {
data := struct {
Name string `json:"name"`
}{
Name: "Joe",
}
JSON(w, data, http.StatusOK)
}
func (n *Notice) Put(w http.ResponseWriter, request *http.Request, d *Data) {
data := struct {
Name string `json:"name"`
}{
Name: "Cosmo",
}
JSON(w, data, http.StatusOK)
}
func (n *Notice) Delete(w http.ResponseWriter, r *http.Request, d *Data) {
data := struct {
Name string `json:"name"`
}{
Name: "Alex",
}
JSON(w, data, http.StatusOK)
}
func (n *Notice) Get(w http.ResponseWriter, r *http.Request, d *Data) {
data := struct {
Name string `json:"name"`
}{
Name: "Ram",
}
JSON(w, data, http.StatusOK)
}
func TestViewGet(t *testing.T) {
c := Config{}
mockApp := NewTestApp(c)
mockApp.Route("/blogs").Methods("POST").Resource(&Notice{})
mockApp.Start()
notice := Notice{}
handler := CreateTestHandler(mockApp, notice.Get)
req := httptest.NewRequest(http.MethodPost, "/blogs", nil)
w := httptest.NewRecorder()
handler(w, req)
resp := w.Result()
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
t.Errorf("Error: %v", err)
}
expected := `{"name":"Joe"}`
if string(data) != expected {
t.Errorf("Expected %s got '%v'", expected, string(data))
}
}
func TestViewPost(t *testing.T) {
c := Config{}
mockApp := NewTestApp(c)
mockApp.Route("/blogs").Methods("GET", "POST").Resource(&Notice{})
mockApp.Start()
notice := Notice{}
handler := CreateTestHandler(mockApp, notice.Get)
req := httptest.NewRequest(http.MethodGet, "/blogs", nil)
w := httptest.NewRecorder()
handler(w, req)
resp := w.Result()
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
t.Errorf("Error: %v", err)
}
expected := `{"name":"Ram"}`
if string(data) != expected {
t.Errorf("Expected %s got '%v'", expected, string(data))
}
}
func TestViewDelete(t *testing.T) {
c := Config{}
mockApp := NewTestApp(c)
mockApp.Route("/blogs").Methods("GET", "POST", "PUT", "DELETE").Resource(&Notice{})
mockApp.Start()
notice := Notice{}
handler := CreateTestHandler(mockApp, notice.Put)
req := httptest.NewRequest(http.MethodPut, "/blogs", nil)
w := httptest.NewRecorder()
handler(w, req)
resp := w.Result()
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
t.Errorf("Error: %v", err)
}
expected := `{"name":"Cosmo"}`
if string(data) != expected {
t.Errorf("Expected %s got '%v'", expected, string(data))
}
}