Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close response bodies #15

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion _example/object/append.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,16 @@ func main() {
panic(err)
return
}
defer resp.Body.Close()
fmt.Printf("%s\n", resp.Status)

// head
if _, err = c.Object.Head(ctx, name, nil); err != nil {
resp, err = c.Object.Head(ctx, name, nil)
if err != nil {
panic(err)
return
}
defer resp.Body.Close()

// 再次 append
data = genBigData(1024 * 1024 * 5)
Expand All @@ -72,5 +75,6 @@ func main() {
if err != nil {
panic(err)
}
defer resp.Body.Close()
fmt.Printf("%s\n", resp.Status)
}
2 changes: 2 additions & 0 deletions _example/object/completeMultipartUpload.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ func uploadPart(c *cos.Client, name string, uploadID string, blockSize, n int) s
resp, err := c.Object.UploadPart(
context.Background(), name, uploadID, n, f, nil,
)
defer resp.Body.Close()

if err != nil {
panic(err)
}
Expand Down
5 changes: 3 additions & 2 deletions _example/object/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ func main() {
expected := "test"
f := strings.NewReader(expected)

_, err := c.Object.Put(context.Background(), source, f, nil)
resp, err := c.Object.Put(context.Background(), source, f, nil)
if err != nil {
panic(err)
}
defer resp.Body.Close()

soruceURL := fmt.Sprintf("%s/%s", u.Host, source)
dest := fmt.Sprintf("test/objectMove_%d.go", time.Now().Nanosecond())
Expand All @@ -50,7 +51,7 @@ func main() {
}
fmt.Printf("%+v\n\n", res)

resp, err := c.Object.Get(context.Background(), dest, nil)
resp, err = c.Object.Get(context.Background(), dest, nil)
if err != nil {
panic(err)
}
Expand Down
3 changes: 2 additions & 1 deletion _example/object/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ func main() {

name := "test/objectPut.go"

_, err := c.Object.Delete(context.Background(), name)
resp, err := c.Object.Delete(context.Background(), name)
if err != nil {
panic(err)
}
defer resp.Body.Close()
}
3 changes: 2 additions & 1 deletion _example/object/head.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ func main() {
})

name := "test/hello.txt"
_, err := c.Object.Head(context.Background(), name, nil)
resp, err := c.Object.Head(context.Background(), name, nil)
if err != nil {
panic(err)
}
defer resp.Body.Close()
}
1 change: 1 addition & 0 deletions _example/object/listParts.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func uploadPart(c *cos.Client, name string, uploadID string, blockSize, n int) s
if err != nil {
panic(err)
}
defer resp.Body.Close()
fmt.Printf("%s\n", resp.Status)
return resp.Header.Get("Etag")
}
Expand Down
5 changes: 3 additions & 2 deletions _example/object/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ func main() {

name := "test/hello.txt"
opt := &cos.ObjectOptionsOptions{
Origin: "http://www.qq.com",
Origin: "http://www.qq.com",
AccessControlRequestMethod: "PUT",
}
_, err := c.Object.Options(context.Background(), name, opt)
resp, err := c.Object.Options(context.Background(), name, opt)
if err != nil {
panic(err)
}
defer resp.Body.Close()
}
3 changes: 2 additions & 1 deletion _example/object/put.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ func main() {
name := "test/objectPut.go"
f := strings.NewReader("test")

_, err := c.Object.Put(context.Background(), name, f, nil)
resp, err := c.Object.Put(context.Background(), name, f, nil)
if err != nil {
panic(err)
}
defer resp.Body.Close()

// 测试上传以及特殊字符
name = "test/put_ + !'()* option.go"
Expand Down
6 changes: 4 additions & 2 deletions _example/object/putACL.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ func main() {
},
}
name := "test/hello.txt"
_, err := c.Object.PutACL(context.Background(), name, opt)
resp, err := c.Object.PutACL(context.Background(), name, opt)
if err != nil {
panic(err)
}
defer resp.Body.Close()

// with body
opt = &cos.ObjectPutACLOptions{
Expand All @@ -57,8 +58,9 @@ func main() {
},
}

_, err = c.Object.PutACL(context.Background(), name, opt)
resp, err = c.Object.PutACL(context.Background(), name, opt)
if err != nil {
panic(err)
}
defer resp.Body.Close()
}
3 changes: 2 additions & 1 deletion _example/object/putWithPresignedURL.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ func main() {
f := strings.NewReader("test")

// 通过生成签名 header 上传文件
_, err := c.Object.Put(ctx, name, f, nil)
resp, err := c.Object.Put(ctx, name, f, nil)
if err != nil {
panic(err)
}
defer resp.Body.Close()

// 获取预签名授权 URL
opt := &cos.ObjectPutOptions{
Expand Down
3 changes: 2 additions & 1 deletion _example/object/uploadFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ func main() {
}
fmt.Println(s.Size())

_, err = c.Object.Put(context.Background(), name, f, nil)
resp, err = c.Object.Put(context.Background(), name, f, nil)
if err != nil {
panic(err)
}
defer resp.Body.Close()
}
3 changes: 2 additions & 1 deletion _example/object/uploadPart.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ func main() {
uploadID := up.UploadID

f := strings.NewReader("test heoo")
_, err := c.Object.UploadPart(
resp, err := c.Object.UploadPart(
context.Background(), name, uploadID, 1, f, nil,
)
if err != nil {
panic(err)
}
defer resp.Body.Close()
}
1 change: 1 addition & 0 deletions codelingo.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
tenets:
- import: codelingo/effective-go
- import: codelingo/code-review-comments
- import: codelingo/mozillazg-go-cos
30 changes: 21 additions & 9 deletions object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func TestObjectService_Get(t *testing.T) {
if err != nil {
t.Fatalf("Object.Get returned error: %v", err)
}
defer resp.Body.Close()

b, _ := ioutil.ReadAll(resp.Body)
ref := string(b)
Expand Down Expand Up @@ -76,6 +77,7 @@ func TestObjectService_Get_with_PresignedURL(t *testing.T) {
if err != nil {
t.Fatalf("Object.Get returned error: %v", err)
}
defer resp.Body.Close()

b, _ := ioutil.ReadAll(resp.Body)
ref := string(b)
Expand Down Expand Up @@ -114,10 +116,11 @@ func TestObjectService_Put(t *testing.T) {
})

r := bytes.NewReader([]byte("hello"))
_, err := client.Object.Put(context.Background(), name, r, opt)
resp, err := client.Object.Put(context.Background(), name, r, opt)
if err != nil {
t.Fatalf("Object.Put returned error: %v", err)
}
defer resp.Body.Close()

}

Expand Down Expand Up @@ -153,10 +156,11 @@ func TestObjectService_Put_with_PresignedURL(t *testing.T) {
})

r := bytes.NewReader([]byte("hello"))
_, err := client.Object.Put(context.Background(), name, r, opt)
resp, err := client.Object.Put(context.Background(), name, r, opt)
if err != nil {
t.Fatalf("Object.Put returned error: %v", err)
}
defer resp.Body.Close()

}

Expand Down Expand Up @@ -192,10 +196,12 @@ func TestObjectService_Put_not_close(t *testing.T) {
})

r := newTraceCloser(bytes.NewReader([]byte("hello")))
_, err := client.Object.Put(context.Background(), name, r, opt)
resp, err := client.Object.Put(context.Background(), name, r, opt)
if err != nil {
t.Fatalf("Object.Put returned error: %v", err)
}
defer resp.Body.Close()

if r.Called {
t.Fatal("Should not close input")
}
Expand All @@ -212,10 +218,11 @@ func TestObjectService_Delete(t *testing.T) {
w.WriteHeader(http.StatusNoContent)
})

_, err := client.Object.Delete(context.Background(), name)
resp, err := client.Object.Delete(context.Background(), name)
if err != nil {
t.Fatalf("Object.Delete returned error: %v", err)
}
defer resp.Body.Close()
}

func TestObjectService_Head(t *testing.T) {
Expand All @@ -232,10 +239,11 @@ func TestObjectService_Head(t *testing.T) {
IfModifiedSince: "Mon, 12 Jun 2017 05:36:19 GMT",
}

_, err := client.Object.Head(context.Background(), name, opt)
resp, err := client.Object.Head(context.Background(), name, opt)
if err != nil {
t.Fatalf("Object.Head returned error: %v", err)
}
defer resp.Body.Close()

}

Expand All @@ -251,14 +259,15 @@ func TestObjectService_Options(t *testing.T) {
})

opt := &ObjectOptionsOptions{
Origin: "www.qq.com",
Origin: "www.qq.com",
AccessControlRequestMethod: "PUT",
}

_, err := client.Object.Options(context.Background(), name, opt)
resp, err := client.Object.Options(context.Background(), name, opt)
if err != nil {
t.Fatalf("Object.Options returned error: %v", err)
}
defer resp.Body.Close()

}

Expand Down Expand Up @@ -297,10 +306,11 @@ func TestObjectService_Append(t *testing.T) {
})

r := bytes.NewReader([]byte("hello"))
_, err := client.Object.Append(context.Background(), name, position, r, opt)
resp, err := client.Object.Append(context.Background(), name, position, r, opt)
if err != nil {
t.Fatalf("Object.Append returned error: %v", err)
}
defer resp.Body.Close()
}

func TestObjectService_Append_not_close(t *testing.T) {
Expand Down Expand Up @@ -342,10 +352,12 @@ func TestObjectService_Append_not_close(t *testing.T) {
})

r := newTraceCloser(bytes.NewReader([]byte("hello")))
_, err := client.Object.Append(context.Background(), name, position, r, opt)
resp, err := client.Object.Append(context.Background(), name, position, r, opt)
if err != nil {
t.Fatalf("Object.Append returned error: %v", err)
}
defer resp.Body.Close()

if r.Called {
t.Fatal("Should not close input")
}
Expand Down