-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtest_test.go
153 lines (129 loc) · 3.19 KB
/
htest_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
package htest
import (
"encoding/json"
"fmt"
"io"
"net/http"
"testing"
)
type data struct {
Mail string
Password string
}
func TestRequests(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/path", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
w.Header().Set("foo", "bar")
fmt.Fprintf(w, "Response")
}
})
mux.HandleFunc("/cookie", func(w http.ResponseWriter, r *http.Request) {
http.SetCookie(w, &http.Cookie{
Name: "batman",
Value: "htest",
})
})
mux.HandleFunc("/json", func(w http.ResponseWriter, r *http.Request) {
d := &data{
Mail: "test@test.com",
Password: "pass",
}
b, _ := json.Marshal(d)
w.Write(b)
})
mux.HandleFunc("/auth", func(w http.ResponseWriter, r *http.Request) {
var d data
err := json.NewDecoder(r.Body).Decode(&d)
if err != nil {
t.Error(err)
t.FailNow()
}
status := http.StatusOK
if d.Password != "pass" {
status = http.StatusUnauthorized
}
w.WriteHeader(status)
w.Write([]byte(http.StatusText(status)))
})
test := New(t, mux)
test.Request("POST", "/path").
Do().
ExpectBody("Response").
ExpectHeader("foo", "bar").
ExpectStatus(http.StatusOK)
test.Request("GET", "/cookie").Do().
ExpectCookie("batman", "htest")
test.Request("HEAD", "/path").Do().
ExpectStatus(http.StatusOK).
ExpectBody("").
ExpectHeader("foo", "")
test.Request("GET", "/unknownpath").Do().ExpectStatus(http.StatusNotFound)
test.Request("GET", "/json").Do().
ExpectJSON(&data{
Mail: "test@test.com",
Password: "pass",
})
test.Post("/auth").
Send(&data{
Mail: "test@test.com",
Password: "pass",
}).
Do().
ExpectStatus(http.StatusOK)
test.Post("/auth").
Send(&data{
Mail: "test@test.com",
Password: "pass222222",
}).
Do().
ExpectStatus(http.StatusUnauthorized).
ExpectBody(http.StatusText(http.StatusUnauthorized))
}
func TestExample(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/path", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
w.Header().Set("foo", "bar")
fmt.Fprintf(w, "Response")
}
})
test := New(t, mux)
test.Request("POST", "/path").
Do().
ExpectBody("Response").
ExpectHeader("foo", "bar").
ExpectStatus(http.StatusOK)
}
func TestHandler(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/admin", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("foo", "bar")
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintf(w, "You are not authorized")
})
test := New(t, mux)
test.Get("/admin").Do().
ExpectHeader("foo", "bar").
ExpectStatus(http.StatusUnauthorized).
ExpectBody("You are not authorized")
}
func TestBuildingRequest(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/path", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("foo", r.Header.Get("foo"))
io.Copy(w, r.Body)
})
test := New(t, mux)
test.Get("/path").
// Set a header to the request
AddHeader("foo", "barbar").
// Send a string to the request's body
SendString("my data").
// Executes the request
Do().
// The body sent should stay the same
ExpectBody("my data").
// The header sent should stay the same
ExpectHeader("foo", "barbar")
}