-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain_test.go
88 lines (81 loc) · 2.21 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
package main
import (
"bytes"
"io"
"log"
"mime/multipart"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"github.com/gin-gonic/gin"
)
func TestPostFileForConversion(t *testing.T) {
gin.SetMode(gin.TestMode)
testRouter := SetupRouter()
resp, err := convertFile(testRouter, "testdata", "example.docx")
if err != nil {
t.Errorf("Create request failed %d", err)
}
if resp.Code != 200 {
t.Errorf("Expected 200, received %d.", resp.Code)
}
cdh := resp.Header().Get("Content-Disposition")
if !strings.HasSuffix(cdh, "example.pdf") {
t.Errorf("Wrong Content-Disposition header %s.", cdh)
}
if resp.Body.Len() < 1 {
t.Errorf("Body should not be empty")
}
}
func TestPostFileForConversionWithTimeout(t *testing.T) {
gin.SetMode(gin.TestMode)
testRouter := SetupRouter()
resp, err := convertFileWitTimeout(testRouter, "testdata", "example.docx", "10ms")
if err != nil {
t.Errorf("Create request failed %d", err)
}
if resp.Code != 504 {
t.Errorf("Expected 504, received %d.", resp.Code)
}
}
func convertFile(router *gin.Engine, dir string, file string) (*httptest.ResponseRecorder, error) {
return convertFileWitTimeout(router, dir, file, "10s")
}
func convertFileWitTimeout(router *gin.Engine, dir string, file string, timeout string) (*httptest.ResponseRecorder, error) {
inputFile, _ := filepath.Abs(filepath.Join("testdata", "example.docx"))
req, err := multipartRequest("/to/pdf?timeout="+timeout, inputFile)
if err != nil {
return nil, err
}
resp := httptest.NewRecorder()
router.ServeHTTP(resp, req)
return resp, nil
}
func multipartRequest(uri string, filePath string) (*http.Request, error) {
log.Printf("Create multipart request uri:%s, file:%s", uri, filePath)
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("file", filepath.Base(filePath))
if err != nil {
return nil, err
}
_, err = io.Copy(part, file)
if err != nil {
return nil, err
}
err = writer.Close()
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", uri, body)
req.Header.Set("Content-Type", writer.FormDataContentType())
return req, err
}