-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
138 lines (111 loc) · 4.06 KB
/
main_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
package main
import (
"bytes"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/http/httptest"
"net/textproto"
"os"
"path/filepath"
"testing"
log "github.com/sirupsen/logrus"
)
func TestMain(m *testing.M) {
log.SetLevel(log.PanicLevel)
os.Exit(m.Run())
}
func TestWelcome(t *testing.T) {
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(Welcome)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
if rr.Body.String() != welcomeTemplate {
t.Errorf("handler returned unexpected body: got %v want %v",
rr.Body.String(), welcomeTemplate)
}
}
func newfileUploadRequest(uri string, params map[string]string, paramName, path string, contentType string) (*http.Request, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer func() {
err := file.Close()
if err != nil {
log.WithError(err).WithField("Path", path).Warn("Unable to close file")
}
}()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
h := make(textproto.MIMEHeader)
h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%s"; filename="%s"`, paramName, filepath.Base(path)))
h.Set("Content-Type", contentType)
part, err := writer.CreatePart(h)
//part, err := writer.CreateFormFile(paramName, filepath.Base(path))
if err != nil {
return nil, err
}
_, err = io.Copy(part, file)
for key, val := range params {
_ = writer.WriteField(key, val)
}
err = writer.Close()
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", uri, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", writer.FormDataContentType())
return req, err
}
func testUpload(t *testing.T, testname string, paramName string, path string, expected string, expectedStatusCode int, contentType string) {
t.Run(testname, func(t *testing.T) {
req, err := newfileUploadRequest("/", nil, paramName, path, contentType)
if err != nil {
t.Fatal(err)
}
req.Header.Add("Content-Type", contentType)
rr := httptest.NewRecorder()
handler := http.HandlerFunc(ReceiveFile)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != expectedStatusCode {
t.Errorf("handler returned wrong status code: got %v want %v",
status, expectedStatusCode)
}
log.Debug(len(rr.Body.String()))
if expected != "" && rr.Body.String() != expected {
t.Errorf("handler returned unexpected body: got %v want %v",
rr.Body.String(), expected)
}
})
}
func TestReceiveFile(t *testing.T) {
// generic XLSX
expected := `{"name":"sample.xlsx","spreadsheets":[{"name":"Sheet 1","columns":["Column0","Column1","Column2","Column3","Column4"],"rows":[["1","2","3","4","5"],["a","b","c","d","e"]]},{"name":"Sheet 2","columns":["Column0","Column1","Column2"],"rows":[["1","2","3"],["a","b","c"]]}]}` + "\n"
testUpload(t, "sample.xlsx", "file", "testfiles/sample.xlsx", expected, 200, xlsxMimeType)
// empty XLSX
expected2 := `{"name":"empty.xlsx","spreadsheets":[{"name":"Sheet 1","columns":null,"rows":null}]}` + "\n"
testUpload(t, "empty.xlsx", "file", "testfiles/empty.xlsx", expected2, 200, xlsxMimeType)
// empty CSV file
expected3 := `{"http_error_code":500,"http_error":"Internal Server Error","message":"invalid XLSX stream"}` + "\n"
testUpload(t, "wrong.csv", "file", "testfiles/wrong.csv", expected3, 500, xlsxMimeType)
// not sending as `file` in the POST body (also captures sending empty body)
expected4 := `{"http_error_code":500,"http_error":"Internal Server Error","message":"parameter named 'file' not found in form"}` + "\n"
testUpload(t, "wrong param name", "upload", "testfiles/sample.xlsx", expected4, 500, xlsxMimeType)
// ZIP file renamed to XLSX
expected5 := `{"http_error_code":500,"http_error":"Internal Server Error","message":"invalid XLSX stream"}` + "\n"
testUpload(t, "wrong.xslx", "file", "testfiles/wrong.xslx", expected5, 500, xlsxMimeType)
// JSON
testUpload(t, "test.json", "file", "testfiles/test.json", "", 200, jsonMimeType)
}