github.com/cloudreve/Cloudreve/v3@v3.0.0-20240224133659-3edb00a6484c/pkg/filesystem/driver/remote/client_test.go (about) 1 package remote 2 3 import ( 4 "context" 5 "errors" 6 model "github.com/cloudreve/Cloudreve/v3/models" 7 "github.com/cloudreve/Cloudreve/v3/pkg/cache" 8 "github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx" 9 "github.com/cloudreve/Cloudreve/v3/pkg/mocks/requestmock" 10 "github.com/cloudreve/Cloudreve/v3/pkg/request" 11 "github.com/stretchr/testify/assert" 12 testMock "github.com/stretchr/testify/mock" 13 "io/ioutil" 14 "net/http" 15 "strings" 16 "testing" 17 ) 18 19 func TestNewClient(t *testing.T) { 20 a := assert.New(t) 21 policy := &model.Policy{} 22 23 // 无法解析服务端url 24 { 25 policy.Server = string([]byte{0x7f}) 26 c, err := NewClient(policy) 27 a.Error(err) 28 a.Nil(c) 29 } 30 31 // 成功 32 { 33 policy.Server = "" 34 c, err := NewClient(policy) 35 a.NoError(err) 36 a.NotNil(c) 37 } 38 } 39 40 func TestRemoteClient_Upload(t *testing.T) { 41 a := assert.New(t) 42 c, _ := NewClient(&model.Policy{}) 43 44 // 无法创建上传会话 45 { 46 clientMock := requestmock.RequestMock{} 47 c.(*remoteClient).httpClient = &clientMock 48 clientMock.On( 49 "Request", 50 "PUT", 51 "upload", 52 testMock.Anything, 53 testMock.Anything, 54 ).Return(&request.Response{ 55 Err: errors.New("error"), 56 }) 57 err := c.Upload(context.Background(), &fsctx.FileStream{}) 58 a.Error(err) 59 a.Contains(err.Error(), "error") 60 clientMock.AssertExpectations(t) 61 } 62 63 // 分片上传失败,成功删除上传会话 64 { 65 cache.Set("setting_chunk_retries", "1", 0) 66 clientMock := requestmock.RequestMock{} 67 c.(*remoteClient).httpClient = &clientMock 68 clientMock.On( 69 "Request", 70 "PUT", 71 "upload", 72 testMock.Anything, 73 testMock.Anything, 74 ).Return(&request.Response{ 75 Err: nil, 76 Response: &http.Response{ 77 StatusCode: 200, 78 Body: ioutil.NopCloser(strings.NewReader(`{"code":0}`)), 79 }, 80 }) 81 clientMock.On( 82 "Request", 83 "POST", 84 testMock.Anything, 85 testMock.Anything, 86 testMock.Anything, 87 ).Return(&request.Response{ 88 Err: errors.New("error"), 89 }) 90 clientMock.On( 91 "Request", 92 "DELETE", 93 testMock.Anything, 94 testMock.Anything, 95 testMock.Anything, 96 ).Return(&request.Response{ 97 Err: nil, 98 Response: &http.Response{ 99 StatusCode: 200, 100 Body: ioutil.NopCloser(strings.NewReader(`{"code":0}`)), 101 }, 102 }) 103 err := c.Upload(context.Background(), &fsctx.FileStream{}) 104 a.Error(err) 105 a.Contains(err.Error(), "error") 106 clientMock.AssertExpectations(t) 107 } 108 109 // 分片上传失败,无法删除上传会话 110 { 111 cache.Set("setting_chunk_retries", "1", 0) 112 clientMock := requestmock.RequestMock{} 113 c.(*remoteClient).httpClient = &clientMock 114 clientMock.On( 115 "Request", 116 "PUT", 117 "upload", 118 testMock.Anything, 119 testMock.Anything, 120 ).Return(&request.Response{ 121 Err: nil, 122 Response: &http.Response{ 123 StatusCode: 200, 124 Body: ioutil.NopCloser(strings.NewReader(`{"code":0}`)), 125 }, 126 }) 127 clientMock.On( 128 "Request", 129 "POST", 130 testMock.Anything, 131 testMock.Anything, 132 testMock.Anything, 133 ).Return(&request.Response{ 134 Err: errors.New("error"), 135 }) 136 clientMock.On( 137 "Request", 138 "DELETE", 139 testMock.Anything, 140 testMock.Anything, 141 testMock.Anything, 142 ).Return(&request.Response{ 143 Err: errors.New("error2"), 144 Response: &http.Response{ 145 StatusCode: 200, 146 Body: ioutil.NopCloser(strings.NewReader(`{"code":0}`)), 147 }, 148 }) 149 err := c.Upload(context.Background(), &fsctx.FileStream{}) 150 a.Error(err) 151 a.Contains(err.Error(), "error") 152 clientMock.AssertExpectations(t) 153 } 154 155 // 成功 156 { 157 cache.Set("setting_chunk_retries", "1", 0) 158 clientMock := requestmock.RequestMock{} 159 c.(*remoteClient).httpClient = &clientMock 160 clientMock.On( 161 "Request", 162 "PUT", 163 "upload", 164 testMock.Anything, 165 testMock.Anything, 166 ).Return(&request.Response{ 167 Err: nil, 168 Response: &http.Response{ 169 StatusCode: 200, 170 Body: ioutil.NopCloser(strings.NewReader(`{"code":0}`)), 171 }, 172 }) 173 clientMock.On( 174 "Request", 175 "POST", 176 testMock.Anything, 177 testMock.Anything, 178 testMock.Anything, 179 ).Return(&request.Response{ 180 Response: &http.Response{ 181 StatusCode: 200, 182 Body: ioutil.NopCloser(strings.NewReader(`{"code":0}`)), 183 }, 184 }) 185 err := c.Upload(context.Background(), &fsctx.FileStream{}) 186 a.NoError(err) 187 clientMock.AssertExpectations(t) 188 } 189 } 190 191 func TestRemoteClient_CreateUploadSessionFailed(t *testing.T) { 192 a := assert.New(t) 193 c, _ := NewClient(&model.Policy{}) 194 195 clientMock := requestmock.RequestMock{} 196 c.(*remoteClient).httpClient = &clientMock 197 clientMock.On( 198 "Request", 199 "PUT", 200 "upload", 201 testMock.Anything, 202 testMock.Anything, 203 ).Return(&request.Response{ 204 Err: nil, 205 Response: &http.Response{ 206 StatusCode: 200, 207 Body: ioutil.NopCloser(strings.NewReader(`{"code":500,"msg":"error"}`)), 208 }, 209 }) 210 err := c.Upload(context.Background(), &fsctx.FileStream{}) 211 a.Error(err) 212 a.Contains(err.Error(), "error") 213 clientMock.AssertExpectations(t) 214 } 215 216 func TestRemoteClient_UploadChunkFailed(t *testing.T) { 217 a := assert.New(t) 218 c, _ := NewClient(&model.Policy{}) 219 220 clientMock := requestmock.RequestMock{} 221 c.(*remoteClient).httpClient = &clientMock 222 clientMock.On( 223 "Request", 224 "POST", 225 testMock.Anything, 226 testMock.Anything, 227 testMock.Anything, 228 ).Return(&request.Response{ 229 Err: nil, 230 Response: &http.Response{ 231 StatusCode: 200, 232 Body: ioutil.NopCloser(strings.NewReader(`{"code":500,"msg":"error"}`)), 233 }, 234 }) 235 err := c.(*remoteClient).uploadChunk(context.Background(), "", 0, strings.NewReader(""), false, 0) 236 a.Error(err) 237 a.Contains(err.Error(), "error") 238 clientMock.AssertExpectations(t) 239 } 240 241 func TestRemoteClient_GetUploadURL(t *testing.T) { 242 a := assert.New(t) 243 c, _ := NewClient(&model.Policy{}) 244 245 // url 解析失败 246 { 247 c.(*remoteClient).policy.Server = string([]byte{0x7f}) 248 res, sign, err := c.GetUploadURL(0, "") 249 a.Error(err) 250 a.Empty(res) 251 a.Empty(sign) 252 } 253 254 // 成功 255 { 256 c.(*remoteClient).policy.Server = "" 257 res, sign, err := c.GetUploadURL(0, "") 258 a.NoError(err) 259 a.NotEmpty(res) 260 a.NotEmpty(sign) 261 } 262 }