url_utils_test.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package restclient
  14. import (
  15. "path"
  16. "testing"
  17. "k8s.io/kubernetes/pkg/api/testapi"
  18. )
  19. func TestValidatesHostParameter(t *testing.T) {
  20. testCases := []struct {
  21. Host string
  22. APIPath string
  23. URL string
  24. Err bool
  25. }{
  26. {"127.0.0.1", "", "http://127.0.0.1/" + testapi.Default.GroupVersion().Version, false},
  27. {"127.0.0.1:8080", "", "http://127.0.0.1:8080/" + testapi.Default.GroupVersion().Version, false},
  28. {"foo.bar.com", "", "http://foo.bar.com/" + testapi.Default.GroupVersion().Version, false},
  29. {"http://host/prefix", "", "http://host/prefix/" + testapi.Default.GroupVersion().Version, false},
  30. {"http://host", "", "http://host/" + testapi.Default.GroupVersion().Version, false},
  31. {"http://host", "/", "http://host/" + testapi.Default.GroupVersion().Version, false},
  32. {"http://host", "/other", "http://host/other/" + testapi.Default.GroupVersion().Version, false},
  33. {"host/server", "", "", true},
  34. }
  35. for i, testCase := range testCases {
  36. u, versionedAPIPath, err := DefaultServerURL(testCase.Host, testCase.APIPath, *testapi.Default.GroupVersion(), false)
  37. switch {
  38. case err == nil && testCase.Err:
  39. t.Errorf("expected error but was nil")
  40. continue
  41. case err != nil && !testCase.Err:
  42. t.Errorf("unexpected error %v", err)
  43. continue
  44. case err != nil:
  45. continue
  46. }
  47. u.Path = path.Join(u.Path, versionedAPIPath)
  48. if e, a := testCase.URL, u.String(); e != a {
  49. t.Errorf("%d: expected host %s, got %s", i, e, a)
  50. continue
  51. }
  52. }
  53. }