github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/cmd/api-response_test.go (about)

     1  // Copyright (c) 2015-2021 MinIO, Inc.
     2  //
     3  // This file is part of MinIO Object Storage stack
     4  //
     5  // This program is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Affero General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // This program is distributed in the hope that it will be useful
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU Affero General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Affero General Public License
    16  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package cmd
    19  
    20  import (
    21  	"net/http"
    22  	"testing"
    23  )
    24  
    25  // Tests object location.
    26  func TestObjectLocation(t *testing.T) {
    27  	testCases := []struct {
    28  		request          *http.Request
    29  		bucket, object   string
    30  		domains          []string
    31  		expectedLocation string
    32  	}{
    33  		// Server binding to localhost IP with https.
    34  		{
    35  			request: &http.Request{
    36  				Host: "127.0.0.1:9000",
    37  				Header: map[string][]string{
    38  					"X-Forwarded-Scheme": {httpScheme},
    39  				},
    40  			},
    41  			bucket:           "testbucket1",
    42  			object:           "test/1.txt",
    43  			expectedLocation: "http://127.0.0.1:9000/testbucket1/test/1.txt",
    44  		},
    45  		{
    46  			request: &http.Request{
    47  				Host: "127.0.0.1:9000",
    48  				Header: map[string][]string{
    49  					"X-Forwarded-Scheme": {httpsScheme},
    50  				},
    51  			},
    52  			bucket:           "testbucket1",
    53  			object:           "test/1.txt",
    54  			expectedLocation: "https://127.0.0.1:9000/testbucket1/test/1.txt",
    55  		},
    56  		// Server binding to fqdn.
    57  		{
    58  			request: &http.Request{
    59  				Host: "s3.mybucket.org",
    60  				Header: map[string][]string{
    61  					"X-Forwarded-Scheme": {httpScheme},
    62  				},
    63  			},
    64  			bucket:           "mybucket",
    65  			object:           "test/1.txt",
    66  			expectedLocation: "http://s3.mybucket.org/mybucket/test/1.txt",
    67  		},
    68  		// Server binding to fqdn.
    69  		{
    70  			request: &http.Request{
    71  				Host:   "mys3.mybucket.org",
    72  				Header: map[string][]string{},
    73  			},
    74  			bucket:           "mybucket",
    75  			object:           "test/1.txt",
    76  			expectedLocation: "http://mys3.mybucket.org/mybucket/test/1.txt",
    77  		},
    78  		// Server with virtual domain name.
    79  		{
    80  			request: &http.Request{
    81  				Host:   "mybucket.mys3.bucket.org",
    82  				Header: map[string][]string{},
    83  			},
    84  			domains:          []string{"mys3.bucket.org"},
    85  			bucket:           "mybucket",
    86  			object:           "test/1.txt",
    87  			expectedLocation: "http://mybucket.mys3.bucket.org/test/1.txt",
    88  		},
    89  		{
    90  			request: &http.Request{
    91  				Host: "mybucket.mys3.bucket.org",
    92  				Header: map[string][]string{
    93  					"X-Forwarded-Scheme": {httpsScheme},
    94  				},
    95  			},
    96  			domains:          []string{"mys3.bucket.org"},
    97  			bucket:           "mybucket",
    98  			object:           "test/1.txt",
    99  			expectedLocation: "https://mybucket.mys3.bucket.org/test/1.txt",
   100  		},
   101  	}
   102  	for _, testCase := range testCases {
   103  		testCase := testCase
   104  		t.Run("", func(t *testing.T) {
   105  			gotLocation := getObjectLocation(testCase.request, testCase.domains, testCase.bucket, testCase.object)
   106  			if testCase.expectedLocation != gotLocation {
   107  				t.Errorf("expected %s, got %s", testCase.expectedLocation, gotLocation)
   108  			}
   109  		})
   110  	}
   111  }
   112  
   113  // Tests getURLScheme function behavior.
   114  func TestGetURLScheme(t *testing.T) {
   115  	tls := false
   116  	gotScheme := getURLScheme(tls)
   117  	if gotScheme != httpScheme {
   118  		t.Errorf("Expected %s, got %s", httpScheme, gotScheme)
   119  	}
   120  	tls = true
   121  	gotScheme = getURLScheme(tls)
   122  	if gotScheme != httpsScheme {
   123  		t.Errorf("Expected %s, got %s", httpsScheme, gotScheme)
   124  	}
   125  }