| 
									
										
										
										
											2021-04-19 03:41:13 +08:00
										 |  |  | // Copyright (c) 2015-2021 MinIO, Inc.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This file is part of MinIO Object Storage stack
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This program is free software: you can redistribute it and/or modify
 | 
					
						
							|  |  |  | // it under the terms of the GNU Affero General Public License as published by
 | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or
 | 
					
						
							|  |  |  | // (at your option) any later version.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This program is distributed in the hope that it will be useful
 | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
					
						
							|  |  |  | // GNU Affero General Public License for more details.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // You should have received a copy of the GNU Affero General Public License
 | 
					
						
							|  |  |  | // along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							| 
									
										
										
										
											2017-09-25 07:43:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | package cmd | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2018-03-03 07:23:04 +08:00
										 |  |  | 	"net/http" | 
					
						
							| 
									
										
										
										
											2017-09-25 07:43:21 +08:00
										 |  |  | 	"testing" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Tests object location.
 | 
					
						
							|  |  |  | func TestObjectLocation(t *testing.T) { | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							| 
									
										
										
										
											2018-03-03 07:23:04 +08:00
										 |  |  | 		request          *http.Request | 
					
						
							|  |  |  | 		bucket, object   string | 
					
						
							| 
									
										
										
										
											2019-02-23 11:18:01 +08:00
										 |  |  | 		domains          []string | 
					
						
							| 
									
										
										
										
											2018-03-03 07:23:04 +08:00
										 |  |  | 		expectedLocation string | 
					
						
							| 
									
										
										
										
											2017-09-25 07:43:21 +08:00
										 |  |  | 	}{ | 
					
						
							|  |  |  | 		// Server binding to localhost IP with https.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-03-03 07:23:04 +08:00
										 |  |  | 			request: &http.Request{ | 
					
						
							|  |  |  | 				Host: "127.0.0.1:9000", | 
					
						
							|  |  |  | 				Header: map[string][]string{ | 
					
						
							|  |  |  | 					"X-Forwarded-Scheme": {httpScheme}, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			bucket:           "testbucket1", | 
					
						
							|  |  |  | 			object:           "test/1.txt", | 
					
						
							|  |  |  | 			expectedLocation: "http://127.0.0.1:9000/testbucket1/test/1.txt", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			request: &http.Request{ | 
					
						
							|  |  |  | 				Host: "127.0.0.1:9000", | 
					
						
							|  |  |  | 				Header: map[string][]string{ | 
					
						
							|  |  |  | 					"X-Forwarded-Scheme": {httpsScheme}, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2017-09-25 07:43:21 +08:00
										 |  |  | 			bucket:           "testbucket1", | 
					
						
							|  |  |  | 			object:           "test/1.txt", | 
					
						
							|  |  |  | 			expectedLocation: "https://127.0.0.1:9000/testbucket1/test/1.txt", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		// Server binding to fqdn.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-03-03 07:23:04 +08:00
										 |  |  | 			request: &http.Request{ | 
					
						
							|  |  |  | 				Host: "s3.mybucket.org", | 
					
						
							|  |  |  | 				Header: map[string][]string{ | 
					
						
							|  |  |  | 					"X-Forwarded-Scheme": {httpScheme}, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2017-09-25 07:43:21 +08:00
										 |  |  | 			bucket:           "mybucket", | 
					
						
							|  |  |  | 			object:           "test/1.txt", | 
					
						
							|  |  |  | 			expectedLocation: "http://s3.mybucket.org/mybucket/test/1.txt", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		// Server binding to fqdn.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-03-03 07:23:04 +08:00
										 |  |  | 			request: &http.Request{ | 
					
						
							|  |  |  | 				Host:   "mys3.mybucket.org", | 
					
						
							|  |  |  | 				Header: map[string][]string{}, | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2017-09-25 07:43:21 +08:00
										 |  |  | 			bucket:           "mybucket", | 
					
						
							|  |  |  | 			object:           "test/1.txt", | 
					
						
							|  |  |  | 			expectedLocation: "http://mys3.mybucket.org/mybucket/test/1.txt", | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2018-03-03 07:23:04 +08:00
										 |  |  | 		// Server with virtual domain name.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			request: &http.Request{ | 
					
						
							| 
									
										
										
										
											2020-07-17 04:28:29 +08:00
										 |  |  | 				Host:   "mybucket.mys3.bucket.org", | 
					
						
							| 
									
										
										
										
											2018-03-03 07:23:04 +08:00
										 |  |  | 				Header: map[string][]string{}, | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2019-02-23 11:18:01 +08:00
										 |  |  | 			domains:          []string{"mys3.bucket.org"}, | 
					
						
							| 
									
										
										
										
											2018-03-03 07:23:04 +08:00
										 |  |  | 			bucket:           "mybucket", | 
					
						
							|  |  |  | 			object:           "test/1.txt", | 
					
						
							|  |  |  | 			expectedLocation: "http://mybucket.mys3.bucket.org/test/1.txt", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			request: &http.Request{ | 
					
						
							| 
									
										
										
										
											2020-07-17 04:28:29 +08:00
										 |  |  | 				Host: "mybucket.mys3.bucket.org", | 
					
						
							| 
									
										
										
										
											2018-03-03 07:23:04 +08:00
										 |  |  | 				Header: map[string][]string{ | 
					
						
							|  |  |  | 					"X-Forwarded-Scheme": {httpsScheme}, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2019-02-23 11:18:01 +08:00
										 |  |  | 			domains:          []string{"mys3.bucket.org"}, | 
					
						
							| 
									
										
										
										
											2018-03-03 07:23:04 +08:00
										 |  |  | 			bucket:           "mybucket", | 
					
						
							|  |  |  | 			object:           "test/1.txt", | 
					
						
							|  |  |  | 			expectedLocation: "https://mybucket.mys3.bucket.org/test/1.txt", | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2017-09-25 07:43:21 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-07-17 04:28:29 +08:00
										 |  |  | 	for _, testCase := range testCases { | 
					
						
							|  |  |  | 		t.Run("", func(t *testing.T) { | 
					
						
							|  |  |  | 			gotLocation := getObjectLocation(testCase.request, testCase.domains, testCase.bucket, testCase.object) | 
					
						
							|  |  |  | 			if testCase.expectedLocation != gotLocation { | 
					
						
							|  |  |  | 				t.Errorf("expected %s, got %s", testCase.expectedLocation, gotLocation) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2017-09-25 07:43:21 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Tests getURLScheme function behavior.
 | 
					
						
							|  |  |  | func TestGetURLScheme(t *testing.T) { | 
					
						
							|  |  |  | 	tls := false | 
					
						
							|  |  |  | 	gotScheme := getURLScheme(tls) | 
					
						
							|  |  |  | 	if gotScheme != httpScheme { | 
					
						
							|  |  |  | 		t.Errorf("Expected %s, got %s", httpScheme, gotScheme) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	tls = true | 
					
						
							|  |  |  | 	gotScheme = getURLScheme(tls) | 
					
						
							|  |  |  | 	if gotScheme != httpsScheme { | 
					
						
							|  |  |  | 		t.Errorf("Expected %s, got %s", httpsScheme, gotScheme) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |