github.com/micro/go-micro/v2@v2.9.1/util/kubernetes/api/api_test.go (about) 1 package api 2 3 import ( 4 "encoding/json" 5 "net/http" 6 "net/http/httptest" 7 "reflect" 8 "testing" 9 ) 10 11 type testcase struct { 12 Token string 13 ReqFn func(opts *Options) *Request 14 Method string 15 URI string 16 Body interface{} 17 Header map[string]string 18 Assert func(req *http.Request) bool 19 } 20 21 type assertFn func(req *http.Request) bool 22 23 var tests = []testcase{ 24 testcase{ 25 ReqFn: func(opts *Options) *Request { 26 return NewRequest(opts).Get().Resource("service") 27 }, 28 Method: "GET", 29 URI: "/api/v1/namespaces/default/services/", 30 }, 31 testcase{ 32 ReqFn: func(opts *Options) *Request { 33 return NewRequest(opts).Get().Resource("service").Name("foo") 34 }, 35 Method: "GET", 36 URI: "/api/v1/namespaces/default/services/foo", 37 }, 38 testcase{ 39 ReqFn: func(opts *Options) *Request { 40 return NewRequest(opts).Get().Resource("service").Namespace("test").Name("bar") 41 }, 42 Method: "GET", 43 URI: "/api/v1/namespaces/test/services/bar", 44 }, 45 testcase{ 46 ReqFn: func(opts *Options) *Request { 47 return NewRequest(opts).Get().Resource("deployment").Name("foo") 48 }, 49 Method: "GET", 50 URI: "/apis/apps/v1/namespaces/default/deployments/foo", 51 }, 52 testcase{ 53 ReqFn: func(opts *Options) *Request { 54 return NewRequest(opts).Get().Resource("deployment").Namespace("test").Name("foo") 55 }, 56 Method: "GET", 57 URI: "/apis/apps/v1/namespaces/test/deployments/foo", 58 }, 59 testcase{ 60 ReqFn: func(opts *Options) *Request { 61 return NewRequest(opts).Get().Resource("pod").Params(&Params{LabelSelector: map[string]string{"foo": "bar"}}) 62 }, 63 Method: "GET", 64 URI: "/api/v1/namespaces/default/pods/?labelSelector=foo%3Dbar", 65 }, 66 testcase{ 67 ReqFn: func(opts *Options) *Request { 68 return NewRequest(opts).Post().Resource("service").Name("foo").Body(map[string]string{"foo": "bar"}) 69 }, 70 Method: "POST", 71 URI: "/api/v1/namespaces/default/services/foo", 72 Body: map[string]string{"foo": "bar"}, 73 }, 74 testcase{ 75 ReqFn: func(opts *Options) *Request { 76 return NewRequest(opts).Post().Resource("deployment").Namespace("test").Name("foo").Body(map[string]string{"foo": "bar"}) 77 }, 78 Method: "POST", 79 URI: "/apis/apps/v1/namespaces/test/deployments/foo", 80 Body: map[string]string{"foo": "bar"}, 81 }, 82 testcase{ 83 ReqFn: func(opts *Options) *Request { 84 return NewRequest(opts).Put().Resource("endpoint").Name("baz").Body(map[string]string{"bam": "bar"}) 85 }, 86 Method: "PUT", 87 URI: "/api/v1/namespaces/default/endpoints/baz", 88 Body: map[string]string{"bam": "bar"}, 89 }, 90 testcase{ 91 ReqFn: func(opts *Options) *Request { 92 return NewRequest(opts).Patch().Resource("endpoint").Name("baz").Body(map[string]string{"bam": "bar"}) 93 }, 94 Method: "PATCH", 95 URI: "/api/v1/namespaces/default/endpoints/baz", 96 Body: map[string]string{"bam": "bar"}, 97 }, 98 testcase{ 99 ReqFn: func(opts *Options) *Request { 100 return NewRequest(opts).Patch().Resource("endpoint").Name("baz").SetHeader("foo", "bar") 101 }, 102 Method: "PATCH", 103 URI: "/api/v1/namespaces/default/endpoints/baz", 104 Header: map[string]string{"foo": "bar"}, 105 }, 106 testcase{ 107 ReqFn: func(opts *Options) *Request { 108 return NewRequest(opts).Patch().Resource("deployment").Name("baz").SetHeader("foo", "bar") 109 }, 110 Method: "PATCH", 111 URI: "/apis/apps/v1/namespaces/default/deployments/baz", 112 Header: map[string]string{"foo": "bar"}, 113 }, 114 testcase{ 115 ReqFn: func(opts *Options) *Request { 116 return NewRequest(opts). 117 Get(). 118 Resource("pod"). 119 SubResource("log"). 120 Name("foolog") 121 }, 122 Method: "GET", 123 URI: "/api/v1/namespaces/default/pods/foolog/log", 124 }, 125 } 126 127 var wrappedHandler = func(test *testcase, t *testing.T) http.HandlerFunc { 128 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 129 auth := r.Header.Get("Authorization") 130 if len(test.Token) > 0 && (len(auth) == 0 || auth != "Bearer "+test.Token) { 131 t.Errorf("test case token (%s) did not match expected token (%s)", "Bearer "+test.Token, auth) 132 } 133 134 if len(test.Method) > 0 && test.Method != r.Method { 135 t.Errorf("test case Method (%s) did not match expected Method (%s)", test.Method, r.Method) 136 } 137 138 if len(test.URI) > 0 && test.URI != r.URL.RequestURI() { 139 t.Errorf("test case URI (%s) did not match expected URI (%s)", test.URI, r.URL.RequestURI()) 140 } 141 142 if test.Body != nil { 143 var res map[string]string 144 decoder := json.NewDecoder(r.Body) 145 if err := decoder.Decode(&res); err != nil { 146 t.Errorf("decoding body failed: %v", err) 147 } 148 if !reflect.DeepEqual(res, test.Body) { 149 t.Error("body did not match") 150 } 151 } 152 153 if test.Header != nil { 154 for k, v := range test.Header { 155 if r.Header.Get(k) != v { 156 t.Error("header did not exist") 157 } 158 } 159 } 160 161 w.WriteHeader(http.StatusOK) 162 }) 163 } 164 165 func TestRequest(t *testing.T) { 166 for _, test := range tests { 167 ts := httptest.NewServer(wrappedHandler(&test, t)) 168 req := test.ReqFn(&Options{ 169 Host: ts.URL, 170 Client: &http.Client{}, 171 BearerToken: &test.Token, 172 Namespace: "default", 173 }) 174 res := req.Do() 175 if res.Error() != nil { 176 t.Errorf("request failed with %v", res.Error()) 177 } 178 ts.Close() 179 } 180 }