-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch_test.go
383 lines (364 loc) · 12.9 KB
/
fetch_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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
Author: John Connor Sanders
License: Apache Version 2.0
Version: 0.0.2
Released: 01/29/2021
Copyright (c) 2021 John Connor Sanders
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
----------------FETCH--------------------
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
*/
package fetch
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"mime"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
"time"
)
// jsonData
type jsonData struct {
Id string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
}
// fileData
type fileData struct {
Id string
Name string
Type string
Content []byte
}
// testDataCache
type testDataCache struct {
JSONData []*jsonData
FileData []*fileData
}
// getJSONData - returns all jsonData in the testDataCache
func (tc *testDataCache) getJSONData() []*jsonData {
return tc.JSONData
}
// getJSONDatum - returns a specific jsonData entry from the testDataCache
func (tc *testDataCache) getJSONDatum(id string) (*jsonData, error) {
for _, td := range tc.JSONData {
if td.Id == id {
return td, nil
}
}
return &jsonData{}, errors.New(id + " Not found!")
}
// postJSONData - adds a new jsonData entry to the testDataCache
func (tc *testDataCache) postJSONData(td *jsonData) {
td.Id = strconv.Itoa(len(tc.JSONData))
tc.JSONData = append(tc.JSONData, td)
}
// getFileData - returns a fileData entry from the testDataCache
func (tc *testDataCache) getFileData(id string) (*fileData, error) {
for _, fd := range tc.FileData {
if fd.Id == id {
return fd, nil
}
}
return &fileData{}, errors.New(id + " Not found!")
}
// postFileData - adds a new fileData entry to the testDataCache
func (tc *testDataCache) postFileData(fd *fileData) {
fd.Id = strconv.Itoa(len(tc.FileData))
tc.FileData = append(tc.FileData, fd)
}
// newTestDataCache returns a pointer to a new testDataCache struct
func newTestDataCache(td []*jsonData, fd []*fileData) *testDataCache {
return &testDataCache{
td,
fd,
}
}
// testErr
type testErr struct {
Code int `json:"code"`
Text string `json:"text"`
}
// testRes
type testRes struct {
Code int `json:"code"`
Id string `json:"id"`
}
// TestFetch...
func TestFetch(t *testing.T) {
t.Run("JSONGet", testJSONGet)
t.Run("JSONPost", testJSONPost)
t.Run("FileGet", testFileGet)
t.Run("FilePost", testFilePost)
}
// testJSONGet
func testJSONGet(t *testing.T) {
fmt.Println("\n<-----------BEGINNING 1 of 4: testJSONGet...")
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
fmt.Println("Expected 'GET' received: ", r.Method)
t.Errorf("Expected 'GET' received: '%s'", r.Method)
}
if r.URL.EscapedPath() != "/data/1" {
fmt.Println("Incorrect url endpoint: ", r.URL.EscapedPath())
t.Errorf("Incorrect url endpoint: '%s'", r.URL.EscapedPath())
}
dataCache := newTestDataCache([]*jsonData{{Id: "1", Name: "test", Type: "GET"}}, []*fileData{})
id := strings.TrimPrefix(r.URL.Path, "/data/")
td, err := dataCache.getJSONDatum(id)
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
if err != nil {
w.WriteHeader(http.StatusNotFound)
tErr := testErr{Code: http.StatusNotFound, Text: err.Error()}
if err = json.NewEncoder(w).Encode(tErr); err != nil {
fmt.Println("Error encoding testErr struct into JSON for response: ", err.Error())
t.Errorf("Error encoding testErr struct into JSON for response: '%s'", err.Error())
}
return
}
w.WriteHeader(http.StatusOK)
if err = json.NewEncoder(w).Encode(*td); err != nil {
fmt.Println("Error encoding jsonData struct into JSON for response: ", err.Error())
t.Errorf("Error encoding jsonData struct into JSON for response: '%s'", err.Error())
}
return
}))
defer ts.Close()
api := ts.URL
endPoint := fmt.Sprintf("%s/data/%s", api, "1")
fmt.Printf("TEST Url: GET %s\n", endPoint)
method := "GET"
f, err := NewFetch(endPoint, method, JSONDefaultHeaders(), nil)
if err != nil {
fmt.Println("Error Initializing New Fetch: ", err.Error())
t.Errorf("Error Initializing New Fetch: %v", err.Error())
}
err = f.Execute("")
if err != nil {
fmt.Println("Error Executing Fetch Request: ", err.Error())
t.Errorf("Error Executing Fetch Request: %v", err.Error())
}
f.Resolve()
if f.Res.StatusCode != 200 {
fmt.Println("Error Executing and Resolving Test Request: ", f.Res.Status)
t.Errorf("Error Executing and Resolving Test Request: %v", f.Res.Status)
}
fmt.Println("<-----------COMPLETE 1 of 4: testJSONGet")
}
// testJSONPost
func testJSONPost(t *testing.T) {
fmt.Println("\n<-----------BEGINNING 2 of 4: testJSONPost...")
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
fmt.Println("Expected 'POST' received: ", r.Method)
t.Errorf("Expected 'POST' received: '%s'", r.Method)
}
if r.URL.EscapedPath() != "/data" {
fmt.Println("Incorrect url endpoint: ", r.URL.EscapedPath())
t.Errorf("Incorrect url endpoint: '%s'", r.URL.EscapedPath())
}
var td jsonData
dataCache := newTestDataCache([]*jsonData{}, []*fileData{})
body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
if err != nil {
fmt.Println("Error encoding json post request body into jsonData struct: ", err.Error())
t.Errorf("Error encoding json post request body into jsonData struct: '%s'", err.Error())
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
if err = json.Unmarshal(body, &td); err != nil {
w.WriteHeader(http.StatusUnprocessableEntity)
tErr := testErr{Code: http.StatusUnprocessableEntity, Text: err.Error()}
if err = json.NewEncoder(w).Encode(tErr); err != nil {
fmt.Println("Error encoding testErr struct into JSON for response: ", err.Error())
t.Errorf("Error encoding testErr struct into JSON for response: '%s'", err.Error())
}
return
}
dataCache.postJSONData(&td)
w.WriteHeader(http.StatusCreated)
if err = json.NewEncoder(w).Encode(td); err != nil {
fmt.Println("Error encoding error into JSON for response: ", err.Error())
t.Errorf("Error encoding error into JSON for response: '%s'", err.Error())
}
return
}))
defer ts.Close()
api := ts.URL
endPoint := fmt.Sprintf("%s/data", api)
fmt.Printf("TEST Url: POST %s\n", endPoint)
method := "POST"
body := []byte(`{"name":"test","type":"POST"}`)
f, err := NewFetch(endPoint, method, JSONDefaultHeaders(), bytes.NewBuffer(body))
if err != nil {
fmt.Println("Error Initializing New Fetch: ", err.Error())
t.Errorf("Error Initializing New Fetch: %v", err.Error())
}
err = f.Execute("")
if err != nil {
fmt.Println("Error Executing Fetch Request: ", err.Error())
t.Errorf("Error Executing Fetch Request: %v", err.Error())
}
f.Resolve()
if f.Res.StatusCode != 201 {
fmt.Println("Error Executing and Resolving Test Request: ", f.Res.Status)
t.Errorf("Error Executing and Resolving Test Request: %v", f.Res.Status)
}
fmt.Println("<-----------COMPLETE 2 of 4: testJSONPost")
}
// testFileGet
func testFileGet(t *testing.T) {
fmt.Println("\n<-----------BEGINNING 3 of 4: testFileGet...")
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
fmt.Println("Expected 'GET' received: ", r.Method)
t.Errorf("Expected 'GET' received: '%s'", r.Method)
}
if r.URL.EscapedPath() != "/files/1" {
fmt.Println("Incorrect url endpoint: ", r.URL.EscapedPath())
t.Errorf("Incorrect url endpoint: '%s'", r.URL.EscapedPath())
}
fc := []byte("This is the GET test file's contents!")
dataCache := newTestDataCache([]*jsonData{}, []*fileData{{Id: "1", Name: "test.txt", Type: "txt", Content: fc}})
id := strings.TrimPrefix(r.URL.Path, "/files/")
fd, err := dataCache.getFileData(id)
if err != nil {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusNotFound)
tErr := testErr{Code: http.StatusNotFound, Text: err.Error()}
if err = json.NewEncoder(w).Encode(tErr); err != nil {
fmt.Println("Error encoding testErr struct into JSON for response: ", err.Error())
t.Errorf("Error encoding testErr struct into JSON for response: '%s'", err.Error())
}
return
}
modTime := time.Now()
cd := mime.FormatMediaType("attachment", map[string]string{"filename": fd.Name})
w.Header().Set("Content-Disposition", cd)
w.Header().Set("Content-Type", "application/octet-stream")
bReader := bytes.NewReader(fd.Content)
http.ServeContent(w, r, fd.Name, modTime, bReader)
}))
defer ts.Close()
api := ts.URL
endPoint := fmt.Sprintf("%s/files/%s", api, "1")
fmt.Printf("TEST Url: GET %s\n", endPoint)
method := "GET"
f, err := NewFetch(endPoint, method, JSONDefaultHeaders(), nil)
if err != nil {
fmt.Println("Error Initializing New Fetch: ", err.Error())
t.Errorf("Error Initializing New Fetch: %v", err.Error())
}
err = f.Execute("")
if err != nil {
fmt.Println("Error Executing Fetch Request: ", err.Error())
t.Errorf("Error Executing Fetch Request: %v", err.Error())
}
f.Resolve()
if f.Res.StatusCode != 200 {
fmt.Println("Error Executing and Resolving Test Request: ", f.Res.Status)
t.Errorf("Error Executing and Resolving Test Request: %v", f.Res.Status)
}
d, err := ioutil.ReadAll(f.Res.Body)
if err != nil {
fmt.Println("Error reading response file body: ", err.Error())
t.Errorf("Error reading response file body: %v", err.Error())
}
if string(d) != "This is the GET test file's contents!" {
fmt.Println("Expected file contents and returned do not match: ", string(d))
t.Errorf("Expected file contents and returned do not match: %v", string(d))
}
fmt.Println("<-----------COMPLETE 3 of 4: testFileGet")
}
// testFilePost
func testFilePost(t *testing.T) {
fmt.Println("\n<-----------BEGINNING 4 of 4: testFilePost...")
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
fmt.Println("Expected 'POST' received: ", r.Method)
t.Errorf("Expected 'POST' received: '%s'", r.Method)
}
if r.URL.EscapedPath() != "/files" {
fmt.Println("Incorrect url endpoint: ", r.URL.EscapedPath())
t.Errorf("Incorrect url endpoint: '%s'", r.URL.EscapedPath())
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
err := r.ParseMultipartForm(32 << 20)
if err != nil {
fmt.Println("Error parsing multipart form", err.Error())
w.WriteHeader(http.StatusBadRequest)
tErr := testErr{Code: http.StatusBadRequest, Text: err.Error()}
if err = json.NewEncoder(w).Encode(tErr); err != nil {
fmt.Println("Error encoding testErr struct into JSON for response: ", err.Error())
t.Errorf("Error encoding testErr struct into JSON for response: '%s'", err.Error())
}
return
}
dataCache := newTestDataCache([]*jsonData{}, []*fileData{})
var tf fileData
file, h, err := r.FormFile("file")
if err != nil {
fmt.Println("Error getting form file", err.Error())
w.WriteHeader(http.StatusBadRequest)
tErr := testErr{Code: http.StatusBadRequest, Text: err.Error()}
if err = json.NewEncoder(w).Encode(tErr); err != nil {
fmt.Println("Error encoding testErr struct into JSON for response: ", err.Error())
t.Errorf("Error encoding testErr struct into JSON for response: '%s'", err.Error())
}
return
}
defer file.Close()
buf := bytes.NewBuffer(nil)
if _, err = io.Copy(buf, file); err != nil {
w.WriteHeader(http.StatusInternalServerError)
tErr := testErr{Code: http.StatusInternalServerError, Text: err.Error()}
if err = json.NewEncoder(w).Encode(tErr); err != nil {
fmt.Println("Error encoding testErr struct into JSON for response: ", err.Error())
t.Errorf("Error encoding testErr struct into JSON for response: '%s'", err.Error())
}
return
}
tf.Name = h.Filename
tf.Content = buf.Bytes()
tf.Type = "txt"
if strings.Contains(h.Filename, ".") {
tf.Type = strings.Split(h.Filename, ".")[1]
}
dataCache.postFileData(&tf)
w.WriteHeader(http.StatusOK)
res := testRes{http.StatusOK, tf.Id}
if err = json.NewEncoder(w).Encode(res); err != nil {
fmt.Println("Error encoding testRes struct into JSON for response: ", err.Error())
t.Errorf("Error encoding testRes struct into JSON for response: '%s'", err.Error())
}
return
}))
defer ts.Close()
api := ts.URL
endPoint := fmt.Sprintf("%s/files", api)
fmt.Printf("TEST Url: POST %s\n", endPoint)
method := "POST"
fContent := []byte("This is the POST test file's contents!")
f, err := NewFileFetch("test.txt", endPoint, method, DefaultHeaders(), bytes.NewBuffer(fContent))
if err != nil {
fmt.Println("Error Initializing New File Fetch: ", err.Error())
t.Errorf("Error Initializing New File Fetch: %v", err.Error())
}
err = f.Execute("")
if err != nil {
fmt.Println("Error Executing File Fetch Request: ", err.Error())
t.Errorf("Error Executing File Fetch Request: %v", err.Error())
}
f.Resolve()
if f.Res.StatusCode != 200 {
fmt.Println("Error Executing and Resolving Test Request: ", f.Res.Status)
t.Errorf("Error Executing and Resolving Test Request: %v", f.Res.Status)
}
fmt.Println("<-----------COMPLETE 4 of 4: testFilePost")
}