| 
									
										
										
										
											2017-09-25 07:43:21 +08:00
										 |  |  | /* | 
					
						
							|  |  |  |  * Minio Cloud Storage, (C) 2017 Minio, Inc. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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 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 | 
					
						
							|  |  |  | 		domain           string | 
					
						
							|  |  |  | 		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{ | 
					
						
							|  |  |  | 				Host:   "mys3.bucket.org", | 
					
						
							|  |  |  | 				Header: map[string][]string{}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			domain:           "mys3.bucket.org", | 
					
						
							|  |  |  | 			bucket:           "mybucket", | 
					
						
							|  |  |  | 			object:           "test/1.txt", | 
					
						
							|  |  |  | 			expectedLocation: "http://mybucket.mys3.bucket.org/test/1.txt", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			request: &http.Request{ | 
					
						
							|  |  |  | 				Host: "mys3.bucket.org", | 
					
						
							|  |  |  | 				Header: map[string][]string{ | 
					
						
							|  |  |  | 					"X-Forwarded-Scheme": {httpsScheme}, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			domain:           "mys3.bucket.org", | 
					
						
							|  |  |  | 			bucket:           "mybucket", | 
					
						
							|  |  |  | 			object:           "test/1.txt", | 
					
						
							|  |  |  | 			expectedLocation: "https://mybucket.mys3.bucket.org/test/1.txt", | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2017-09-25 07:43:21 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	for i, testCase := range testCases { | 
					
						
							| 
									
										
										
										
											2018-03-03 07:23:04 +08:00
										 |  |  | 		gotLocation := getObjectLocation(testCase.request, testCase.domain, testCase.bucket, testCase.object) | 
					
						
							| 
									
										
										
										
											2017-09-25 07:43:21 +08:00
										 |  |  | 		if testCase.expectedLocation != gotLocation { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: expected %s, got %s", i+1, testCase.expectedLocation, gotLocation) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 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) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |