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

     1  /*
     2   * MinIO Cloud Storage, (C) 2017 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/http"
    21  	"testing"
    22  )
    23  
    24  // Tests object location.
    25  func TestObjectLocation(t *testing.T) {
    26  	testCases := []struct {
    27  		request          *http.Request
    28  		bucket, object   string
    29  		domains          []string
    30  		expectedLocation string
    31  	}{
    32  		// Server binding to localhost IP with https.
    33  		{
    34  			request: &http.Request{
    35  				Host: "127.0.0.1:9000",
    36  				Header: map[string][]string{
    37  					"X-Forwarded-Scheme": {httpScheme},
    38  				},
    39  			},
    40  			bucket:           "testbucket1",
    41  			object:           "test/1.txt",
    42  			expectedLocation: "http://127.0.0.1:9000/testbucket1/test/1.txt",
    43  		},
    44  		{
    45  			request: &http.Request{
    46  				Host: "127.0.0.1:9000",
    47  				Header: map[string][]string{
    48  					"X-Forwarded-Scheme": {httpsScheme},
    49  				},
    50  			},
    51  			bucket:           "testbucket1",
    52  			object:           "test/1.txt",
    53  			expectedLocation: "https://127.0.0.1:9000/testbucket1/test/1.txt",
    54  		},
    55  		// Server binding to fqdn.
    56  		{
    57  			request: &http.Request{
    58  				Host: "s3.mybucket.org",
    59  				Header: map[string][]string{
    60  					"X-Forwarded-Scheme": {httpScheme},
    61  				},
    62  			},
    63  			bucket:           "mybucket",
    64  			object:           "test/1.txt",
    65  			expectedLocation: "http://s3.mybucket.org/mybucket/test/1.txt",
    66  		},
    67  		// Server binding to fqdn.
    68  		{
    69  			request: &http.Request{
    70  				Host:   "mys3.mybucket.org",
    71  				Header: map[string][]string{},
    72  			},
    73  			bucket:           "mybucket",
    74  			object:           "test/1.txt",
    75  			expectedLocation: "http://mys3.mybucket.org/mybucket/test/1.txt",
    76  		},
    77  		// Server with virtual domain name.
    78  		{
    79  			request: &http.Request{
    80  				Host:   "mybucket.mys3.bucket.org",
    81  				Header: map[string][]string{},
    82  			},
    83  			domains:          []string{"mys3.bucket.org"},
    84  			bucket:           "mybucket",
    85  			object:           "test/1.txt",
    86  			expectedLocation: "http://mybucket.mys3.bucket.org/test/1.txt",
    87  		},
    88  		{
    89  			request: &http.Request{
    90  				Host: "mybucket.mys3.bucket.org",
    91  				Header: map[string][]string{
    92  					"X-Forwarded-Scheme": {httpsScheme},
    93  				},
    94  			},
    95  			domains:          []string{"mys3.bucket.org"},
    96  			bucket:           "mybucket",
    97  			object:           "test/1.txt",
    98  			expectedLocation: "https://mybucket.mys3.bucket.org/test/1.txt",
    99  		},
   100  	}
   101  	for _, testCase := range testCases {
   102  		testCase := testCase
   103  		t.Run("", func(t *testing.T) {
   104  			gotLocation := getObjectLocation(testCase.request, testCase.domains, testCase.bucket, testCase.object)
   105  			if testCase.expectedLocation != gotLocation {
   106  				t.Errorf("expected %s, got %s", testCase.expectedLocation, gotLocation)
   107  			}
   108  		})
   109  	}
   110  }
   111  
   112  // Tests getURLScheme function behavior.
   113  func TestGetURLScheme(t *testing.T) {
   114  	tls := false
   115  	gotScheme := getURLScheme(tls)
   116  	if gotScheme != httpScheme {
   117  		t.Errorf("Expected %s, got %s", httpScheme, gotScheme)
   118  	}
   119  	tls = true
   120  	gotScheme = getURLScheme(tls)
   121  	if gotScheme != httpsScheme {
   122  		t.Errorf("Expected %s, got %s", httpsScheme, gotScheme)
   123  	}
   124  }