TestMakeTargetURL - testing makeTargetURL()
(t *testing.T)
| 179 | |
| 180 | // TestMakeTargetURL - testing makeTargetURL() |
| 181 | func TestMakeTargetURL(t *testing.T) { |
| 182 | testCases := []struct { |
| 183 | addr string |
| 184 | secure bool |
| 185 | bucketName string |
| 186 | objectName string |
| 187 | bucketLocation string |
| 188 | queryValues map[string][]string |
| 189 | expectedURL url.URL |
| 190 | expectedErr error |
| 191 | }{ |
| 192 | // Test 1 |
| 193 | {"localhost:9000", false, "", "", "", nil, url.URL{Host: "localhost:9000", Scheme: "http", Path: "/"}, nil}, |
| 194 | // Test 2 |
| 195 | {"localhost", true, "", "", "", nil, url.URL{Host: "localhost", Scheme: "https", Path: "/"}, nil}, |
| 196 | // Test 3 |
| 197 | {"localhost:9000", true, "mybucket", "", "", nil, url.URL{Host: "localhost:9000", Scheme: "https", Path: "/mybucket/"}, nil}, |
| 198 | // Test 4, testing against google storage API |
| 199 | {"storage.googleapis.com", true, "mybucket", "", "", nil, url.URL{Host: "mybucket.storage.googleapis.com", Scheme: "https", Path: "/"}, nil}, |
| 200 | // Test 5, testing against AWS S3 API |
| 201 | {"s3.amazonaws.com", true, "mybucket", "myobject", "", nil, url.URL{Host: "mybucket.s3.dualstack.us-east-1.amazonaws.com", Scheme: "https", Path: "/myobject"}, nil}, |
| 202 | // Test 6 |
| 203 | {"localhost:9000", false, "mybucket", "myobject", "", nil, url.URL{Host: "localhost:9000", Scheme: "http", Path: "/mybucket/myobject"}, nil}, |
| 204 | // Test 7, testing with query |
| 205 | {"localhost:9000", false, "mybucket", "myobject", "", map[string][]string{"param": {"val"}}, url.URL{Host: "localhost:9000", Scheme: "http", Path: "/mybucket/myobject", RawQuery: "param=val"}, nil}, |
| 206 | // Test 8, testing with port 80 |
| 207 | {"localhost:80", false, "mybucket", "myobject", "", nil, url.URL{Host: "localhost", Scheme: "http", Path: "/mybucket/myobject"}, nil}, |
| 208 | // Test 9, testing with port 443 |
| 209 | {"localhost:443", true, "mybucket", "myobject", "", nil, url.URL{Host: "localhost", Scheme: "https", Path: "/mybucket/myobject"}, nil}, |
| 210 | {"[240b:c0e0:102:54C0:1c05:c2c1:19:5001]:443", true, "mybucket", "myobject", "", nil, url.URL{Host: "[240b:c0e0:102:54C0:1c05:c2c1:19:5001]", Scheme: "https", Path: "/mybucket/myobject"}, nil}, |
| 211 | {"[240b:c0e0:102:54C0:1c05:c2c1:19:5001]:9000", true, "mybucket", "myobject", "", nil, url.URL{Host: "[240b:c0e0:102:54C0:1c05:c2c1:19:5001]:9000", Scheme: "https", Path: "/mybucket/myobject"}, nil}, |
| 212 | } |
| 213 | |
| 214 | for i, testCase := range testCases { |
| 215 | // Initialize a MinIO client |
| 216 | c, _ := New(testCase.addr, &Options{ |
| 217 | Creds: credentials.NewStaticV4("foo", "bar", ""), |
| 218 | Secure: testCase.secure, |
| 219 | }) |
| 220 | isVirtualHost := c.isVirtualHostStyleRequest(*c.endpointURL, testCase.bucketName) |
| 221 | u, err := c.makeTargetURL(testCase.bucketName, testCase.objectName, testCase.bucketLocation, isVirtualHost, testCase.queryValues) |
| 222 | // Check the returned error |
| 223 | if testCase.expectedErr == nil && err != nil { |
| 224 | t.Fatalf("Test %d: Should succeed but failed with err = %v", i+1, err) |
| 225 | } |
| 226 | if testCase.expectedErr != nil && err == nil { |
| 227 | t.Fatalf("Test %d: Should fail but succeeded", i+1) |
| 228 | } |
| 229 | if err == nil { |
| 230 | // Check if the returned url is equal to what we expect |
| 231 | if u.String() != testCase.expectedURL.String() { |
| 232 | t.Fatalf("Test %d: Mismatched target url: expected = `%v`, found = `%v`", |
| 233 | i+1, testCase.expectedURL.String(), u.String()) |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | } |
nothing calls this directly
no test coverage detected