2016-02-28 21:25:51 +01:00
|
|
|
package swift
|
|
|
|
|
2019-03-17 12:38:25 +01:00
|
|
|
import (
|
2021-03-16 16:50:02 +01:00
|
|
|
"context"
|
2019-03-17 12:38:25 +01:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-01-22 18:23:51 +01:00
|
|
|
"github.com/ncw/swift/v2"
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs/fserrors"
|
2019-03-17 12:38:25 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
2016-02-28 21:25:51 +01:00
|
|
|
|
|
|
|
func TestInternalUrlEncode(t *testing.T) {
|
|
|
|
for _, test := range []struct {
|
|
|
|
in string
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{"", ""},
|
2022-08-14 04:56:32 +02:00
|
|
|
{"abcdefghijklmnopqrstuvwxyz", "abcdefghijklmnopqrstuvwxyz"},
|
|
|
|
{"ABCDEFGHIJKLMNOPQRSTUVWXYZ", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"},
|
2016-02-28 21:25:51 +01:00
|
|
|
{"0123456789", "0123456789"},
|
|
|
|
{"abc/ABC/123", "abc/ABC/123"},
|
|
|
|
{" ", "%20%20%20"},
|
|
|
|
{"&", "%26"},
|
|
|
|
{"ߣ", "%C3%9F%C2%A3"},
|
|
|
|
{"Vidéo Potato Sausage?&£.mkv", "Vid%C3%A9o%20Potato%20Sausage%3F%26%C2%A3.mkv"},
|
|
|
|
} {
|
|
|
|
got := urlEncode(test.in)
|
|
|
|
if got != test.want {
|
|
|
|
t.Logf("%q: want %q got %q", test.in, test.want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-17 12:38:25 +01:00
|
|
|
|
|
|
|
func TestInternalShouldRetryHeaders(t *testing.T) {
|
2021-03-16 16:50:02 +01:00
|
|
|
ctx := context.Background()
|
2019-03-17 12:38:25 +01:00
|
|
|
headers := swift.Headers{
|
|
|
|
"Content-Length": "64",
|
|
|
|
"Content-Type": "text/html; charset=UTF-8",
|
|
|
|
"Date": "Mon: 18 Mar 2019 12:11:23 GMT",
|
|
|
|
"Retry-After": "1",
|
|
|
|
}
|
|
|
|
err := &swift.Error{
|
|
|
|
StatusCode: 429,
|
|
|
|
Text: "Too Many Requests",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Short sleep should just do the sleep
|
|
|
|
start := time.Now()
|
2021-03-16 16:50:02 +01:00
|
|
|
retry, gotErr := shouldRetryHeaders(ctx, headers, err)
|
2019-03-17 12:38:25 +01:00
|
|
|
dt := time.Since(start)
|
|
|
|
assert.True(t, retry)
|
|
|
|
assert.Equal(t, err, gotErr)
|
|
|
|
assert.True(t, dt > time.Second/2)
|
|
|
|
|
|
|
|
// Long sleep should return RetryError
|
|
|
|
headers["Retry-After"] = "3600"
|
|
|
|
start = time.Now()
|
2021-03-16 16:50:02 +01:00
|
|
|
retry, gotErr = shouldRetryHeaders(ctx, headers, err)
|
2019-03-17 12:38:25 +01:00
|
|
|
dt = time.Since(start)
|
|
|
|
assert.True(t, dt < time.Second)
|
|
|
|
assert.False(t, retry)
|
|
|
|
assert.Equal(t, true, fserrors.IsRetryAfterError(gotErr))
|
|
|
|
after := gotErr.(fserrors.RetryAfter).RetryAfter()
|
|
|
|
dt = after.Sub(start)
|
|
|
|
assert.True(t, dt >= time.Hour-time.Second && dt <= time.Hour+time.Second)
|
|
|
|
|
|
|
|
}
|