k8s.io/apiserver@v0.31.1/pkg/endpoints/handlers/helpers_test.go (about) 1 /* 2 Copyright 2019 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 package handlers 18 19 import ( 20 "context" 21 "fmt" 22 "net/http" 23 "net/url" 24 "testing" 25 26 "github.com/stretchr/testify/assert" 27 "k8s.io/apiserver/pkg/endpoints/request" 28 ) 29 30 func TestLazyTruncatedUserAgent(t *testing.T) { 31 req := &http.Request{} 32 req.Header = http.Header{} 33 34 ua := "short-agent" 35 req.Header.Set("User-Agent", ua) 36 uaNotTruncated := &lazyTruncatedUserAgent{req} 37 assert.Equal(t, ua, fmt.Sprintf("%v", uaNotTruncated)) 38 39 ua = "" 40 for i := 0; i < maxUserAgentLength*2; i++ { 41 ua = ua + "a" 42 } 43 req.Header.Set("User-Agent", ua) 44 uaTruncated := &lazyTruncatedUserAgent{req} 45 assert.NotEqual(t, ua, fmt.Sprintf("%v", uaTruncated)) 46 47 usUnknown := &lazyTruncatedUserAgent{} 48 assert.Equal(t, "unknown", fmt.Sprintf("%v", usUnknown)) 49 } 50 51 func TestLazyClientIP(t *testing.T) { 52 req := &http.Request{} 53 req.Header = http.Header{} 54 55 ip := "127.0.0.1" 56 req.Header.Set("X-Forwarded-For", ip) 57 58 clientIPWithReq := &lazyClientIP{req} 59 assert.Equal(t, ip, fmt.Sprintf("%v", clientIPWithReq)) 60 61 clientIPWithoutReq := &lazyClientIP{} 62 assert.Equal(t, "unknown", fmt.Sprintf("%v", clientIPWithoutReq)) 63 } 64 65 func TestLazyAccept(t *testing.T) { 66 req := &http.Request{} 67 req.Header = http.Header{} 68 69 accept := "application/json" 70 req.Header.Set("Accept", accept) 71 72 acceptWithReq := &lazyAccept{req} 73 assert.Equal(t, accept, fmt.Sprintf("%v", acceptWithReq)) 74 75 acceptWithoutReq := &lazyAccept{} 76 assert.Equal(t, "unknown", fmt.Sprintf("%v", acceptWithoutReq)) 77 } 78 79 func TestLazyVerb(t *testing.T) { 80 assert.Equal(t, "unknown", fmt.Sprintf("%v", &lazyVerb{})) 81 82 u, _ := url.Parse("?watch=true") 83 req := &http.Request{Method: "GET", URL: u} 84 verbWithReq := &lazyVerb{req: req} 85 assert.Equal(t, "WATCH", fmt.Sprintf("%v", verbWithReq)) 86 } 87 88 func TestLazyApiGroup(t *testing.T) { 89 assert.Equal(t, "unknown", fmt.Sprintf("%v", &lazyAPIGroup{})) 90 91 scopeWithEmptyReq := &lazyAPIGroup{&http.Request{}} 92 assert.Equal(t, "unknown", fmt.Sprintf("%v", scopeWithEmptyReq)) 93 94 req := &http.Request{} 95 ctx := request.WithRequestInfo(context.TODO(), &request.RequestInfo{APIGroup: "apps"}) 96 scopeWithReq := &lazyAPIGroup{req: req.WithContext(ctx)} 97 assert.Equal(t, "apps", fmt.Sprintf("%v", scopeWithReq)) 98 } 99 100 func TestLazyApiVersion(t *testing.T) { 101 assert.Equal(t, "unknown", fmt.Sprintf("%v", &lazyAPIVersion{})) 102 103 scopeWithEmptyReq := &lazyAPIVersion{&http.Request{}} 104 assert.Equal(t, "unknown", fmt.Sprintf("%v", scopeWithEmptyReq)) 105 106 req := &http.Request{} 107 ctx := request.WithRequestInfo(context.TODO(), &request.RequestInfo{APIVersion: "v1"}) 108 scopeWithReq := &lazyAPIVersion{req: req.WithContext(ctx)} 109 assert.Equal(t, "v1", fmt.Sprintf("%v", scopeWithReq)) 110 } 111 112 func TestLazyName(t *testing.T) { 113 assert.Equal(t, "unknown", fmt.Sprintf("%v", &lazyName{})) 114 115 scopeWithEmptyReq := &lazyName{&http.Request{}} 116 assert.Equal(t, "unknown", fmt.Sprintf("%v", scopeWithEmptyReq)) 117 118 req := &http.Request{} 119 ctx := request.WithRequestInfo(context.TODO(), &request.RequestInfo{Name: "jaeger-76d45d6876-vqp8t"}) 120 scopeWithReq := &lazyName{req: req.WithContext(ctx)} 121 assert.Equal(t, "jaeger-76d45d6876-vqp8t", fmt.Sprintf("%v", scopeWithReq)) 122 } 123 124 func TestLazySubresource(t *testing.T) { 125 assert.Equal(t, "unknown", fmt.Sprintf("%v", &lazySubresource{})) 126 127 scopeWithEmptyReq := &lazySubresource{&http.Request{}} 128 assert.Equal(t, "unknown", fmt.Sprintf("%v", scopeWithEmptyReq)) 129 130 req := &http.Request{} 131 ctx := request.WithRequestInfo(context.TODO(), &request.RequestInfo{Subresource: "binding"}) 132 scopeWithReq := &lazySubresource{req: req.WithContext(ctx)} 133 assert.Equal(t, "binding", fmt.Sprintf("%v", scopeWithReq)) 134 } 135 136 func TestLazyNamespace(t *testing.T) { 137 assert.Equal(t, "unknown", fmt.Sprintf("%v", &lazyNamespace{})) 138 139 scopeWithEmptyReq := &lazyNamespace{&http.Request{}} 140 assert.Equal(t, "unknown", fmt.Sprintf("%v", scopeWithEmptyReq)) 141 142 req := &http.Request{} 143 ctx := request.WithRequestInfo(context.TODO(), &request.RequestInfo{Namespace: "jaeger"}) 144 scopeWithReq := &lazyNamespace{req: req.WithContext(ctx)} 145 assert.Equal(t, "jaeger", fmt.Sprintf("%v", scopeWithReq)) 146 } 147 148 func TestLazyResource(t *testing.T) { 149 assert.Equal(t, "unknown", fmt.Sprintf("%v", &lazyResource{})) 150 151 resourceWithEmptyReq := &lazyResource{&http.Request{}} 152 assert.Equal(t, "unknown", fmt.Sprintf("%v", resourceWithEmptyReq)) 153 154 req := &http.Request{} 155 ctx := request.WithRequestInfo(context.TODO(), &request.RequestInfo{Resource: "resource"}) 156 resourceWithReq := &lazyResource{req: req.WithContext(ctx)} 157 assert.Equal(t, "resource", fmt.Sprintf("%v", resourceWithReq)) 158 } 159 160 func TestLazyScope(t *testing.T) { 161 assert.Equal(t, "unknown", fmt.Sprintf("%v", &lazyScope{})) 162 163 scopeWithEmptyReq := &lazyScope{&http.Request{}} 164 assert.Equal(t, "unknown", fmt.Sprintf("%v", scopeWithEmptyReq)) 165 166 req := &http.Request{} 167 ctx := request.WithRequestInfo(context.TODO(), &request.RequestInfo{Namespace: "ns"}) 168 scopeWithReq := &lazyScope{req: req.WithContext(ctx)} 169 assert.Equal(t, "namespace", fmt.Sprintf("%v", scopeWithReq)) 170 }