From c43ef040dc4d948f66570f5084d88605e5e40073 Mon Sep 17 00:00:00 2001 From: Marina Biryukova Date: Mon, 6 May 2024 11:42:08 +0300 Subject: [PATCH] [#382] Fix request type determination Signed-off-by: Marina Biryukova --- api/middleware/policy.go | 4 +- api/middleware/policy_test.go | 82 +++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 api/middleware/policy_test.go diff --git a/api/middleware/policy.go b/api/middleware/policy.go index d3ffc09..cc86635 100644 --- a/api/middleware/policy.go +++ b/api/middleware/policy.go @@ -215,11 +215,11 @@ func getBucketObject(r *http.Request, domains []string) (reqType ReqType, bktNam return noneType, "", "" } - if ind := strings.IndexByte(bktObj, '/'); ind != -1 { + if ind := strings.IndexByte(bktObj, '/'); ind != -1 && bktObj[ind+1:] != "" { return objectType, bktObj[:ind], bktObj[ind+1:] } - return bucketType, bktObj, "" + return bucketType, strings.TrimSuffix(bktObj, "/"), "" } func determineOperation(r *http.Request, reqType ReqType) (operation string) { diff --git a/api/middleware/policy_test.go b/api/middleware/policy_test.go new file mode 100644 index 0000000..3f636c7 --- /dev/null +++ b/api/middleware/policy_test.go @@ -0,0 +1,82 @@ +package middleware + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestReqTypeDetermination(t *testing.T) { + bkt, obj, domain := "test-bucket", "test-object", "domain" + + for _, tc := range []struct { + name string + target string + host string + domains []string + expectedType ReqType + expectedBktName string + expectedObjName string + }{ + { + name: "bucket request, path-style", + target: "/" + bkt, + expectedType: bucketType, + expectedBktName: bkt, + }, + { + name: "bucket request with slash, path-style", + target: "/" + bkt + "/", + expectedType: bucketType, + expectedBktName: bkt, + }, + { + name: "object request, path-style", + target: "/" + bkt + "/" + obj, + expectedType: objectType, + expectedBktName: bkt, + expectedObjName: obj, + }, + { + name: "object request with slash, path-style", + target: "/" + bkt + "/" + obj + "/", + expectedType: objectType, + expectedBktName: bkt, + expectedObjName: obj + "/", + }, + { + name: "none type request", + target: "/", + expectedType: noneType, + }, + { + name: "bucket request, virtual-hosted style", + target: "/", + host: bkt + "." + domain, + domains: []string{"some-domain", domain}, + expectedType: bucketType, + expectedBktName: bkt, + }, + { + name: "object request, virtual-hosted style", + target: "/" + obj, + host: bkt + "." + domain, + domains: []string{"some-domain", domain}, + expectedType: objectType, + expectedBktName: bkt, + expectedObjName: obj, + }, + } { + t.Run(tc.name, func(t *testing.T) { + r := httptest.NewRequest(http.MethodPut, tc.target, nil) + r.Host = tc.host + + reqType, bktName, objName := getBucketObject(r, tc.domains) + require.Equal(t, tc.expectedType, reqType) + require.Equal(t, tc.expectedBktName, bktName) + require.Equal(t, tc.expectedObjName, objName) + }) + } +}