k8s.io/apiserver@v0.31.1/pkg/endpoints/filters/warning_test.go (about) 1 /* 2 Copyright 2020 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 filters 18 19 import ( 20 "net/http/httptest" 21 "reflect" 22 "testing" 23 ) 24 25 func Test_recorder_AddWarning(t *testing.T) { 26 type args struct { 27 agent string 28 text string 29 } 30 tests := []struct { 31 name string 32 args []args 33 expect []string 34 }{ 35 { 36 name: "empty", 37 args: []args{}, 38 expect: nil, 39 }, 40 { 41 name: "empty values", 42 args: []args{{agent: "", text: ""}}, 43 expect: nil, 44 }, 45 { 46 name: "empty agent", 47 args: []args{{agent: "", text: "mytext"}}, 48 expect: []string{`299 - "mytext"`}, 49 }, 50 { 51 name: "simple", 52 args: []args{{agent: "myagent", text: "mytext"}}, 53 expect: []string{`299 myagent "mytext"`}, 54 }, 55 { 56 name: "duplicate text", 57 args: []args{ 58 {agent: "myagent", text: "mytext"}, 59 {agent: "myagent2", text: "mytext"}, 60 }, 61 expect: []string{`299 myagent "mytext"`}, 62 }, 63 { 64 name: "multiple", 65 args: []args{ 66 {agent: "", text: "mytext1"}, 67 {agent: "", text: "mytext2"}, 68 {agent: "", text: "mytext3"}, 69 }, 70 expect: []string{ 71 `299 - "mytext1"`, 72 `299 - "mytext2"`, 73 `299 - "mytext3"`, 74 }, 75 }, 76 77 { 78 name: "invalid agent", 79 args: []args{{agent: "my agent", text: "mytext"}}, 80 expect: nil, 81 }, 82 { 83 name: "invalid text", 84 args: []args{{agent: "myagent", text: "my\ntext"}}, 85 expect: nil, 86 }, 87 } 88 for _, tt := range tests { 89 t.Run(tt.name, func(t *testing.T) { 90 responseRecorder := httptest.NewRecorder() 91 warningRecorder := &recorder{writer: responseRecorder} 92 for _, arg := range tt.args { 93 warningRecorder.AddWarning(arg.agent, arg.text) 94 } 95 if !reflect.DeepEqual(tt.expect, responseRecorder.Header()["Warning"]) { 96 t.Errorf("expected\n%#v\ngot\n%#v", tt.expect, responseRecorder.Header()["Warning"]) 97 } 98 }) 99 } 100 } 101 102 func TestTruncation(t *testing.T) { 103 originalTotalRunes, originalItemRunes := truncateAtTotalRunes, truncateItemRunes 104 truncateAtTotalRunes, truncateItemRunes = 25, 5 105 defer func() { 106 truncateAtTotalRunes, truncateItemRunes = originalTotalRunes, originalItemRunes 107 }() 108 109 responseRecorder := httptest.NewRecorder() 110 warningRecorder := &recorder{writer: responseRecorder} 111 112 // add items longer than the individual length 113 warningRecorder.AddWarning("", "aaaaaaaaaa") // long item 114 warningRecorder.AddWarning("-", "aaaaaaaaaa") // duplicate item 115 warningRecorder.AddWarning("", "bb") // short item 116 warningRecorder.AddWarning("", "ccc") // short item 117 warningRecorder.AddWarning("", "Iñtërnâtiô") // long item 118 // check they are preserved 119 if e, a := []string{`299 - "aaaaaaaaaa"`, `299 - "bb"`, `299 - "ccc"`, `299 - "Iñtërnâtiô"`}, responseRecorder.Header()["Warning"]; !reflect.DeepEqual(e, a) { 120 t.Errorf("expected\n%#v\ngot\n%#v", e, a) 121 } 122 // add an item that exceeds the length and triggers truncation, reducing existing items to 15 runes 123 warningRecorder.AddWarning("", "e") // triggering item, 16 total 124 warningRecorder.AddWarning("", "ffffffffff") // long item to get truncated, 21 total 125 warningRecorder.AddWarning("", "ffffffffff") // duplicate item 126 warningRecorder.AddWarning("", "gggggggggg") // item to get truncated, 26 total 127 warningRecorder.AddWarning("", "h") // item to get ignored since we're over our limit 128 // check that existing items are truncated, and order preserved 129 if e, a := []string{`299 - "aaaaa"`, `299 - "bb"`, `299 - "ccc"`, `299 - "Iñtër"`, `299 - "e"`, `299 - "fffff"`, `299 - "ggggg"`}, responseRecorder.Header()["Warning"]; !reflect.DeepEqual(e, a) { 130 t.Errorf("expected\n%#v\ngot\n%#v", e, a) 131 } 132 }