-
Notifications
You must be signed in to change notification settings - Fork 7
/
http_test.go
156 lines (123 loc) · 3.81 KB
/
http_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
package handy
import (
"bytes"
"fmt"
"math"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
func Test_HTTPRequestAsString(t *testing.T) {
const habemosPapa = "[9,8,7,5]"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.Run("testing parsed value", func(t *testing.T) {
tr := HTTPRequestAsString(r, "status", 50)
if tr != habemosPapa {
t.Errorf("Test has failed!\n\tExpected: %s, \n\tGot: %v", habemosPapa, tr)
}
})
}))
defer ts.Close()
if _, err := http.PostForm(ts.URL, url.Values{"status": {habemosPapa}}); err != nil {
t.Errorf("Test has failed! %v", err)
}
if _, err := http.Get(fmt.Sprintf("%s?status=%s", ts.URL, habemosPapa)); err != nil {
t.Errorf("Test has failed! %v", err)
}
}
func Test_HTTPRequestAsInteger(t *testing.T) {
const xBurger = 1234567890
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.Run("testing parsed value", func(t *testing.T) {
tr := HTTPRequestAsInteger(r, "status")
if tr != xBurger {
t.Errorf("Test has failed!\n\tExpected: %v, \n\tGot: %v", xBurger, tr)
}
})
}))
defer ts.Close()
if _, err := http.PostForm(ts.URL, url.Values{"status": {fmt.Sprintf("%d", xBurger)}}); err != nil {
t.Errorf("Test has failed! %v", err)
}
}
func Test_HTTPRequestAsFloat64(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.Run("testing parsed value", func(t *testing.T) {
tr := HTTPRequestAsFloat64(r, "status", '.')
if tr != math.Pi {
t.Errorf("Test has failed!\n\tExpected: %v, \n\tGot: %v", math.Pi, tr)
}
})
}))
defer ts.Close()
if _, err := http.PostForm(ts.URL, url.Values{"status": {fmt.Sprintf("%.15f", math.Pi)}}); err != nil {
t.Errorf("Test has failed! %v", err)
}
}
func Test_HTTPJSONBodyToStruct(t *testing.T) {
const (
testName = "Forty Two"
testAge = 42
)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.Run("testing parsed value", func(t *testing.T) {
var testDummy struct {
Name string `json:"name"`
Age int `json:"age"`
}
tr := HTTPJSONBodyToStruct(r, &testDummy)
if !tr || ((testDummy.Name != testName) || (testDummy.Age != testAge)) {
t.Errorf("Test has failed!\n\tExpected: (%v,%v)\n\tGot: %v", testName, testAge, testDummy)
}
})
}))
defer ts.Close()
j := []byte(fmt.Sprintf(`{"name":"%s","age":%d}`, testName, testAge))
if _, err := http.Post(ts.URL, "application/json;charset=utf-8", bytes.NewBuffer(j)); err != nil {
t.Errorf("Test has failed! %v", err)
}
}
func TestHTTPJSONToStruct(t *testing.T) {
const (
testName = "Forty Two"
testAge = 42
closeBody = true
)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.Run("testing parsed value", func(t *testing.T) {
var testDummy struct {
Name string `json:"name"`
Age int `json:"age"`
}
if err := HTTPJSONToStruct(r, &testDummy, closeBody); err != nil {
t.Error(err)
}
if (testDummy.Name != testName) || (testDummy.Age != testAge) {
t.Errorf("Test has failed!\n\tExpected: (%v,%v)\n\tGot: %v", testName, testAge, testDummy)
}
})
}))
defer ts.Close()
j := []byte(fmt.Sprintf(`{"name":"%s","age":%d}`, testName, testAge))
if _, err := http.Post(ts.URL, "application/json;charset=utf-8", bytes.NewBuffer(j)); err != nil {
t.Errorf("Test has failed! %v", err)
}
}
func TestHTTPAnswerJSON(t *testing.T) {
const (
testName = "Forty Two"
testAge = 42
)
httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var testDummy struct {
Name string `json:"name"`
Age int `json:"age"`
}
testDummy.Name = testName
testDummy.Age = testAge
if err := HTTPAnswerJSON(w, testDummy); err != nil {
t.Error(err)
}
}))
}