Skip to content

Commit

Permalink
fix: unit test failed for the customHost rule update commit
Browse files Browse the repository at this point in the history
  • Loading branch information
saltbo committed Jul 8, 2021
1 parent 9d01dce commit 44ec083
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 38 deletions.
10 changes: 5 additions & 5 deletions internal/app/service/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func NewStorage() *Storage {
}

func (s *Storage) Create(storage *model.Storage) error {
conf := provider.Config{
conf := &provider.Config{
Provider: storage.Provider,
Bucket: storage.Bucket,
Endpoint: storage.Endpoint,
Expand All @@ -38,17 +38,17 @@ func (s *Storage) Create(storage *model.Storage) error {
return s.dStorage.Create(storage)
}

// fixme: 单元测试mock侵入了业务代码,有没有更好的办法?
func (s *Storage) Get(id interface{}) (*model.Storage, error) {
// fixme: 单元测试mock侵入了业务代码,有没有更好的办法?
if sid, ok := id.(int64); ok && sid == 0 {
return &model.Storage{}, nil
}

return s.dStorage.Find(id)
}

// fixme: 单元测试mock侵入了业务代码,有没有更好的办法?
func (s *Storage) GetProvider(id interface{}) (provider.Provider, error) {
// fixme: 单元测试mock侵入了业务代码,有没有更好的办法?
if sid, ok := id.(int64); ok && sid == 0 {
return &provider.MockProvider{}, nil
}
Expand All @@ -61,13 +61,13 @@ func (s *Storage) GetProvider(id interface{}) (provider.Provider, error) {
return s.GetProviderByStorage(storage)
}

// fixme: 单元测试mock侵入了业务代码,有没有更好的办法?
func (s *Storage) GetProviderByStorage(storage *model.Storage) (provider.Provider, error) {
// fixme: 单元测试mock侵入了业务代码,有没有更好的办法?
if storage.Id == 0 {
return &provider.MockProvider{}, nil
}

conf := provider.Config{
conf := &provider.Config{
Provider: storage.Provider,
Bucket: storage.Bucket,
Endpoint: storage.Endpoint,
Expand Down
16 changes: 13 additions & 3 deletions internal/pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,17 @@ type Config struct {
AccessSecret string
}

type Constructor func(provider Config) (Provider, error)
func (c *Config) Clone() *Config {
clone := *c
return &clone
}

func (c *Config) WithCustomHost(s string) *Config {
c.CustomHost = s
return c
}

type Constructor func(provider *Config) (Provider, error)

var supportProviders = map[string]Constructor{
"COS": NewCOSProvider,
Expand All @@ -46,12 +56,12 @@ var supportProviders = map[string]Constructor{
//"gd": NewGDProvider,
}

func New(conf Config) (Provider, error) {
func New(conf *Config) (Provider, error) {
if conf.Region == "" {
conf.Region = "auto"
}

if !strings.Contains(conf.CustomHost, "://") {
if conf.CustomHost != "" && !strings.Contains(conf.CustomHost, "://") {
conf.CustomHost = "http://" + conf.CustomHost
}

Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/provider/provider_cos.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type COSProvider struct {
client *cos.Client
}

func NewCOSProvider(conf Config) (Provider, error) {
func NewCOSProvider(conf *Config) (Provider, error) {
p, err := newS3Provider(conf)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/provider/provider_kodo.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type KODOProvider struct {
client *oss.Client
}

func NewKODOProvider(conf Config) (Provider, error) {
func NewKODOProvider(conf *Config) (Provider, error) {
p, err := newS3Provider(conf)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/provider/provider_minio.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type MINIOProvider struct {
client *oss.Client
}

func NewMINIOProvider(conf Config) (Provider, error) {
func NewMINIOProvider(conf *Config) (Provider, error) {
p, err := newS3Provider(conf)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/provider/provider_nos.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type NOSProvider struct {
client *nosclient.NosClient
}

func NewNOSProvider(conf Config) (Provider, error) {
func NewNOSProvider(conf *Config) (Provider, error) {
cfg := &config.Config{
Endpoint: conf.Endpoint,
AccessKey: conf.AccessKey,
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/provider/provider_obs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type OBSProvider struct {
client *obs.ObsClient
}

func NewOBSProvider(conf Config) (Provider, error) {
func NewOBSProvider(conf *Config) (Provider, error) {
client, err := obs.New(conf.AccessKey, conf.AccessSecret, conf.Endpoint)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/provider/provider_oss.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type OSSProvider struct {
client *oss.Client
}

func NewOSSProvider(conf Config) (Provider, error) {
func NewOSSProvider(conf *Config) (Provider, error) {
client, err := oss.New(conf.Endpoint, conf.AccessKey, conf.AccessSecret)
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/provider/provider_s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ type S3Provider struct {
bucket string
}

func NewS3Provider(conf Config) (Provider, error) {
func NewS3Provider(conf *Config) (Provider, error) {
return newS3Provider(conf)
}

func newS3Provider(conf Config) (*S3Provider, error) {
func newS3Provider(conf *Config) (*S3Provider, error) {
cfg := aws.NewConfig().WithCredentials(credentials.NewStaticCredentials(conf.AccessKey, conf.AccessSecret, ""))
s, err := session.NewSession(cfg)
if err != nil {
Expand Down
44 changes: 24 additions & 20 deletions internal/pkg/provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package provider
import (
"fmt"
"net/url"
"strconv"
"strings"
"testing"

Expand All @@ -11,7 +12,7 @@ import (
)

// default config
var dc = Config{
var dc = &Config{
Provider: "s3",
Bucket: "test-bucket",
Endpoint: "s3.ap-northeast-1.amazonaws.com",
Expand All @@ -21,7 +22,8 @@ var dc = Config{

var key = "1001/test.txt"

func checkSignedURLStr(t *testing.T, us, customHost string) *url.URL {
func assertSignedURL(t *testing.T, err error, us, customHost string) *url.URL {
assert.NoError(t, err)
host := fmt.Sprintf("%s.%s", dc.Bucket, dc.Endpoint)
if customHost != "" {
host = strings.TrimPrefix(customHost, "http://")
Expand All @@ -35,39 +37,41 @@ func checkSignedURLStr(t *testing.T, us, customHost string) *url.URL {
return u
}

func testSignedGetUrl(t *testing.T, conf Config) {
disk, err := New(conf)
func TestSignedPutURL(t *testing.T) {
disk, err := New(dc)
assert.NoError(t, err)

us, headers, err := disk.SignedPutURL(key, "text/plain", false)
_, headers, err := disk.SignedPutURL(key, "text/plain", false)
assert.NoError(t, err)
checkSignedURLStr(t, us, conf.CustomHost)

assert.Equal(t, s3.ObjectCannedACLAuthenticatedRead, headers.Get("x-amz-acl"))
assert.Equal(t, "text/plain", headers.Get("content-type"))
}

func TestSignedPutURL(t *testing.T) {
testSignedGetUrl(t, dc)
}

func TestSignedPutURLWithCustomHost(t *testing.T) {
conf := dc
conf.CustomHost = "http://dl.zpan.com"
testSignedGetUrl(t, conf)
}

func TestSignedGetURL(t *testing.T) {
disk, err := New(dc)
func testSignedGetURL(t *testing.T, cfg *Config) {
disk, err := New(cfg)
assert.NoError(t, err)

filename := "test2.txt"
us, err := disk.SignedGetURL(key, filename)
assert.NoError(t, err)
u := checkSignedURLStr(t, us, "")
u := assertSignedURL(t, err, us, cfg.CustomHost)
assert.Equal(t, u.Query().Get("response-content-disposition"), fmt.Sprintf(`attachment;filename="%s"`, urlEncode(filename)))
}

func TestSignedGetURL(t *testing.T) {
configs := []*Config{
dc.Clone().WithCustomHost(""),
dc.Clone().WithCustomHost("dl.zpan.com"),
dc.Clone().WithCustomHost("http://dl.zpan.com"),
}

for idx, config := range configs {
t.Run(strconv.Itoa(idx), func(t *testing.T) {
testSignedGetURL(t, config)
})
}
}

func TestPublicURL(t *testing.T) {
disk, err := New(dc)
assert.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/provider/provider_us3.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type US3Provider struct {
client *oss.Client
}

func NewUS3Provider(conf Config) (Provider, error) {
func NewUS3Provider(conf *Config) (Provider, error) {
p, err := newS3Provider(conf)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/provider/provider_uss.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type USSProvider struct {
client *upyun.UpYun
}

func NewUSSProvider(conf Config) (Provider, error) {
func NewUSSProvider(conf *Config) (Provider, error) {
return &USSProvider{
client: upyun.NewUpYun(&upyun.UpYunConfig{
Bucket: conf.Bucket,
Expand Down

0 comments on commit 44ec083

Please sign in to comment.