storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/api-resources_test.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2016 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package cmd
    18  
    19  import (
    20  	"net/url"
    21  	"testing"
    22  )
    23  
    24  // Test list objects resources V2.
    25  func TestListObjectsV2Resources(t *testing.T) {
    26  	testCases := []struct {
    27  		values                               url.Values
    28  		prefix, token, startAfter, delimiter string
    29  		fetchOwner                           bool
    30  		maxKeys                              int
    31  		encodingType                         string
    32  		errCode                              APIErrorCode
    33  	}{
    34  		{
    35  			values: url.Values{
    36  				"prefix":             []string{"photos/"},
    37  				"continuation-token": []string{"dG9rZW4="},
    38  				"start-after":        []string{"start-after"},
    39  				"delimiter":          []string{SlashSeparator},
    40  				"fetch-owner":        []string{"true"},
    41  				"max-keys":           []string{"100"},
    42  				"encoding-type":      []string{"gzip"},
    43  			},
    44  			prefix:       "photos/",
    45  			token:        "token",
    46  			startAfter:   "start-after",
    47  			delimiter:    SlashSeparator,
    48  			fetchOwner:   true,
    49  			maxKeys:      100,
    50  			encodingType: "gzip",
    51  			errCode:      ErrNone,
    52  		},
    53  		{
    54  			values: url.Values{
    55  				"prefix":             []string{"photos/"},
    56  				"continuation-token": []string{"dG9rZW4="},
    57  				"start-after":        []string{"start-after"},
    58  				"delimiter":          []string{SlashSeparator},
    59  				"fetch-owner":        []string{"true"},
    60  				"encoding-type":      []string{"gzip"},
    61  			},
    62  			prefix:       "photos/",
    63  			token:        "token",
    64  			startAfter:   "start-after",
    65  			delimiter:    SlashSeparator,
    66  			fetchOwner:   true,
    67  			maxKeys:      maxObjectList,
    68  			encodingType: "gzip",
    69  			errCode:      ErrNone,
    70  		},
    71  		{
    72  			values: url.Values{
    73  				"prefix":             []string{"photos/"},
    74  				"continuation-token": []string{""},
    75  				"start-after":        []string{"start-after"},
    76  				"delimiter":          []string{SlashSeparator},
    77  				"fetch-owner":        []string{"true"},
    78  				"encoding-type":      []string{"gzip"},
    79  			},
    80  			prefix:       "",
    81  			token:        "",
    82  			startAfter:   "",
    83  			delimiter:    "",
    84  			fetchOwner:   false,
    85  			maxKeys:      0,
    86  			encodingType: "",
    87  			errCode:      ErrIncorrectContinuationToken,
    88  		},
    89  	}
    90  
    91  	for i, testCase := range testCases {
    92  		prefix, token, startAfter, delimiter, fetchOwner, maxKeys, encodingType, errCode := getListObjectsV2Args(testCase.values)
    93  
    94  		if errCode != testCase.errCode {
    95  			t.Errorf("Test %d: Expected error code:%d, got %d", i+1, testCase.errCode, errCode)
    96  		}
    97  		if prefix != testCase.prefix {
    98  			t.Errorf("Test %d: Expected %s, got %s", i+1, testCase.prefix, prefix)
    99  		}
   100  		if token != testCase.token {
   101  			t.Errorf("Test %d: Expected %s, got %s", i+1, testCase.token, token)
   102  		}
   103  		if startAfter != testCase.startAfter {
   104  			t.Errorf("Test %d: Expected %s, got %s", i+1, testCase.startAfter, startAfter)
   105  		}
   106  		if delimiter != testCase.delimiter {
   107  			t.Errorf("Test %d: Expected %s, got %s", i+1, testCase.delimiter, delimiter)
   108  		}
   109  		if fetchOwner != testCase.fetchOwner {
   110  			t.Errorf("Test %d: Expected %t, got %t", i+1, testCase.fetchOwner, fetchOwner)
   111  		}
   112  		if maxKeys != testCase.maxKeys {
   113  			t.Errorf("Test %d: Expected %d, got %d", i+1, testCase.maxKeys, maxKeys)
   114  		}
   115  		if encodingType != testCase.encodingType {
   116  			t.Errorf("Test %d: Expected %s, got %s", i+1, testCase.encodingType, encodingType)
   117  		}
   118  	}
   119  }
   120  
   121  // Test list objects resources V1.
   122  func TestListObjectsV1Resources(t *testing.T) {
   123  	testCases := []struct {
   124  		values                    url.Values
   125  		prefix, marker, delimiter string
   126  		maxKeys                   int
   127  		encodingType              string
   128  	}{
   129  		{
   130  			values: url.Values{
   131  				"prefix":        []string{"photos/"},
   132  				"marker":        []string{"test"},
   133  				"delimiter":     []string{SlashSeparator},
   134  				"max-keys":      []string{"100"},
   135  				"encoding-type": []string{"gzip"},
   136  			},
   137  			prefix:       "photos/",
   138  			marker:       "test",
   139  			delimiter:    SlashSeparator,
   140  			maxKeys:      100,
   141  			encodingType: "gzip",
   142  		},
   143  		{
   144  			values: url.Values{
   145  				"prefix":        []string{"photos/"},
   146  				"marker":        []string{"test"},
   147  				"delimiter":     []string{SlashSeparator},
   148  				"encoding-type": []string{"gzip"},
   149  			},
   150  			prefix:       "photos/",
   151  			marker:       "test",
   152  			delimiter:    SlashSeparator,
   153  			maxKeys:      maxObjectList,
   154  			encodingType: "gzip",
   155  		},
   156  	}
   157  
   158  	for i, testCase := range testCases {
   159  		prefix, marker, delimiter, maxKeys, encodingType, argsErr := getListObjectsV1Args(testCase.values)
   160  		if argsErr != ErrNone {
   161  			t.Errorf("Test %d: argument parsing failed, got %v", i+1, argsErr)
   162  		}
   163  		if prefix != testCase.prefix {
   164  			t.Errorf("Test %d: Expected %s, got %s", i+1, testCase.prefix, prefix)
   165  		}
   166  		if marker != testCase.marker {
   167  			t.Errorf("Test %d: Expected %s, got %s", i+1, testCase.marker, marker)
   168  		}
   169  		if delimiter != testCase.delimiter {
   170  			t.Errorf("Test %d: Expected %s, got %s", i+1, testCase.delimiter, delimiter)
   171  		}
   172  		if maxKeys != testCase.maxKeys {
   173  			t.Errorf("Test %d: Expected %d, got %d", i+1, testCase.maxKeys, maxKeys)
   174  		}
   175  		if encodingType != testCase.encodingType {
   176  			t.Errorf("Test %d: Expected %s, got %s", i+1, testCase.encodingType, encodingType)
   177  		}
   178  	}
   179  }
   180  
   181  // Validates extracting information for object resources.
   182  func TestGetObjectsResources(t *testing.T) {
   183  	testCases := []struct {
   184  		values                     url.Values
   185  		uploadID                   string
   186  		partNumberMarker, maxParts int
   187  		encodingType               string
   188  	}{
   189  		{
   190  			values: url.Values{
   191  				"uploadId":           []string{"11123-11312312311231-12313"},
   192  				"part-number-marker": []string{"1"},
   193  				"max-parts":          []string{"1000"},
   194  				"encoding-type":      []string{"gzip"},
   195  			},
   196  			uploadID:         "11123-11312312311231-12313",
   197  			partNumberMarker: 1,
   198  			maxParts:         1000,
   199  			encodingType:     "gzip",
   200  		},
   201  	}
   202  
   203  	for i, testCase := range testCases {
   204  		uploadID, partNumberMarker, maxParts, encodingType, argsErr := getObjectResources(testCase.values)
   205  		if argsErr != ErrNone {
   206  			t.Errorf("Test %d: argument parsing failed, got %v", i+1, argsErr)
   207  		}
   208  		if uploadID != testCase.uploadID {
   209  			t.Errorf("Test %d: Expected %s, got %s", i+1, testCase.uploadID, uploadID)
   210  		}
   211  		if partNumberMarker != testCase.partNumberMarker {
   212  			t.Errorf("Test %d: Expected %d, got %d", i+1, testCase.partNumberMarker, partNumberMarker)
   213  		}
   214  		if maxParts != testCase.maxParts {
   215  			t.Errorf("Test %d: Expected %d, got %d", i+1, testCase.maxParts, maxParts)
   216  		}
   217  		if encodingType != testCase.encodingType {
   218  			t.Errorf("Test %d: Expected %s, got %s", i+1, testCase.encodingType, encodingType)
   219  		}
   220  	}
   221  }