forked from TrueCloudLab/frostfs-http-gw
[#108] Add different expiration header formats
Signed-off-by: Denis Kirillov <denis@nspcc.ru>
This commit is contained in:
parent
06be798111
commit
cd0633cda0
5 changed files with 279 additions and 0 deletions
|
@ -1,7 +1,11 @@
|
|||
package uploader
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/nspcc-dev/neofs-api-go/v2/object"
|
||||
|
||||
"github.com/nspcc-dev/neofs-sdk-go/logger"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
@ -30,3 +34,142 @@ func TestFilter(t *testing.T) {
|
|||
|
||||
require.Equal(t, expected, result)
|
||||
}
|
||||
|
||||
func TestPrepareExpirationHeader(t *testing.T) {
|
||||
tomorrow := time.Now().Add(24 * time.Hour)
|
||||
tomorrowUnix := tomorrow.Unix()
|
||||
tomorrowUnixNano := tomorrow.UnixNano()
|
||||
tomorrowUnixMilli := tomorrowUnixNano / 1e6
|
||||
|
||||
epoch := "100"
|
||||
duration := "24h"
|
||||
timestampSec := strconv.FormatInt(tomorrowUnix, 10)
|
||||
timestampMilli := strconv.FormatInt(tomorrowUnixMilli, 10)
|
||||
timestampNano := strconv.FormatInt(tomorrowUnixNano, 10)
|
||||
|
||||
defaultDurations := &epochDurations{
|
||||
currentEpoch: 10,
|
||||
msPerBlock: 1000,
|
||||
blockPerEpoch: 101,
|
||||
}
|
||||
|
||||
epochPerDay := (24 * time.Hour).Milliseconds() / int64(defaultDurations.blockPerEpoch) / defaultDurations.msPerBlock
|
||||
defaultExpEpoch := strconv.FormatInt(int64(defaultDurations.currentEpoch)+epochPerDay, 10)
|
||||
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
headers map[string]string
|
||||
durations *epochDurations
|
||||
err bool
|
||||
expected map[string]string
|
||||
}{
|
||||
{
|
||||
name: "valid epoch",
|
||||
headers: map[string]string{object.SysAttributeExpEpoch: epoch},
|
||||
expected: map[string]string{object.SysAttributeExpEpoch: epoch},
|
||||
},
|
||||
{
|
||||
name: "valid epoch, valid duration",
|
||||
headers: map[string]string{
|
||||
object.SysAttributeExpEpoch: epoch,
|
||||
expirationDurationAttr: duration,
|
||||
},
|
||||
durations: defaultDurations,
|
||||
expected: map[string]string{object.SysAttributeExpEpoch: epoch},
|
||||
},
|
||||
{
|
||||
name: "valid epoch, valid rfc3339",
|
||||
headers: map[string]string{
|
||||
object.SysAttributeExpEpoch: epoch,
|
||||
expirationRFC3339Attr: tomorrow.Format(time.RFC3339),
|
||||
},
|
||||
durations: defaultDurations,
|
||||
expected: map[string]string{object.SysAttributeExpEpoch: epoch},
|
||||
},
|
||||
{
|
||||
name: "valid epoch, valid timestamp sec",
|
||||
headers: map[string]string{
|
||||
object.SysAttributeExpEpoch: epoch,
|
||||
expirationTimestampAttr: timestampSec,
|
||||
},
|
||||
durations: defaultDurations,
|
||||
expected: map[string]string{object.SysAttributeExpEpoch: epoch},
|
||||
},
|
||||
{
|
||||
name: "valid epoch, valid timestamp milli",
|
||||
headers: map[string]string{
|
||||
object.SysAttributeExpEpoch: epoch,
|
||||
expirationTimestampAttr: timestampMilli,
|
||||
},
|
||||
durations: defaultDurations,
|
||||
expected: map[string]string{object.SysAttributeExpEpoch: epoch},
|
||||
},
|
||||
{
|
||||
name: "valid epoch, valid timestamp nano",
|
||||
headers: map[string]string{
|
||||
object.SysAttributeExpEpoch: epoch,
|
||||
expirationTimestampAttr: timestampNano,
|
||||
},
|
||||
durations: defaultDurations,
|
||||
expected: map[string]string{object.SysAttributeExpEpoch: epoch},
|
||||
},
|
||||
{
|
||||
name: "valid timestamp sec",
|
||||
headers: map[string]string{expirationTimestampAttr: timestampSec},
|
||||
durations: defaultDurations,
|
||||
expected: map[string]string{object.SysAttributeExpEpoch: defaultExpEpoch},
|
||||
},
|
||||
{
|
||||
name: "valid duration",
|
||||
headers: map[string]string{expirationDurationAttr: duration},
|
||||
durations: defaultDurations,
|
||||
expected: map[string]string{object.SysAttributeExpEpoch: defaultExpEpoch},
|
||||
},
|
||||
{
|
||||
name: "valid rfc3339",
|
||||
headers: map[string]string{expirationRFC3339Attr: tomorrow.Format(time.RFC3339)},
|
||||
durations: defaultDurations,
|
||||
expected: map[string]string{object.SysAttributeExpEpoch: defaultExpEpoch},
|
||||
},
|
||||
{
|
||||
name: "invalid timestamp sec",
|
||||
headers: map[string]string{expirationTimestampAttr: "abc"},
|
||||
err: true,
|
||||
},
|
||||
{
|
||||
name: "invalid timestamp sec zero",
|
||||
headers: map[string]string{expirationTimestampAttr: "0"},
|
||||
err: true,
|
||||
},
|
||||
{
|
||||
name: "invalid duration",
|
||||
headers: map[string]string{expirationDurationAttr: "1d"},
|
||||
err: true,
|
||||
},
|
||||
{
|
||||
name: "invalid duration negative",
|
||||
headers: map[string]string{expirationDurationAttr: "-5h"},
|
||||
err: true,
|
||||
},
|
||||
{
|
||||
name: "invalid rfc3339",
|
||||
headers: map[string]string{expirationRFC3339Attr: "abc"},
|
||||
err: true,
|
||||
},
|
||||
{
|
||||
name: "invalid rfc3339 zero",
|
||||
headers: map[string]string{expirationRFC3339Attr: time.RFC3339},
|
||||
err: true,
|
||||
},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := prepareExpirationHeader(tc.headers, tc.durations)
|
||||
if tc.err {
|
||||
require.Error(t, err)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, tc.expected, tc.headers)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue