Marina Biryukova
c43ef040dc
All checks were successful
/ DCO (pull_request) Successful in 1m36s
/ Builds (1.20) (pull_request) Successful in 2m15s
/ Builds (1.21) (pull_request) Successful in 2m9s
/ Lint (pull_request) Successful in 3m22s
/ Tests (1.20) (pull_request) Successful in 2m18s
/ Tests (1.21) (pull_request) Successful in 2m6s
/ Vulncheck (pull_request) Successful in 57s
Signed-off-by: Marina Biryukova <m.biryukova@yadro.com>
82 lines
2 KiB
Go
82 lines
2 KiB
Go
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)
|
|
})
|
|
}
|
|
}
|