forked from go-chi/chi
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathphi_test.go
More file actions
142 lines (106 loc) · 2.85 KB
/
phi_test.go
File metadata and controls
142 lines (106 loc) · 2.85 KB
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
package phi
import (
"bytes"
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
)
var r *Mux
func TestMain(t *testing.T) {
r = NewRouter()
r.Route("/", func(r Router) {
r.Get("/standard", handleRoot)
r.POST("/newHandler", handleNewHandler)
})
t.Run("Test Standard", TestStandard)
t.Run("New Handler", TestNewHandler)
t.Run("New Handler 2", TestNewHandler2)
}
func TestStandard(t *testing.T) {
req := httptest.NewRequest("GET", "/standard", nil)
recorder := httptest.NewRecorder()
r.ServeHTTP(recorder, req)
res := recorder.Result()
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
t.Errorf("expected err to be nil got %v", err)
}
fmt.Printf("Body: %s\n", body)
}
func TestErrorSuccess(t *testing.T) {
req := httptest.NewRequest("GET", "/errorSuccess", nil)
recorder := httptest.NewRecorder()
r.ServeHTTP(recorder, req)
res := recorder.Result()
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
t.Errorf("expected err to be nil got %v", err)
}
fmt.Printf("Body: %s\n", body)
}
func TestError(t *testing.T) {
req := httptest.NewRequest("GET", "/error", nil)
recorder := httptest.NewRecorder()
r.ServeHTTP(recorder, req)
res := recorder.Result()
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
t.Errorf("expected err to be nil got %v", err)
}
fmt.Printf("Body: %s\n", body)
}
func TestNewHandler(t *testing.T) {
data := bytes.NewBuffer([]byte("{\"data\":\"1337\"}"))
req := httptest.NewRequest("POST", "/newHandler", data)
recorder := httptest.NewRecorder()
r.ServeHTTP(recorder, req)
res := recorder.Result()
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
t.Errorf("expected err to be nil got %v", err)
}
expected := "{\"data\":\"success\"}"
if fmt.Sprintf("%s", body) != expected {
t.Errorf("expected body to be {\"data\":\"success\"} got %s", body)
}
}
func TestNewHandler2(t *testing.T) {
data := bytes.NewBuffer([]byte("{\"data\":\"1338\"}"))
req := httptest.NewRequest("POST", "/newHandler", data)
recorder := httptest.NewRecorder()
r.ServeHTTP(recorder, req)
res := recorder.Result()
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
t.Errorf("expected err to be nil got %v", err)
}
expected := "{\"error\":\"Invalid Data\",\"message\":\"Data is not 1337\"}"
if fmt.Sprintf("%s", body) != expected {
t.Errorf("expected body to be {\"data\":\"success\"} got %s", body)
}
}
func handleRoot(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, World!"))
}
func handleNewHandler(res *Response, req *Request) *Error {
body, err := Validate[struct {
Data string `json:"data,required"`
}](req)
if err != nil {
return err
}
if body.Data != "1337" {
return &Error{
Error: "Invalid Data",
Message: "Data is not 1337",
}
}
return res.JSON("success")
}