Skip to content

Commit 252e595

Browse files
committed
test: 增加断点下载测试用例
1 parent 216b0db commit 252e595

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ data*
33
go.sum
44
build/
55
!distributed/data
6+
willchange
7+
token_location.tmp

‎distributed/test/client.go‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ func putIncorrect(file string) error {
165165
}
166166

167167
// size 第一次上传的大小
168+
// 断点续传测试函数
168169
func putBigFile(file string, size int64) error {
169170
utils.Log.Printf(utils.Info, "open file, %s\n", file)
170171
f, e := os.Open(filepath + file)
@@ -268,4 +269,36 @@ func putBigFile(file string, size int64) error {
268269
return nil
269270
}
270271

272+
func getBigFile(file string, size int64) error {
273+
r, _ := http.NewRequest(http.MethodGet, apihost+file, nil)
274+
r.Header.Set("Range", fmt.Sprintf("bytes=%v-", size))
275+
client := http.Client{}
276+
response, err := client.Do(r)
277+
if err != nil {
278+
return err
279+
}
280+
if response.StatusCode < 200 || response.StatusCode >= 300 {
281+
return fmt.Errorf("size get %s", response.Status)
282+
}
283+
ran := response.Header.Get("content-range")
284+
fmt.Println(ran)
285+
data_1, _ := os.Create("data_1")
286+
defer data_1.Close()
287+
io.Copy(data_1, response.Body)
271288

289+
// 第二次 获取全部数据
290+
r.Header.Set("Range", "bytes=0-")
291+
response, err = client.Do(r)
292+
if err != nil {
293+
return err
294+
}
295+
if response.StatusCode < 200 || response.StatusCode >= 300 {
296+
return fmt.Errorf("all get %s", response.Status)
297+
}
298+
ran = response.Header.Get("content-range")
299+
fmt.Println(ran) // 应该是空的
300+
data_all, _ := os.Create("data_all")
301+
defer data_all.Close()
302+
io.Copy(data_all, response.Body)
303+
return nil
304+
}

‎distributed/test/client_test.go‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func Test_putBigFile(t *testing.T) {
140140
// TODO: Add test cases.
141141
{"big put test", args{
142142
file: miya,
143-
size: 1<<18,
143+
size: 1<<18+1234,
144144
}, false},
145145
//{"big put test1", args{
146146
// file: somefile,
@@ -156,3 +156,25 @@ func Test_putBigFile(t *testing.T) {
156156
}
157157
}
158158

159+
func Test_getBigFile(t *testing.T) {
160+
type args struct {
161+
file string
162+
size int64
163+
}
164+
tests := []struct {
165+
name string
166+
args args
167+
wantErr bool
168+
}{
169+
{"big get test", args{
170+
miya, 900000,
171+
}, false},
172+
}
173+
for _, tt := range tests {
174+
t.Run(tt.name, func(t *testing.T) {
175+
if err := getBigFile(tt.args.file, tt.args.size); (err != nil) != tt.wantErr {
176+
t.Errorf("getBigFile() error = %v, wantErr %v", err, tt.wantErr)
177+
}
178+
})
179+
}
180+
}

0 commit comments

Comments
 (0)