From 80c4982bd43cb1f000a7238eb70e334920fcbbff Mon Sep 17 00:00:00 2001 From: Denis Kirillov Date: Wed, 19 Jul 2023 17:18:55 +0300 Subject: [PATCH] [#174] Add router tests Signed-off-by: Denis Kirillov --- api/router_mock_test.go | 377 ++++++++++++++++++++++++++++++++++++++++ api/router_test.go | 75 ++++++++ 2 files changed, 452 insertions(+) create mode 100644 api/router_mock_test.go create mode 100644 api/router_test.go diff --git a/api/router_mock_test.go b/api/router_mock_test.go new file mode 100644 index 00000000..3c84921d --- /dev/null +++ b/api/router_mock_test.go @@ -0,0 +1,377 @@ +package api + +import ( + "context" + "encoding/json" + "net/http" + "testing" + + "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/auth" + "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/data" + "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/middleware" + "github.com/stretchr/testify/require" +) + +type centerMock struct { +} + +func (c *centerMock) Authenticate(*http.Request) (*auth.Box, error) { + return &auth.Box{}, nil +} + +type handlerMock struct { + t *testing.T +} + +type handlerResult struct { + Method string + ReqInfo *middleware.ReqInfo +} + +func (h *handlerMock) HeadObjectHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) GetObjectACLHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) PutObjectACLHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) GetObjectTaggingHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) PutObjectTaggingHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) DeleteObjectTaggingHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) SelectObjectContentHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) GetObjectRetentionHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) GetObjectLegalHoldHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) GetObjectHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) GetObjectAttributesHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) CopyObjectHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) PutObjectRetentionHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) PutObjectLegalHoldHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) PutObjectHandler(w http.ResponseWriter, r *http.Request) { + res := &handlerResult{ + Method: "PutObject", + ReqInfo: middleware.GetReqInfo(r.Context()), + } + + h.writeResponse(w, res) +} + +func (h *handlerMock) DeleteObjectHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) GetBucketLocationHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) GetBucketPolicyHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) GetBucketLifecycleHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) GetBucketEncryptionHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) GetBucketACLHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) PutBucketACLHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) GetBucketCorsHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) PutBucketCorsHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) DeleteBucketCorsHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) GetBucketWebsiteHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) GetBucketAccelerateHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) GetBucketRequestPaymentHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) GetBucketLoggingHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) GetBucketReplicationHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) GetBucketTaggingHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) DeleteBucketWebsiteHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) DeleteBucketTaggingHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) GetBucketObjectLockConfigHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) GetBucketVersioningHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) GetBucketNotificationHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) ListenBucketNotificationHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) ListObjectsV2MHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) ListObjectsV2Handler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) ListBucketObjectVersionsHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) ListObjectsV1Handler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) PutBucketLifecycleHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) PutBucketEncryptionHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) PutBucketPolicyHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) PutBucketObjectLockConfigHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) PutBucketTaggingHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) PutBucketVersioningHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) PutBucketNotificationHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) CreateBucketHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) HeadBucketHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) PostObject(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) DeleteMultipleObjectsHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) DeleteBucketPolicyHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) DeleteBucketLifecycleHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) DeleteBucketEncryptionHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) DeleteBucketHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) ListBucketsHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) Preflight(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) AppendCORSHeaders(http.ResponseWriter, *http.Request) { +} + +func (h *handlerMock) CreateMultipartUploadHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) UploadPartHandler(w http.ResponseWriter, r *http.Request) { + res := &handlerResult{ + Method: "UploadPart", + ReqInfo: middleware.GetReqInfo(r.Context()), + } + + h.writeResponse(w, res) +} + +func (h *handlerMock) UploadPartCopy(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) CompleteMultipartUploadHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) AbortMultipartUploadHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) ListPartsHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) ListMultipartUploadsHandler(http.ResponseWriter, *http.Request) { + //TODO implement me + panic("implement me") +} + +func (h *handlerMock) ResolveBucket(context.Context, string) (*data.BucketInfo, error) { + return &data.BucketInfo{}, nil +} + +func (h *handlerMock) writeResponse(w http.ResponseWriter, resp *handlerResult) { + respData, err := json.Marshal(resp) + require.NoError(h.t, err) + + _, err = w.Write(respData) + require.NoError(h.t, err) +} diff --git a/api/router_test.go b/api/router_test.go new file mode 100644 index 00000000..e63007e0 --- /dev/null +++ b/api/router_test.go @@ -0,0 +1,75 @@ +package api + +import ( + "encoding/json" + "fmt" + "io" + "net/http" + "net/http/httptest" + "net/url" + "testing" + "time" + + "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/metrics" + "github.com/go-chi/chi/v5" + "github.com/go-chi/chi/v5/middleware" + "github.com/stretchr/testify/require" + "go.uber.org/zap/zaptest" +) + +func TestRouterUploadPart(t *testing.T) { + chiRouter := prepareRouter(t) + + w := httptest.NewRecorder() + r := httptest.NewRequest(http.MethodPut, "/dkirillov/fix-object", nil) + query := make(url.Values) + query.Set("uploadId", "some-id") + query.Set("partNumber", "1") + r.URL.RawQuery = query.Encode() + + chiRouter.ServeHTTP(w, r) + resp := readResponse(t, w) + require.Equal(t, "UploadPart", resp.Method) +} + +func TestRouterObjectWithSlashes(t *testing.T) { + chiRouter := prepareRouter(t) + + bktName, objName := "dkirillov", "/fix/object" + target := fmt.Sprintf("/%s/%s", bktName, objName) + + w := httptest.NewRecorder() + r := httptest.NewRequest(http.MethodPut, target, nil) + + chiRouter.ServeHTTP(w, r) + resp := readResponse(t, w) + require.Equal(t, "PutObject", resp.Method) + require.Equal(t, objName, resp.ReqInfo.ObjectName) +} + +func prepareRouter(t *testing.T) *chi.Mux { + throttleOps := middleware.ThrottleOpts{ + Limit: 10, + BacklogTimeout: 30 * time.Second, + } + + handleMock := &handlerMock{t: t} + cntrMock := ¢erMock{} + log := zaptest.NewLogger(t) + metric := &metrics.AppMetrics{} + + chiRouter := chi.NewRouter() + AttachChi(chiRouter, nil, throttleOps, handleMock, cntrMock, log, metric) + return chiRouter +} + +func readResponse(t *testing.T, w *httptest.ResponseRecorder) handlerResult { + var res handlerResult + + resData, err := io.ReadAll(w.Result().Body) + require.NoError(t, err) + + err = json.Unmarshal(resData, &res) + require.NoErrorf(t, err, "actual body: '%s'", string(resData)) + return res +}