-
Notifications
You must be signed in to change notification settings - Fork 53
/
http-utils_test.go
208 lines (196 loc) · 7.22 KB
/
http-utils_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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package utils
import (
"bytes"
"fmt"
"log"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestHTTPResponse(t *testing.T) {
t.Run("json content-type", func(t *testing.T) {
rr := httptest.NewRecorder()
HTTPResponse(rr, JSONApplicationUTF8, http.StatusOK, []byte("JSON Content-Type"))
assert.Equal(t, JSONApplicationUTF8, rr.Header().Get(ContentType))
assert.Equal(t, "JSON Content-Type", rr.Body.String())
})
t.Run("empty content-type", func(t *testing.T) {
rr := httptest.NewRecorder()
HTTPResponse(rr, "", http.StatusOK, []byte("Empty Content-Type"))
assert.Equal(t, "", rr.Header().Get(ContentType))
assert.Equal(t, "Empty Content-Type", rr.Body.String())
})
t.Run("json body", func(t *testing.T) {
rr := httptest.NewRecorder()
type DataJSON struct {
Key1 string `json:"key1"`
Key2 string `json:"key2"`
}
data := DataJSON{
Key1: "value1",
Key2: "value2",
}
HTTPResponse(rr, JSONApplication, http.StatusOK, data)
assert.Equal(t, JSONApplication, rr.Header().Get(ContentType))
assert.Equal(t, `{"key1":"value1","key2":"value2"}`, rr.Body.String())
})
}
func serverMock() *httptest.Server {
handler := http.NewServeMux()
handler.HandleFunc("/server/testing", testingMock)
srv := httptest.NewServer(handler)
return srv
}
func testingMock(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("the test works"))
}
func captureOutput(f func()) string {
var buf bytes.Buffer
log.SetOutput(&buf)
f()
log.SetOutput(os.Stderr)
return buf.String()
}
func TestSendRequest(t *testing.T) {
server := serverMock()
defer server.Close()
t.Run("empty url", func(t *testing.T) {
code, _, err := SendRequest(http.MethodPost, "", nil, map[string]string{})
assert.Error(t, err)
assert.Equal(t, 0, code)
})
t.Run("invalid url", func(t *testing.T) {
_, _, err := SendRequest(http.MethodPost, "http://whatever/notfound", nil, map[string]string{})
assert.Error(t, err)
})
t.Run("url not found", func(t *testing.T) {
code, _, err := SendRequest(http.MethodPost, server.URL+"/notfound", nil, map[string]string{})
assert.NoError(t, err)
assert.Equal(t, http.StatusNotFound, code)
})
t.Run("url not found", func(t *testing.T) {
code, body, err := SendRequest(http.MethodPost, server.URL+"/server/testing", nil, map[string]string{})
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, code)
assert.Equal(t, []byte("the test works"), body)
})
t.Run("https url", func(t *testing.T) {
_, _, err := SendRequest(http.MethodPost, "https://whatever/notfound", nil, map[string]string{})
assert.Error(t, err)
})
t.Run("headers url", func(t *testing.T) {
headers := make(map[string]string)
headers["test"] = "aaa"
_, _, err := SendRequest(http.MethodPost, server.URL+"/server/testing", nil, headers)
assert.NoError(t, err)
})
}
func TestDebugHTTP(t *testing.T) {
t.Run("no debug", func(t *testing.T) {
req, _ := http.NewRequest(http.MethodGet, "https://whatever/server/path", nil)
output := DebugHTTP(req, false, false)
assert.Equal(t, ``, output)
})
t.Run("debug no body", func(t *testing.T) {
req, _ := http.NewRequest(http.MethodGet, "https://whatever/server/path", nil)
output := DebugHTTP(req, true, false)
expected := fmt.Sprintf("%s\n", "---------------- request")
expected += fmt.Sprintf("%s\r\n", "GET /server/path HTTP/1.1")
expected += fmt.Sprintf("%s\r\n\r\n\n", "Host: whatever")
expected += fmt.Sprintf("%s\n", "---------------- No Body")
expected += fmt.Sprintf("%s\n", "---------------- end")
assert.Equal(t, expected, output)
})
t.Run("debug with body", func(t *testing.T) {
req, _ := http.NewRequest(http.MethodGet, "https://whatever/server/path", nil)
output := DebugHTTP(req, true, false)
expected := fmt.Sprintf("%s\n", "---------------- request")
expected += fmt.Sprintf("%s\r\n", "GET /server/path HTTP/1.1")
expected += fmt.Sprintf("%s\r\n\r\n\n", "Host: whatever")
expected += fmt.Sprintf("%s\n", "---------------- No Body")
expected += fmt.Sprintf("%s\n", "---------------- end")
assert.Equal(t, expected, output)
})
}
func TestDebugHTTPDump(t *testing.T) {
t.Run("no debug", func(t *testing.T) {
req, _ := http.NewRequest(http.MethodGet, "https://whatever/server/path", nil)
output := captureOutput(func() {
DebugHTTPDump(req, false, false)
})
assert.Equal(t, ``, output)
})
t.Run("debug no body", func(t *testing.T) {
req, _ := http.NewRequest(http.MethodGet, "https://whatever/server/path", nil)
output := captureOutput(func() {
DebugHTTPDump(req, true, false)
})
expected := fmt.Sprintf("%s\n", "---------------- request")
expected += fmt.Sprintf("%s\r\n", "GET /server/path HTTP/1.1")
expected += fmt.Sprintf("%s\r\n\r\n\n", "Host: whatever")
expected += fmt.Sprintf("%s\n", "---------------- No Body")
expected += fmt.Sprintf("%s\n", "---------------- end")
assert.Contains(t, output, expected)
})
t.Run("debug with body", func(t *testing.T) {
req, _ := http.NewRequest(http.MethodGet, "https://whatever/server/path", nil)
output := captureOutput(func() {
DebugHTTPDump(req, true, false)
})
expected := fmt.Sprintf("%s\n", "---------------- request")
expected += fmt.Sprintf("%s\r\n", "GET /server/path HTTP/1.1")
expected += fmt.Sprintf("%s\r\n\r\n\n", "Host: whatever")
expected += fmt.Sprintf("%s\n", "---------------- No Body")
expected += fmt.Sprintf("%s\n", "---------------- end")
assert.Contains(t, output, expected)
})
}
func TestGetIP(t *testing.T) {
t.Run("get ip X-Real-IP header", func(t *testing.T) {
req, _ := http.NewRequest(http.MethodGet, "https://whatever/server/path", nil)
req.Header.Set(XRealIP, "1.2.3.4")
ip := GetIP(req)
assert.Equal(t, "1.2.3.4", ip)
})
t.Run("get ip X-Forwarder-For header", func(t *testing.T) {
req, _ := http.NewRequest(http.MethodGet, "https://whatever/server/path", nil)
req.Header.Set(XForwardedFor, "1.2.3.4")
ip := GetIP(req)
assert.Equal(t, "1.2.3.4", ip)
})
t.Run("get ip RemoteAddr", func(t *testing.T) {
req, _ := http.NewRequest(http.MethodGet, "https://whatever/server/path", nil)
req.Header.Set(XForwardedFor, "")
ip := GetIP(req)
assert.Equal(t, "", ip)
})
}
func TestHTTPDownload(t *testing.T) {
t.Run("HTTPDownload headers", func(t *testing.T) {
rr := httptest.NewRecorder()
HTTPDownload(rr, "whatever", "file.txt", 123)
assert.Equal(t, OctetStream, rr.Header().Get(ContentType))
assert.Equal(t, TransferEncodingBinary, rr.Header().Get(ContentTransferEncoding))
assert.Equal(t, KeepAlive, rr.Header().Get(Connection))
assert.Equal(t, "0", rr.Header().Get(Expires))
assert.Equal(t, CacheControlMustRevalidate, rr.Header().Get(CacheControl))
assert.Equal(t, PragmaPublic, rr.Header().Get(Pragma))
})
t.Run("content-description", func(t *testing.T) {
rr := httptest.NewRecorder()
HTTPDownload(rr, "whatever", "file.txt", 123)
assert.Equal(t, "whatever", rr.Header().Get(ContentDescription))
})
t.Run("content-disposition", func(t *testing.T) {
rr := httptest.NewRecorder()
HTTPDownload(rr, "whatever", "file.txt", 123)
assert.Equal(t, "attachment; filename=file.txt", rr.Header().Get(ContentDisposition))
})
t.Run("content-length", func(t *testing.T) {
rr := httptest.NewRecorder()
HTTPDownload(rr, "whatever", "file.txt", 123)
assert.Equal(t, "123", rr.Header().Get(ContentLength))
})
}