k8s.io/client-go@v0.31.1/rest/fake/fake.go (about) 1 /* 2 Copyright 2014 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 // This is made a separate package and should only be imported by tests, because 18 // it imports testapi 19 package fake 20 21 import ( 22 "net/http" 23 "net/url" 24 25 "k8s.io/apimachinery/pkg/runtime" 26 "k8s.io/apimachinery/pkg/runtime/schema" 27 "k8s.io/apimachinery/pkg/types" 28 restclient "k8s.io/client-go/rest" 29 "k8s.io/client-go/util/flowcontrol" 30 ) 31 32 // CreateHTTPClient creates an http.Client that will invoke the provided roundTripper func 33 // when a request is made. 34 func CreateHTTPClient(roundTripper func(*http.Request) (*http.Response, error)) *http.Client { 35 return &http.Client{ 36 Transport: roundTripperFunc(roundTripper), 37 } 38 } 39 40 type roundTripperFunc func(*http.Request) (*http.Response, error) 41 42 func (f roundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error) { 43 return f(req) 44 } 45 46 // RESTClient provides a fake RESTClient interface. It is used to mock network 47 // interactions via a rest.Request, or to make them via the provided Client to 48 // a specific server. 49 type RESTClient struct { 50 NegotiatedSerializer runtime.NegotiatedSerializer 51 GroupVersion schema.GroupVersion 52 VersionedAPIPath string 53 54 // Err is returned when any request would be made to the server. If Err is set, 55 // Req will not be recorded, Resp will not be returned, and Client will not be 56 // invoked. 57 Err error 58 // Req is set to the last request that was executed (had the methods Do/DoRaw) invoked. 59 Req *http.Request 60 // If Client is specified, the client will be invoked instead of returning Resp if 61 // Err is not set. 62 Client *http.Client 63 // Resp is returned to the caller after Req is recorded, unless Err or Client are set. 64 Resp *http.Response 65 } 66 67 func (c *RESTClient) Get() *restclient.Request { 68 return c.Verb("GET") 69 } 70 71 func (c *RESTClient) Put() *restclient.Request { 72 return c.Verb("PUT") 73 } 74 75 func (c *RESTClient) Patch(pt types.PatchType) *restclient.Request { 76 return c.Verb("PATCH").SetHeader("Content-Type", string(pt)) 77 } 78 79 func (c *RESTClient) Post() *restclient.Request { 80 return c.Verb("POST") 81 } 82 83 func (c *RESTClient) Delete() *restclient.Request { 84 return c.Verb("DELETE") 85 } 86 87 func (c *RESTClient) Verb(verb string) *restclient.Request { 88 return c.Request().Verb(verb) 89 } 90 91 func (c *RESTClient) APIVersion() schema.GroupVersion { 92 return c.GroupVersion 93 } 94 95 func (c *RESTClient) GetRateLimiter() flowcontrol.RateLimiter { 96 return nil 97 } 98 99 func (c *RESTClient) Request() *restclient.Request { 100 config := restclient.ClientContentConfig{ 101 ContentType: runtime.ContentTypeJSON, 102 GroupVersion: c.GroupVersion, 103 Negotiator: runtime.NewClientNegotiator(c.NegotiatedSerializer, c.GroupVersion), 104 } 105 return restclient.NewRequestWithClient(&url.URL{Scheme: "https", Host: "localhost"}, c.VersionedAPIPath, config, CreateHTTPClient(c.do)) 106 } 107 108 // do is invoked when a Request() created by this client is executed. 109 func (c *RESTClient) do(req *http.Request) (*http.Response, error) { 110 if c.Err != nil { 111 return nil, c.Err 112 } 113 c.Req = req 114 if c.Client != nil { 115 return c.Client.Do(req) 116 } 117 return c.Resp, nil 118 }