k8s.io/apiserver@v0.31.1/pkg/endpoints/discovery/aggregated/wrapper_test.go (about)

     1  /*
     2  Copyright 2022 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 aggregated
    18  
    19  import (
    20  	"net/http"
    21  	"net/http/httptest"
    22  
    23  	"io"
    24  	"testing"
    25  
    26  	"github.com/stretchr/testify/assert"
    27  	genericfeatures "k8s.io/apiserver/pkg/features"
    28  	utilfeature "k8s.io/apiserver/pkg/util/feature"
    29  	featuregatetesting "k8s.io/component-base/featuregate/testing"
    30  )
    31  
    32  const discoveryPath = "/apis"
    33  const jsonAccept = "application/json"
    34  const protobufAccept = "application/vnd.kubernetes.protobuf"
    35  const aggregatedV2Beta1AcceptSuffix = ";g=apidiscovery.k8s.io;v=v2beta1;as=APIGroupDiscoveryList"
    36  const aggregatedAcceptSuffix = ";g=apidiscovery.k8s.io;v=v2;as=APIGroupDiscoveryList"
    37  
    38  const aggregatedV2Beta1JSONAccept = jsonAccept + aggregatedV2Beta1AcceptSuffix
    39  const aggregatedV2Beta1ProtoAccept = protobufAccept + aggregatedV2Beta1AcceptSuffix
    40  const aggregatedJSONAccept = jsonAccept + aggregatedAcceptSuffix
    41  const aggregatedProtoAccept = protobufAccept + aggregatedAcceptSuffix
    42  
    43  func fetchPath(handler http.Handler, path, accept string) string {
    44  	w := httptest.NewRecorder()
    45  	req := httptest.NewRequest("GET", discoveryPath, nil)
    46  
    47  	// Ask for JSON response
    48  	req.Header.Set("Accept", accept)
    49  
    50  	handler.ServeHTTP(w, req)
    51  	return string(w.Body.Bytes())
    52  }
    53  
    54  type fakeHTTPHandler struct {
    55  	data string
    56  }
    57  
    58  func (f fakeHTTPHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
    59  	io.WriteString(resp, f.data)
    60  }
    61  
    62  func TestAggregationEnabled(t *testing.T) {
    63  	featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, genericfeatures.AggregatedDiscoveryEndpoint, true)
    64  
    65  	unaggregated := fakeHTTPHandler{data: "unaggregated"}
    66  	aggregated := fakeHTTPHandler{data: "aggregated"}
    67  	wrapped := WrapAggregatedDiscoveryToHandler(unaggregated, aggregated)
    68  
    69  	testCases := []struct {
    70  		accept   string
    71  		expected string
    72  	}{
    73  		{
    74  			// Misconstructed/incorrect accept headers should be passed to the unaggregated handler to return an error
    75  			accept:   "application/json;foo=bar",
    76  			expected: "unaggregated",
    77  		}, {
    78  			// Empty accept headers are valid and should be handled by the unaggregated handler
    79  			accept:   "",
    80  			expected: "unaggregated",
    81  		}, {
    82  			accept:   aggregatedV2Beta1JSONAccept,
    83  			expected: "aggregated",
    84  		}, {
    85  			accept:   aggregatedV2Beta1ProtoAccept,
    86  			expected: "aggregated",
    87  		}, {
    88  			accept:   aggregatedJSONAccept,
    89  			expected: "aggregated",
    90  		}, {
    91  			accept:   aggregatedProtoAccept,
    92  			expected: "aggregated",
    93  		}, {
    94  			accept:   jsonAccept,
    95  			expected: "unaggregated",
    96  		}, {
    97  			accept:   protobufAccept,
    98  			expected: "unaggregated",
    99  		}, {
   100  			// Server should return the first accepted type
   101  			accept:   aggregatedJSONAccept + "," + jsonAccept,
   102  			expected: "aggregated",
   103  		}, {
   104  			// Server should return the first accepted type
   105  			accept:   aggregatedProtoAccept + "," + protobufAccept,
   106  			expected: "aggregated",
   107  		},
   108  	}
   109  
   110  	for _, tc := range testCases {
   111  		body := fetchPath(wrapped, discoveryPath, tc.accept)
   112  		assert.Equal(t, tc.expected, body)
   113  	}
   114  }