[#484] Add tests

Signed-off-by: Denis Kirillov <denis@nspcc.ru>
This commit is contained in:
Denis Kirillov 2022-06-01 17:50:30 +03:00 committed by Alex Vanin
parent f282e877e2
commit ea252421f5
3 changed files with 106 additions and 1 deletions

View file

@ -4,10 +4,13 @@ import (
"bytes" "bytes"
"context" "context"
"encoding/xml" "encoding/xml"
"io"
"math/rand" "math/rand"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"strconv"
"testing" "testing"
"time"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neofs-s3-gw/api" "github.com/nspcc-dev/neofs-s3-gw/api"
@ -17,6 +20,7 @@ import (
"github.com/nspcc-dev/neofs-s3-gw/api/resolver" "github.com/nspcc-dev/neofs-s3-gw/api/resolver"
"github.com/nspcc-dev/neofs-s3-gw/internal/neofstest" "github.com/nspcc-dev/neofs-s3-gw/internal/neofstest"
cid "github.com/nspcc-dev/neofs-sdk-go/container/id" cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
"github.com/nspcc-dev/neofs-sdk-go/object"
"github.com/nspcc-dev/neofs-sdk-go/user" "github.com/nspcc-dev/neofs-sdk-go/user"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"go.uber.org/zap" "go.uber.org/zap"
@ -111,12 +115,16 @@ func createTestObject(ctx context.Context, t *testing.T, h *handlerContext, bktI
_, err := rand.Read(content) _, err := rand.Read(content)
require.NoError(t, err) require.NoError(t, err)
header := map[string]string{
object.AttributeTimestamp: strconv.FormatInt(time.Now().UTC().Unix(), 10),
}
_, err = h.Layer().PutObject(ctx, &layer.PutObjectParams{ _, err = h.Layer().PutObject(ctx, &layer.PutObjectParams{
BktInfo: bktInfo, BktInfo: bktInfo,
Object: objName, Object: objName,
Size: int64(len(content)), Size: int64(len(content)),
Reader: bytes.NewReader(content), Reader: bytes.NewReader(content),
Header: make(map[string]string), Header: header,
}) })
require.NoError(t, err) require.NoError(t, err)
} }
@ -133,3 +141,11 @@ func prepareTestRequest(t *testing.T, bktName, objName string, body interface{})
return w, r return w, r
} }
func assertStatus(t *testing.T, w *httptest.ResponseRecorder, status int) {
if w.Code != status {
resp, err := io.ReadAll(w.Result().Body)
require.NoError(t, err)
require.Failf(t, string(resp), "assert status fail, expected: %d, actual: %d", status, w.Code)
}
}

85
api/handler/head_test.go Normal file
View file

@ -0,0 +1,85 @@
package handler
import (
"context"
"net/http"
"testing"
"time"
"github.com/nspcc-dev/neofs-s3-gw/api"
"github.com/stretchr/testify/require"
)
func TestConditionalHead(t *testing.T) {
ctx := context.Background()
tc := prepareHandlerContext(t)
bktName := "bucket-for-conditional"
createTestBucket(ctx, t, tc, bktName)
bktInfo, err := tc.Layer().GetBucketInfo(ctx, bktName)
require.NoError(t, err)
objName := "object"
createTestObject(ctx, t, tc, bktInfo, objName)
w, r := prepareTestRequest(t, bktName, objName, nil)
tc.Handler().HeadObjectHandler(w, r)
assertStatus(t, w, http.StatusOK)
etag := w.Result().Header.Get(api.ETag)
lastModified := w.Result().Header.Get(api.LastModified)
_ = lastModified
w, r = prepareTestRequest(t, bktName, objName, nil)
r.Header.Set(api.IfMatch, etag)
tc.Handler().HeadObjectHandler(w, r)
assertStatus(t, w, http.StatusOK)
w, r = prepareTestRequest(t, bktName, objName, nil)
r.Header.Set(api.IfMatch, "etag")
tc.Handler().HeadObjectHandler(w, r)
assertStatus(t, w, http.StatusPreconditionFailed)
w, r = prepareTestRequest(t, bktName, objName, nil)
r.Header.Set(api.IfUnmodifiedSince, time.Now().UTC().Format(http.TimeFormat))
tc.Handler().HeadObjectHandler(w, r)
assertStatus(t, w, http.StatusOK)
var zeroTime time.Time
w, r = prepareTestRequest(t, bktName, objName, nil)
r.Header.Set(api.IfUnmodifiedSince, zeroTime.UTC().Format(http.TimeFormat))
tc.Handler().HeadObjectHandler(w, r)
assertStatus(t, w, http.StatusPreconditionFailed)
w, r = prepareTestRequest(t, bktName, objName, nil)
r.Header.Set(api.IfMatch, etag)
r.Header.Set(api.IfUnmodifiedSince, zeroTime.UTC().Format(http.TimeFormat))
tc.Handler().HeadObjectHandler(w, r)
assertStatus(t, w, http.StatusOK)
w, r = prepareTestRequest(t, bktName, objName, nil)
r.Header.Set(api.IfNoneMatch, etag)
tc.Handler().HeadObjectHandler(w, r)
assertStatus(t, w, http.StatusNotModified)
w, r = prepareTestRequest(t, bktName, objName, nil)
r.Header.Set(api.IfNoneMatch, "etag")
tc.Handler().HeadObjectHandler(w, r)
assertStatus(t, w, http.StatusOK)
w, r = prepareTestRequest(t, bktName, objName, nil)
r.Header.Set(api.IfModifiedSince, zeroTime.UTC().Format(http.TimeFormat))
tc.Handler().HeadObjectHandler(w, r)
assertStatus(t, w, http.StatusOK)
w, r = prepareTestRequest(t, bktName, objName, nil)
r.Header.Set(api.IfModifiedSince, time.Now().Add(time.Minute).UTC().Format(http.TimeFormat))
tc.Handler().HeadObjectHandler(w, r)
assertStatus(t, w, http.StatusNotModified)
w, r = prepareTestRequest(t, bktName, objName, nil)
r.Header.Set(api.IfNoneMatch, etag)
r.Header.Set(api.IfModifiedSince, zeroTime.UTC().Format(http.TimeFormat))
tc.Handler().HeadObjectHandler(w, r)
assertStatus(t, w, http.StatusNotModified)
}

View file

@ -13,6 +13,7 @@ import (
objectv2 "github.com/nspcc-dev/neofs-api-go/v2/object" objectv2 "github.com/nspcc-dev/neofs-api-go/v2/object"
"github.com/nspcc-dev/neofs-s3-gw/api/layer/neofs" "github.com/nspcc-dev/neofs-s3-gw/api/layer/neofs"
"github.com/nspcc-dev/neofs-sdk-go/checksum"
"github.com/nspcc-dev/neofs-sdk-go/container" "github.com/nspcc-dev/neofs-sdk-go/container"
cid "github.com/nspcc-dev/neofs-sdk-go/container/id" cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
"github.com/nspcc-dev/neofs-sdk-go/object" "github.com/nspcc-dev/neofs-sdk-go/object"
@ -229,6 +230,9 @@ func (t *TestNeoFS) CreateObject(_ context.Context, prm neofs.PrmObjectCreate) (
} }
obj.SetPayload(all) obj.SetPayload(all)
obj.SetPayloadSize(uint64(len(all))) obj.SetPayloadSize(uint64(len(all)))
var hash checksum.Checksum
checksum.Calculate(&hash, checksum.SHA256, all)
obj.SetPayloadChecksum(hash)
} }
cnrID, _ := obj.ContainerID() cnrID, _ := obj.ContainerID()