-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload_test.go
176 lines (158 loc) · 3.65 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
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
package smugmug_test
import (
"context"
"errors"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/bzimmer/smugmug"
)
func TestUpload(t *testing.T) {
t.Parallel()
a := assert.New(t)
tests := []struct {
name string
album string
filename string
err string
}{
{
name: "no album",
filename: "DSC33556.jpg",
err: "missing albumKey",
},
{
name: "with album",
album: "7dFHSm",
filename: "DSC33556.jpg",
},
{
name: "filename with spaces",
album: "7dFHSm",
filename: "This is a name with spaces.jpg",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
mux := http.NewServeMux()
mux.HandleFunc("/photo.jpg", func(w http.ResponseWriter, r *http.Request) {
a.Equal(http.MethodPut, r.Method)
a.Equal(url.PathEscape(tt.filename), r.Header.Get("X-Smug-FileName"))
http.ServeFile(w, r, "testdata/upload_CVvj69L.json")
})
svr := httptest.NewServer(mux)
defer svr.Close()
mg, err := smugmug.NewClient(smugmug.WithBaseURL(svr.URL), smugmug.WithUploadURL(svr.URL))
a.NoError(err)
up := &smugmug.Uploadable{
Name: tt.filename,
AlbumKey: tt.album,
}
upload, err := mg.Upload.Upload(context.TODO(), up)
if tt.err != "" {
a.Error(err)
a.Contains(err.Error(), tt.err)
} else {
a.NoError(err)
a.NotNil(upload)
}
})
}
}
type testUploadables struct {
albumKey string
sleep time.Duration
}
func (t *testUploadables) Uploadables(_ context.Context) (<-chan *smugmug.Uploadable, <-chan error) {
errc := make(chan error)
uploadablesc := make(chan *smugmug.Uploadable, 1)
go func() {
defer close(errc)
defer close(uploadablesc)
if t.sleep > 0 {
time.Sleep(t.sleep)
}
uploadablesc <- &smugmug.Uploadable{Name: "DSC33556.jpg", AlbumKey: t.albumKey}
}()
return uploadablesc, errc
}
func TestUploads(t *testing.T) {
t.Parallel()
a := assert.New(t)
tests := []struct {
name string
albumKey string
filename string
sleep time.Duration
f func(*smugmug.Upload, error)
}{
{
name: "nil album key",
albumKey: "",
f: func(up *smugmug.Upload, err error) {
a.Error(err)
a.Nil(up)
},
},
{
name: "passing",
albumKey: "7dFHSm",
filename: "testdata/upload_CVvj69L.json",
f: func(up *smugmug.Upload, err error) {
a.Nil(err)
a.Equal("/api/v2/image/CVvj69L-0", up.ImageURI)
},
},
{
name: "exceed deadline",
albumKey: "7dFHSm",
filename: "testdata/upload_CVvj69L.json",
sleep: time.Minute * 1,
f: func(up *smugmug.Upload, err error) {
a.Nil(up)
a.Error(err)
a.True(errors.Is(err, context.DeadlineExceeded))
},
},
}
for i := range tests {
tt := tests[i]
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, tt.filename)
}))
defer svr.Close()
var err error
mg, err := smugmug.NewClient(
smugmug.WithBaseURL(svr.URL),
smugmug.WithUploadURL(svr.URL),
smugmug.WithConcurrency(4))
a.NoError(err)
ctx := context.TODO()
if tt.sleep > 0 {
var cancel func()
ctx, cancel = context.WithTimeout(context.TODO(), time.Millisecond*10)
defer cancel()
}
uploadables := &testUploadables{albumKey: tt.albumKey, sleep: tt.sleep}
uploadc, errc := mg.Upload.Uploads(ctx, uploadables)
a.NotNil(uploadc)
a.NotNil(errc)
err = nil
var up *smugmug.Upload
select {
case <-ctx.Done():
err = ctx.Err()
case err = <-errc:
case up = <-uploadc:
}
tt.f(up, err)
})
}
}