mirror of
https://github.com/versity/versitygw.git
synced 2026-05-23 20:31:27 +00:00
Fixes #2123 Fixes #2120 Fixes #2116 Fixes #2111 Fixes #2108 Fixes #2086 Fixes #2085 Fixes #2083 Fixes #2081 Fixes #2080 Fixes #2073 Fixes #2072 Fixes #2071 Fixes #2069 Fixes #2044 Fixes #2043 Fixes #2042 Fixes #2041 Fixes #2040 Fixes #2039 Fixes #2036 Fixes #2035 Fixes #2034 Fixes #2028 Fixes #2020 Fixes #1842 Fixes #1810 Fixes #1780 Fixes #1775 Fixes #1736 Fixes #1705 Fixes #1663 Fixes #1645 Fixes #1583 Fixes #1526 Fixes #1514 Fixes #1493 Fixes #1487 Fixes #959 Fixes #779 Closes #823 Closes #85 Refactor global S3 error handling around structured error types and centralized XML response generation. All S3 errors now share the common APIError base for the fields every error has: Code, HTTP status code, and Message. Non-traditional errors that need AWS-compatible XML fields now have dedicated typed errors in the s3err package. Each typed error implements the shared S3Error behavior so controllers and middleware can handle errors consistently while still emitting error-specific XML fields. Add a dedicated InvalidArgumentError type because InvalidArgument is used widely across request validation, auth, copy source handling, object lock validation, multipart validation, and header parsing. The new InvalidArgument path uses explicit InvalidArgErrorCode constants with predefined descriptions and ArgumentName values, keeping call sites readable while preserving the correct InvalidArgument XML shape and optional ArgumentValue. New structured errors added in s3err: - `AccessForbiddenError`: Method, ResourceType - `BadDigestError`: CalculatedDigest, ExpectedDigest - `BucketError`: BucketName - `ContentSHA256MismatchError`: ClientComputedContentSHA256, S3ComputedContentSHA256 - `EntityTooLargeError`: ProposedSize, MaxSizeAllowed - `EntityTooSmallError`: ProposedSize, MinSizeAllowed - `ExpiredPresignedURLError`: ServerTime, XAmzExpires, Expires - `InvalidAccessKeyIdError`: AWSAccessKeyId - `InvalidArgumentError`: Description, ArgumentName, ArgumentValue - `InvalidChunkSizeError`: Chunk, BadChunkSize - `InvalidDigestError`: ContentMD5 - `InvalidLocationConstraintError`: LocationConstraint - `InvalidPartError`: UploadId, PartNumber, ETag - `InvalidRangeError`: RangeRequested, ActualObjectSize - `InvalidTagError`: TagKey, TagValue - `KeyTooLongError`: Size, MaxSizeAllowed - `MetadataTooLargeError`: Size, MaxSizeAllowed - `MethodNotAllowedError`: Method, ResourceType, AllowedMethods - `NoSuchUploadError`: UploadId - `NoSuchVersionError`: Key, VersionId - `NotImplementedError`: Header, AdditionalMessage - `PreconditionFailedError`: Condition - `RequestTimeTooSkewedError`: RequestTime, ServerTime, MaxAllowedSkewMilliseconds - `SignatureDoesNotMatchError`: AWSAccessKeyId, StringToSign, SignatureProvided, StringToSignBytes, CanonicalRequest, CanonicalRequestBytes Fix CompleteMultipartUpload validation in the Azure backend so missing or empty `ETag` values return the appropriate S3 error instead of allowing a gateway panic. Fix presigned authentication expiration validation to compare server time in `UTC`, matching the `UTC` timestamp used by presigned URL signing. Add request ID and host ID support across S3 requests. Each request now receives AWS S3-like identifiers, returned in response headers as `x-amz-request-id` and `x-amz-id-2` and included in all XML error responses as RequestId and HostId. The generated ID structure is designed to resemble AWS S3 request IDs and host IDs. The request signature calculation/validation for streaming uploads was previously delayed until the request body was fully read, both for Authorization header authentication and presigned URLs. Now, the signature is validated immediately in the authorization middlewares without reading the request body, since the signature calculation itself does not depend on the request body. Instead, only the `x-amz-content-sha256` SHA-256 hash calculation is delayed.
714 lines
17 KiB
Go
714 lines
17 KiB
Go
// Copyright 2023 Versity Software
|
|
// This file is licensed under the Apache License, Version 2.0
|
|
// (the "License"); you may not use this file except in compliance
|
|
// with the License. You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing,
|
|
// software distributed under the License is distributed on an
|
|
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
// KIND, either express or implied. See the License for the
|
|
// specific language governing permissions and limitations
|
|
// under the License.
|
|
|
|
package controllers
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/xml"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"path"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/valyala/fasthttp"
|
|
"github.com/versity/versitygw/auth"
|
|
"github.com/versity/versitygw/metrics"
|
|
"github.com/versity/versitygw/s3api/utils"
|
|
"github.com/versity/versitygw/s3err"
|
|
"github.com/versity/versitygw/s3event"
|
|
"github.com/versity/versitygw/s3log"
|
|
"github.com/versity/versitygw/s3response"
|
|
)
|
|
|
|
var (
|
|
testRequestID = "5MRQJ97RHWJ4FMX9"
|
|
testHostID = "eS8nILxNKeV1pNi2Z7Pv6mwC+nuquA2UTBwrBSxGq62e9NZ6f2G9aJPRetuD0/lF3OgqRF7N3GU="
|
|
|
|
defaultLocals map[utils.ContextKey]any = map[utils.ContextKey]any{
|
|
utils.ContextKeyIsRoot: true,
|
|
utils.ContextKeyParsedAcl: auth.ACL{
|
|
Owner: "root",
|
|
},
|
|
utils.ContextKeyAccount: auth.Account{
|
|
Access: "root",
|
|
Role: auth.RoleAdmin,
|
|
},
|
|
utils.ContextKeyRegion: "us-east-1",
|
|
}
|
|
|
|
accessDeniedLocals map[utils.ContextKey]any = map[utils.ContextKey]any{
|
|
utils.ContextKeyIsRoot: false,
|
|
utils.ContextKeyParsedAcl: auth.ACL{
|
|
Owner: "root",
|
|
},
|
|
utils.ContextKeyAccount: auth.Account{
|
|
Access: "user",
|
|
Role: auth.RoleUser,
|
|
},
|
|
}
|
|
)
|
|
|
|
type testInput struct {
|
|
bucket string
|
|
body []byte
|
|
locals map[utils.ContextKey]any
|
|
headers map[string]string
|
|
queries map[string]string
|
|
beRes any
|
|
beErr error
|
|
extraMockErr error
|
|
extraMockResp any
|
|
}
|
|
|
|
type testOutput struct {
|
|
response *Response
|
|
err error
|
|
}
|
|
|
|
type ctxInputs struct {
|
|
bucket string
|
|
object string
|
|
body []byte
|
|
locals map[utils.ContextKey]any
|
|
headers map[string]string
|
|
queries map[string]string
|
|
}
|
|
|
|
func testController(t *testing.T, ctrl Controller, resp *Response, expectedErr error, input ctxInputs) {
|
|
app := fiber.New()
|
|
|
|
app.Post("/:bucket/*", func(ctx *fiber.Ctx) error {
|
|
// set the request body
|
|
ctx.Request().SetBody(input.body)
|
|
// set the request locals
|
|
if input.locals != nil {
|
|
for key, local := range input.locals {
|
|
key.Set(ctx, local)
|
|
}
|
|
}
|
|
|
|
// call the controller by passing the ctx
|
|
res, err := ctrl(ctx)
|
|
assert.Equal(t, resp, res)
|
|
if expectedErr != nil {
|
|
assert.Error(t, err)
|
|
|
|
switch expectedErr.(type) {
|
|
case s3err.S3Error:
|
|
assert.EqualValues(t, expectedErr, err)
|
|
default:
|
|
assert.ErrorContains(t, err, expectedErr.Error())
|
|
}
|
|
} else {
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
req := buildRequest(input.bucket, input.object, input.body, input.headers, input.queries)
|
|
|
|
_, err := app.Test(req)
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
func buildRequest(bucket, object string, body []byte, headers, queries map[string]string) *http.Request {
|
|
if bucket == "" {
|
|
bucket = "bucket"
|
|
}
|
|
if object == "" {
|
|
object = "object"
|
|
}
|
|
uri := url.URL{
|
|
Path: "/" + path.Join(bucket, object),
|
|
}
|
|
|
|
// set the request query params
|
|
if queries != nil {
|
|
q := uri.Query()
|
|
for key, val := range queries {
|
|
q.Set(key, val)
|
|
}
|
|
|
|
uri.RawQuery = q.Encode()
|
|
}
|
|
|
|
// create a new request
|
|
req := httptest.NewRequest(http.MethodPost, uri.String(), bytes.NewReader(body))
|
|
|
|
// set the request headers
|
|
for key, val := range headers {
|
|
req.Header.Set(key, val)
|
|
}
|
|
|
|
return req
|
|
}
|
|
|
|
func TestS3ApiController_HandleErrorRoute(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input testInput
|
|
output testOutput
|
|
}{
|
|
{
|
|
name: "should return the passed error",
|
|
input: testInput{
|
|
extraMockErr: s3err.GetAPIError(s3err.ErrAnonymousCreateMp),
|
|
},
|
|
output: testOutput{
|
|
response: &Response{},
|
|
err: s3err.GetAPIError(s3err.ErrAnonymousCreateMp),
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
s3Ctrl := S3ApiController{}
|
|
ctrl := s3Ctrl.HandleErrorRoute(tt.input.extraMockErr)
|
|
testController(
|
|
t,
|
|
ctrl,
|
|
tt.output.response,
|
|
tt.output.err,
|
|
ctxInputs{})
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSetResponseHeaders(t *testing.T) {
|
|
type args struct {
|
|
headers map[string]*string
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
expected map[string]string
|
|
}{
|
|
{
|
|
name: "should not set if map is nil",
|
|
args: args{
|
|
headers: nil,
|
|
},
|
|
expected: nil,
|
|
},
|
|
{
|
|
name: "should set some headers",
|
|
args: args{
|
|
headers: map[string]*string{
|
|
"x-amz-checksum-algorithm": utils.GetStringPtr("crc32"),
|
|
"x-amz-meta-key": utils.GetStringPtr("meta_key"),
|
|
"x-amz-mp-size": utils.GetStringPtr(""),
|
|
"something": nil,
|
|
},
|
|
},
|
|
expected: map[string]string{
|
|
"x-amz-checksum-algorithm": "crc32",
|
|
"x-amz-meta-key": "meta_key",
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
app := fiber.New()
|
|
ctx := app.AcquireCtx(&fasthttp.RequestCtx{})
|
|
SetResponseHeaders(ctx, tt.args.headers)
|
|
if tt.expected != nil {
|
|
for key, val := range tt.expected {
|
|
v := ctx.Response().Header.Peek(key)
|
|
assert.Equal(t, val, string(v))
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestEnsureExposeMetaHeaders_AddsActualMetaHeaderNames(t *testing.T) {
|
|
app := fiber.New()
|
|
ctx := app.AcquireCtx(&fasthttp.RequestCtx{})
|
|
|
|
ctx.Response().Header.Add("Access-Control-Allow-Origin", "https://example.com")
|
|
ctx.Response().Header.Add("Access-Control-Expose-Headers", "ETag")
|
|
ctx.Response().Header.Set("x-amz-meta-foo", "bar")
|
|
ctx.Response().Header.Set("x-amz-meta-bar", "baz")
|
|
|
|
ensureExposeMetaHeaders(ctx)
|
|
|
|
got := string(ctx.Response().Header.Peek("Access-Control-Expose-Headers"))
|
|
assert.Equal(t, "ETag, X-Amz-Meta-Bar, X-Amz-Meta-Foo", got)
|
|
}
|
|
|
|
// mock the audit logger
|
|
type mockAuditLogger struct {
|
|
}
|
|
|
|
func (m *mockAuditLogger) Log(_ *fiber.Ctx, _ error, _ []byte, _ s3log.LogMeta) {}
|
|
func (m *mockAuditLogger) HangUp() error { return nil }
|
|
func (m *mockAuditLogger) Shutdown() error { return nil }
|
|
|
|
// mock S3 event sender
|
|
type mockEvSender struct {
|
|
}
|
|
|
|
func (m *mockEvSender) SendEvent(_ *fiber.Ctx, _ s3event.EventMeta) {}
|
|
func (m *mockEvSender) Close() error { return nil }
|
|
|
|
// mock metrics manager
|
|
|
|
type mockMetricsManager struct{}
|
|
|
|
func (m *mockMetricsManager) Send(_ *fiber.Ctx, _ error, _ string, _ int64, _ int) {}
|
|
func (m *mockMetricsManager) Close() {}
|
|
|
|
func TestProcessController(t *testing.T) {
|
|
payload, err := xml.Marshal(s3response.Bucket{
|
|
Name: "something",
|
|
})
|
|
assert.NoError(t, err)
|
|
|
|
payloadLen := len(payload) + len(xmlhdr)
|
|
|
|
services := &Services{
|
|
Logger: &mockAuditLogger{},
|
|
EventSender: &mockEvSender{},
|
|
MetricsManager: &mockMetricsManager{},
|
|
}
|
|
type args struct {
|
|
controller Controller
|
|
svc *Services
|
|
}
|
|
type expected struct {
|
|
status int
|
|
headers map[string]string
|
|
body []byte
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
expected expected
|
|
}{
|
|
{
|
|
name: "no services successful response",
|
|
args: args{
|
|
svc: &Services{},
|
|
controller: func(ctx *fiber.Ctx) (*Response, error) {
|
|
return &Response{}, nil
|
|
},
|
|
},
|
|
expected: expected{
|
|
status: http.StatusOK,
|
|
},
|
|
},
|
|
{
|
|
name: "handle api error",
|
|
args: args{
|
|
svc: services,
|
|
controller: func(ctx *fiber.Ctx) (*Response, error) {
|
|
return &Response{}, s3err.GetAPIError(s3err.ErrInvalidRequest)
|
|
},
|
|
},
|
|
expected: expected{
|
|
status: http.StatusBadRequest,
|
|
body: s3err.GetAPIError(s3err.ErrInvalidRequest).XMLBody(testRequestID, testHostID),
|
|
},
|
|
},
|
|
{
|
|
name: "handle custom error",
|
|
args: args{
|
|
svc: services,
|
|
controller: func(ctx *fiber.Ctx) (*Response, error) {
|
|
return &Response{}, errors.New("custom error")
|
|
},
|
|
},
|
|
expected: expected{
|
|
status: http.StatusInternalServerError,
|
|
body: s3err.GetAPIError(s3err.ErrInternalError).XMLBody(testRequestID, testHostID),
|
|
},
|
|
},
|
|
{
|
|
name: "body parsing fails",
|
|
args: args{
|
|
svc: services,
|
|
controller: func(ctx *fiber.Ctx) (*Response, error) {
|
|
return &Response{
|
|
Data: make(chan int),
|
|
}, nil
|
|
},
|
|
},
|
|
expected: expected{
|
|
status: http.StatusInternalServerError,
|
|
body: s3err.GetAPIError(s3err.ErrInternalError).XMLBody(testRequestID, testHostID),
|
|
},
|
|
},
|
|
{
|
|
name: "no data payload",
|
|
args: args{
|
|
svc: services,
|
|
controller: func(ctx *fiber.Ctx) (*Response, error) {
|
|
return &Response{
|
|
MetaOpts: &MetaOptions{
|
|
ObjectCount: 2,
|
|
},
|
|
}, nil
|
|
},
|
|
},
|
|
expected: expected{
|
|
status: http.StatusOK,
|
|
},
|
|
},
|
|
{
|
|
name: "should return 204 http status",
|
|
args: args{
|
|
svc: services,
|
|
controller: func(ctx *fiber.Ctx) (*Response, error) {
|
|
return &Response{
|
|
MetaOpts: &MetaOptions{
|
|
Status: http.StatusNoContent,
|
|
},
|
|
}, nil
|
|
},
|
|
},
|
|
expected: expected{
|
|
status: http.StatusNoContent,
|
|
},
|
|
},
|
|
{
|
|
name: "already encoded payload",
|
|
args: args{
|
|
svc: services,
|
|
controller: func(ctx *fiber.Ctx) (*Response, error) {
|
|
return &Response{
|
|
Data: []byte("encoded_data"),
|
|
}, nil
|
|
},
|
|
},
|
|
expected: expected{
|
|
status: http.StatusOK,
|
|
body: []byte("encoded_data"),
|
|
headers: map[string]string{
|
|
"Content-Length": "12",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "should set response headers",
|
|
args: args{
|
|
svc: services,
|
|
controller: func(ctx *fiber.Ctx) (*Response, error) {
|
|
return &Response{
|
|
Headers: map[string]*string{
|
|
"X-Amz-My-Custom-Header": utils.GetStringPtr("my_value"),
|
|
"X-Amz-Meta-My-Meta": utils.GetStringPtr("my_meta"),
|
|
},
|
|
}, nil
|
|
},
|
|
},
|
|
expected: expected{
|
|
status: http.StatusOK,
|
|
headers: map[string]string{
|
|
"X-Amz-My-Custom-Header": "my_value",
|
|
"X-Amz-Meta-My-Meta": "my_meta",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "large payload: should return internal error",
|
|
args: args{
|
|
svc: services,
|
|
controller: func(ctx *fiber.Ctx) (*Response, error) {
|
|
type Item struct {
|
|
Value string `xml:"value"`
|
|
}
|
|
|
|
type payload struct {
|
|
Items []Item `xml:"item"`
|
|
}
|
|
|
|
const targetSize = 5 * 1024 * 1024 // 5 MiB
|
|
const itemCount = 500
|
|
const valueSize = targetSize / itemCount
|
|
|
|
p := payload{
|
|
Items: make([]Item, itemCount),
|
|
}
|
|
|
|
// Preallocate one shared string of desired size
|
|
var sb strings.Builder
|
|
sb.Grow(valueSize)
|
|
for range valueSize {
|
|
sb.WriteByte('A')
|
|
}
|
|
largeValue := sb.String()
|
|
|
|
for i := range p.Items {
|
|
p.Items[i].Value = largeValue
|
|
}
|
|
|
|
return &Response{
|
|
Data: p,
|
|
}, nil
|
|
},
|
|
},
|
|
expected: expected{
|
|
body: s3err.GetAPIError(s3err.ErrInternalError).XMLBody(testRequestID, testHostID),
|
|
status: http.StatusInternalServerError,
|
|
},
|
|
},
|
|
{
|
|
name: "not encoded payload",
|
|
args: args{
|
|
svc: services,
|
|
controller: func(ctx *fiber.Ctx) (*Response, error) {
|
|
return &Response{
|
|
Data: s3response.Bucket{
|
|
Name: "something",
|
|
},
|
|
}, nil
|
|
},
|
|
},
|
|
expected: expected{
|
|
headers: map[string]string{
|
|
"Content-Length": fmt.Sprint(payloadLen),
|
|
},
|
|
body: append(xmlhdr, payload...),
|
|
status: http.StatusOK,
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
ctx := fiber.New().AcquireCtx(&fasthttp.RequestCtx{})
|
|
utils.ContextKeyRequestID.Set(ctx, testRequestID)
|
|
utils.ContextKeyHostID.Set(ctx, testHostID)
|
|
err := ProcessController(ctx, tt.args.controller, metrics.ActionAbortMultipartUpload, tt.args.svc)
|
|
assert.NoError(t, err)
|
|
|
|
// check the status
|
|
assert.Equal(t, tt.expected.status, ctx.Response().StatusCode())
|
|
assert.Equal(t, testRequestID, string(ctx.Response().Header.Peek(utils.HeaderAmzRequestID)))
|
|
assert.Equal(t, testHostID, string(ctx.Response().Header.Peek(utils.HeaderAmzID2)))
|
|
|
|
// check the response headers to be set
|
|
if tt.expected.headers != nil {
|
|
for key, val := range tt.expected.headers {
|
|
v := ctx.Response().Header.Peek(key)
|
|
assert.Equal(t, val, string(v))
|
|
}
|
|
}
|
|
|
|
// check the response body
|
|
if tt.expected.body != nil {
|
|
assert.Equal(t, tt.expected.body, ctx.Response().Body())
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestProcessHandlers(t *testing.T) {
|
|
payload, err := xml.Marshal(s3response.Checksum{
|
|
CRC32: utils.GetStringPtr("crc32"),
|
|
})
|
|
assert.NoError(t, err)
|
|
|
|
type args struct {
|
|
controller Controller
|
|
svc *Services
|
|
handlers []fiber.Handler
|
|
locals map[utils.ContextKey]any
|
|
}
|
|
type expected struct {
|
|
body []byte
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
expected expected
|
|
}{
|
|
{
|
|
name: "should skip the handlers",
|
|
args: args{
|
|
locals: map[utils.ContextKey]any{
|
|
utils.ContextKeySkip: true,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "handler returns error",
|
|
args: args{
|
|
handlers: []fiber.Handler{
|
|
func(ctx *fiber.Ctx) error {
|
|
return nil
|
|
},
|
|
func(ctx *fiber.Ctx) error {
|
|
return s3err.GetAPIError(s3err.ErrAccessDenied)
|
|
},
|
|
},
|
|
svc: &Services{},
|
|
},
|
|
expected: expected{
|
|
body: s3err.GetAPIError(s3err.ErrAccessDenied).XMLBody(testRequestID, testHostID),
|
|
},
|
|
},
|
|
{
|
|
name: "should process the controller",
|
|
args: args{
|
|
handlers: []fiber.Handler{
|
|
func(ctx *fiber.Ctx) error {
|
|
return nil
|
|
},
|
|
func(ctx *fiber.Ctx) error {
|
|
return nil
|
|
},
|
|
},
|
|
svc: &Services{},
|
|
controller: func(ctx *fiber.Ctx) (*Response, error) {
|
|
return &Response{
|
|
Data: s3response.Checksum{
|
|
CRC32: utils.GetStringPtr("crc32"),
|
|
},
|
|
}, nil
|
|
},
|
|
},
|
|
expected: expected{
|
|
body: append(xmlhdr, payload...),
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
mdlwr := ProcessHandlers(tt.args.controller, metrics.ActionCreateBucket, tt.args.svc, tt.args.handlers...)
|
|
|
|
app := fiber.New()
|
|
|
|
app.Post("/:bucket/*", func(ctx *fiber.Ctx) error {
|
|
utils.ContextKeyRequestID.Set(ctx, testRequestID)
|
|
utils.ContextKeyHostID.Set(ctx, testHostID)
|
|
|
|
// set the request locals
|
|
if tt.args.locals != nil {
|
|
for key, val := range tt.args.locals {
|
|
key.Set(ctx, val)
|
|
}
|
|
}
|
|
|
|
// call the controller by passing the ctx
|
|
err := mdlwr(ctx)
|
|
assert.NoError(t, err)
|
|
|
|
// check the response body
|
|
if tt.expected.body != nil {
|
|
assert.Equal(t, tt.expected.body, ctx.Response().Body())
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
app.All("*", func(ctx *fiber.Ctx) error {
|
|
return nil
|
|
})
|
|
|
|
req := buildRequest("bucket", "object", nil, nil, nil)
|
|
|
|
_, err := app.Test(req)
|
|
assert.NoError(t, err)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestWrapMiddleware(t *testing.T) {
|
|
type args struct {
|
|
handler fiber.Handler
|
|
logger s3log.AuditLogger
|
|
mm metrics.Manager
|
|
}
|
|
type expected struct {
|
|
body []byte
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
expected expected
|
|
}{
|
|
{
|
|
name: "handler returns no error",
|
|
args: args{
|
|
handler: func(ctx *fiber.Ctx) error {
|
|
return nil
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "handler returns api error",
|
|
args: args{
|
|
handler: func(ctx *fiber.Ctx) error {
|
|
return s3err.GetAPIError(s3err.ErrAclNotSupported)
|
|
},
|
|
mm: &mockMetricsManager{},
|
|
logger: &mockAuditLogger{},
|
|
},
|
|
expected: expected{
|
|
body: s3err.GetAPIError(s3err.ErrAclNotSupported).XMLBody(testRequestID, testHostID),
|
|
},
|
|
},
|
|
{
|
|
name: "handler returns custom error",
|
|
args: args{
|
|
handler: func(ctx *fiber.Ctx) error {
|
|
return errors.New("custom error")
|
|
},
|
|
},
|
|
expected: expected{
|
|
body: s3err.GetAPIError(s3err.ErrInternalError).XMLBody(testRequestID, testHostID),
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
mdlwr := WrapMiddleware(tt.args.handler, tt.args.logger, tt.args.mm)
|
|
app := fiber.New()
|
|
|
|
app.Post("/:bucket/*", func(ctx *fiber.Ctx) error {
|
|
utils.ContextKeyRequestID.Set(ctx, testRequestID)
|
|
utils.ContextKeyHostID.Set(ctx, testHostID)
|
|
|
|
// call the controller by passing the ctx
|
|
err := mdlwr(ctx)
|
|
assert.NoError(t, err)
|
|
|
|
// check the response body
|
|
if tt.expected.body != nil {
|
|
assert.Equal(t, tt.expected.body, ctx.Response().Body())
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
app.All("*", func(ctx *fiber.Ctx) error {
|
|
return nil
|
|
})
|
|
|
|
req := buildRequest("bucket", "object", nil, nil, nil)
|
|
|
|
_, err := app.Test(req)
|
|
assert.NoError(t, err)
|
|
})
|
|
}
|
|
}
|