59 lines
1.9 KiB
Go
59 lines
1.9 KiB
Go
package outboundimage
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
|
|
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
|
|
)
|
|
|
|
func TestAliyunOSSStoreGetReturnsBoundedImageBytesAndMIME(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet || r.URL.Path != "/private-bucket/dobotai/im-images/id" {
|
|
t.Fatalf("method=%s path=%s", r.Method, r.URL.Path)
|
|
}
|
|
w.Header().Set("Content-Type", "image/png")
|
|
w.Header().Set("Content-Length", "4")
|
|
_, _ = w.Write([]byte("PNG!"))
|
|
}))
|
|
defer server.Close()
|
|
|
|
config := oss.LoadDefaultConfig().
|
|
WithCredentialsProvider(credentials.NewStaticCredentialsProvider("test-id", "test-secret", "")).
|
|
WithRegion("cn-test").
|
|
WithEndpoint(server.URL).
|
|
WithUsePathStyle(true)
|
|
store := &AliyunOSSStore{client: oss.NewClient(config), bucket: "private-bucket"}
|
|
|
|
data, contentType, err := store.Get(context.Background(), "dobotai/im-images/id", 4)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if string(data) != "PNG!" || contentType != "image/png" {
|
|
t.Fatalf("data=%q contentType=%q", data, contentType)
|
|
}
|
|
}
|
|
|
|
func TestAliyunOSSStoreGetRejectsOversizedObjects(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
w.Header().Set("Content-Type", "image/png")
|
|
w.Header().Set("Content-Length", "5")
|
|
_, _ = w.Write([]byte("PNG!!"))
|
|
}))
|
|
defer server.Close()
|
|
|
|
config := oss.LoadDefaultConfig().
|
|
WithCredentialsProvider(credentials.NewStaticCredentialsProvider("test-id", "test-secret", "")).
|
|
WithRegion("cn-test").
|
|
WithEndpoint(server.URL).
|
|
WithUsePathStyle(true)
|
|
store := &AliyunOSSStore{client: oss.NewClient(config), bucket: "private-bucket"}
|
|
|
|
if _, _, err := store.Get(context.Background(), "dobotai/im-images/id", 4); err == nil {
|
|
t.Fatal("expected oversized object error")
|
|
}
|
|
}
|