1
16
17 package rest
18
19 import (
20 "path"
21 "testing"
22
23 "k8s.io/api/core/v1"
24 )
25
26 func TestValidatesHostParameter(t *testing.T) {
27 testCases := []struct {
28 Host string
29 APIPath string
30
31 URL string
32 Err bool
33 }{
34 {"127.0.0.1", "", "http://127.0.0.1/" + v1.SchemeGroupVersion.Version, false},
35 {"127.0.0.1:8080", "", "http://127.0.0.1:8080/" + v1.SchemeGroupVersion.Version, false},
36 {"foo.bar.com", "", "http://foo.bar.com/" + v1.SchemeGroupVersion.Version, false},
37 {"http://host/prefix", "", "http://host/prefix/" + v1.SchemeGroupVersion.Version, false},
38 {"http://host", "", "http://host/" + v1.SchemeGroupVersion.Version, false},
39 {"http://host", "/", "http://host/" + v1.SchemeGroupVersion.Version, false},
40 {"http://host", "/other", "http://host/other/" + v1.SchemeGroupVersion.Version, false},
41 {"host/server", "", "", true},
42 }
43 for i, testCase := range testCases {
44 u, versionedAPIPath, err := DefaultServerURL(testCase.Host, testCase.APIPath, v1.SchemeGroupVersion, false)
45 switch {
46 case err == nil && testCase.Err:
47 t.Errorf("expected error but was nil")
48 continue
49 case err != nil && !testCase.Err:
50 t.Errorf("unexpected error %v", err)
51 continue
52 case err != nil:
53 continue
54 }
55 u.Path = path.Join(u.Path, versionedAPIPath)
56 if e, a := testCase.URL, u.String(); e != a {
57 t.Errorf("%d: expected host %s, got %s", i, e, a)
58 continue
59 }
60 }
61 }
62
View as plain text