-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
379 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"os" | ||
"reflect" | ||
|
||
"github.com/mozillazg/go-cos" | ||
"github.com/mozillazg/go-cos/debug" | ||
) | ||
|
||
type MockSender struct{} | ||
|
||
func (s *MockSender) Send(ctx context.Context, caller cos.Caller, req *http.Request) (*http.Response, error) { | ||
// return &http.Response{}, nil | ||
resp, _ := http.ReadResponse(bufio.NewReader(bytes.NewReader([]byte(`HTTP/1.1 200 OK | ||
Content-Length: 6 | ||
Accept-Ranges: bytes | ||
Connection: keep-alive | ||
Content-Type: text/plain; charset=utf-8 | ||
Date: Sat, 19 Jan 2019 08:25:27 GMT | ||
Etag: "f572d396fae9206628714fb2ce00f72e94f2258f" | ||
Last-Modified: Mon, 12 Jun 2017 13:36:19 GMT | ||
Server: tencent-cos | ||
X-Cos-Request-Id: NWM0MmRlZjdfMmJhZDM1MGFfNDFkM19hZGI3MQ== | ||
hello | ||
`))), nil) | ||
return resp, nil | ||
} | ||
|
||
type MockerResponseParser struct { | ||
result *cos.ObjectGetACLResult | ||
} | ||
|
||
func (p *MockerResponseParser) ParseResponse(caller cos.Caller, resp *http.Response, result interface{}) (*cos.Response, error) { | ||
b, _ := ioutil.ReadAll(resp.Body) | ||
if string(b) != "hello\n" { | ||
panic(string(b)) | ||
} | ||
|
||
switch caller.Method { | ||
case cos.MethodObjectGetACL: | ||
v := result.(*cos.ObjectGetACLResult) | ||
*v = *p.result | ||
} | ||
|
||
return &cos.Response{Response: resp}, nil | ||
} | ||
|
||
func main() { | ||
b, _ := cos.NewBaseURL("http://cos.example.com") | ||
c := cos.NewClient(b, &http.Client{ | ||
Transport: &cos.AuthorizationTransport{ | ||
SecretID: os.Getenv("COS_SECRETID"), | ||
SecretKey: os.Getenv("COS_SECRETKEY"), | ||
Transport: &debug.DebugRequestTransport{ | ||
RequestHeader: true, | ||
RequestBody: true, | ||
ResponseHeader: true, | ||
ResponseBody: true, | ||
}, | ||
}, | ||
}) | ||
c.Sender = &MockSender{} | ||
acl := &cos.ObjectGetACLResult{ | ||
Owner: &cos.Owner{ | ||
ID: "test", | ||
}, | ||
AccessControlList: []cos.ACLGrant{ | ||
{ | ||
Permission: "READ", | ||
}, | ||
}, | ||
} | ||
c.ResponseParser = &MockerResponseParser{acl} | ||
|
||
result, resp, err := c.Object.GetACL(context.Background(), "test/mock.go") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
defer resp.Body.Close() | ||
fmt.Printf("%#v\n", result) | ||
if !reflect.DeepEqual(*result, *acl) { | ||
panic(*result) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.