-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrequest_test.go
71 lines (56 loc) · 1.34 KB
/
request_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
package crater
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"testing"
)
func Test_init(t *testing.T) {
req := newRequest(newHttpRequest("GET", "localhost:8080/"), make(map[string]string))
if req == nil {
t.Error("newRequest returns nil")
}
if req.RouteParams == nil {
t.Error("RouteVars was not set")
}
}
type User struct {
Name string
Age int
}
func TestParseContentTypeJson(t *testing.T) {
userForTest := &User{"Bill", 42}
jsonBytes, _ := json.Marshal(userForTest)
r := newHttpRequest("POST", "localhost:8080/")
r.Body = ioutil.NopCloser(bytes.NewReader(jsonBytes))
r.Header.Add("Content-Type", "application/json")
req := newRequest(r, make(map[string]string))
u := new(User)
req.Parse(u)
if u.Name != "Bill" || u.Age != 42 {
t.Error("Body wasn't parsed")
}
}
func TestParseFormValues(t *testing.T) {
formValues := map[string][]string{
"Name": {"Bill"},
"Age": {"42"},
}
r := newHttpRequest("GET", "localhost:8080/")
r.Form = formValues
req := newRequest(r, make(map[string]string))
u := new(User)
req.Parse(u)
if u.Name != "Bill" || u.Age != 42 {
t.Error("Form values were not parsed")
}
}
// newHttpRequest creates a new request with a method and url
func newHttpRequest(method, url string) *http.Request {
req, err := http.NewRequest(method, url, nil)
if err != nil {
panic(err)
}
return req
}