-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload_test.go
110 lines (101 loc) · 2.36 KB
/
upload_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
package smmssdk
import (
"context"
"image"
"image/png"
"math/rand"
"os"
"testing"
)
func TestUpload(t *testing.T) {
ctx := context.Background()
client := NewClient(
WithLogEnabled(false),
WithSecretToken(getSecretToken()),
)
t.Run("invalid_source", func(t *testing.T) {
req := NewUploadRequest()
_, err := client.Upload(ctx, req)
if err == nil {
t.Fatalf("want non-nil but got %v", err)
}
if err.Error() != "invalid request source is empty" {
t.Fatalf("want error msg but got:%v", err)
}
})
t.Run("upload-smfile", func(t *testing.T) {
tmpfile, err := os.CreateTemp(os.TempDir(), "test-temp-*.png")
if err != nil {
t.Fatal(err)
}
//t.Logf("temp file path=%s", tmpfile.Name())
defer func() {
err := os.Remove(tmpfile.Name())
if err != nil {
t.Fatal(err)
}
//t.Log("remove temp file")
}()
defer func() {
if err := tmpfile.Close(); err != nil {
t.Fatal(err)
}
//t.Log("close temp file")
}()
// temp image data
//img := image.NewRGBA(image.Rect(0, 0, 100, 100))
//rand.Seed(time.Now().UnixNano())
rect := image.Rect(
rand.Intn(100),
rand.Intn(100),
rand.Intn(100),
rand.Intn(100),
)
img := image.NewRGBA(rect)
err = png.Encode(tmpfile, img)
if err != nil {
t.Fatal(err)
}
// reset the file pointer to the beginning
if _, err := tmpfile.Seek(0, 0); err != nil {
t.Fatal(err)
}
req := NewUploadRequest()
//req.Smfile = tmpfile.Name() // ok
req.SmfileStream = tmpfile
rsp, err := client.Upload(ctx, req)
if err != nil {
t.Fatalf("upload request failed, error:%v", err)
}
if rsp == nil {
t.Fatalf("want non-nil but got %v", rsp)
}
if rsp.Success != true {
t.Fatalf("want true but got %v", rsp.Success)
}
if rsp.Code != "success" {
t.Fatalf("want success but got %s", rsp.Code)
}
if rsp.RequestId == "" {
t.Fatalf("want non-empty but got %s", rsp.RequestId)
}
if rsp.Data == nil {
t.Fatalf("want non-nil but got %v", rsp.Data)
}
})
}
func TestTempFile(t *testing.T) {
// create a temporary file for testing
tmpfile, err := os.CreateTemp(os.TempDir(), "test-image-*.png")
if err != nil {
t.Fatal(err)
}
defer func() {
// t.Logf("remove temp file name=%s", tmpfile.Name())
// name=/var/folders/kz/w7qjphf50w5121d_fb6r2fl40000gn/T/test-image-3681701983.png
err := os.Remove(tmpfile.Name())
if err != nil {
t.Fatal(err)
}
}()
}