Skip to content

Commit 665a8df

Browse files
Merge pull request #128343 from Jefftree/responsewriter-test
Add tests for InMemoryResponseWriter Kubernetes-commit: c644f4d18506fb4055858646a01ec8a00802d97e
2 parents b62222b + ada3ee1 commit 665a8df

File tree

3 files changed

+72
-9
lines changed

3 files changed

+72
-9
lines changed

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ require (
5252
gopkg.in/evanphx/json-patch.v4 v4.12.0
5353
gopkg.in/natefinch/lumberjack.v2 v2.2.1
5454
gopkg.in/square/go-jose.v2 v2.6.0
55-
k8s.io/api v0.0.0-20241213094710-075bd98ca2a4
56-
k8s.io/apimachinery v0.0.0-20241213094412-7249ce11e455
57-
k8s.io/client-go v0.0.0-20241213095137-633c24cce509
55+
k8s.io/api v0.0.0-20241213174810-855f46b651ee
56+
k8s.io/apimachinery v0.0.0-20241213174434-cfa44a126e90
57+
k8s.io/client-go v0.0.0-20241213175236-ab443a50c620
5858
k8s.io/component-base v0.0.0-20241213100147-3ee150d133b9
5959
k8s.io/klog/v2 v2.130.1
6060
k8s.io/kms v0.0.0-20241213100418-8cb606989fcf

go.sum

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -365,12 +365,12 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
365365
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
366366
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
367367
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
368-
k8s.io/api v0.0.0-20241213094710-075bd98ca2a4 h1:1cW7Ic/WXRaAbFDAbmtra9C7d2UUt5P127WBG8ijsOE=
369-
k8s.io/api v0.0.0-20241213094710-075bd98ca2a4/go.mod h1:bgLmB9gxsff4S1tB//F/RWzcdq5Ti4YA78/sDVrSP3o=
370-
k8s.io/apimachinery v0.0.0-20241213094412-7249ce11e455 h1:EpmRoNKEKpdJnnmcRdywZxc/8mQkpvtEqxfQMwjlr3k=
371-
k8s.io/apimachinery v0.0.0-20241213094412-7249ce11e455/go.mod h1:1Ya4EscxtjDT7Rem+4gvp/C/+xs8jpJtJv7fyH0+Gzo=
372-
k8s.io/client-go v0.0.0-20241213095137-633c24cce509 h1:wKYr6pM7XGp2HtSXAr1gxOhAvsP6lr2vYFhiBXDYSic=
373-
k8s.io/client-go v0.0.0-20241213095137-633c24cce509/go.mod h1:QY2yB9dGvBwA/Ym/Iooz87wZX37xJnGOuNqp0gXRzUY=
368+
k8s.io/api v0.0.0-20241213174810-855f46b651ee h1:WWGrsu1oX+PIojnolAoB8/aLmnUU0MUv/wV+AgKZ8Cc=
369+
k8s.io/api v0.0.0-20241213174810-855f46b651ee/go.mod h1:qVRanebSXI0iPj2gXDP64njA69rwc1lGJAUC+IaX24I=
370+
k8s.io/apimachinery v0.0.0-20241213174434-cfa44a126e90 h1:lovn4RlPSJolbPdyMjl56Sj9UpWoQJmONUCkrf5kPXg=
371+
k8s.io/apimachinery v0.0.0-20241213174434-cfa44a126e90/go.mod h1:KDPQAeziTN7qt/3bXgtoWbjSIrXXrP//t0oWa0iC+fw=
372+
k8s.io/client-go v0.0.0-20241213175236-ab443a50c620 h1:kKDIdcMNuWxQHPo1LDFgCWnHGq7kguNmDFA1Y3ofbU4=
373+
k8s.io/client-go v0.0.0-20241213175236-ab443a50c620/go.mod h1:Hn6xwSLyA2O3o+qEdlksZeAqu99xQTIOm3m5R+4o1xw=
374374
k8s.io/component-base v0.0.0-20241213100147-3ee150d133b9 h1:kPYBW7qMrUsfngiC1fJnzGfUWjf9PHVqvgJWoGjOKF8=
375375
k8s.io/component-base v0.0.0-20241213100147-3ee150d133b9/go.mod h1:4v+s3aKlLBNO8dXYFNyOuemZPXNadHd8P6n6UyhwGs4=
376376
k8s.io/klog/v2 v2.130.1 h1:n9Xl7H1Xvksem4KFG4PYbdQCQxqc/tTUyrgXaOhHSzk=
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
Copyright 2024 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 responsewriter
18+
19+
import (
20+
"net/http"
21+
"testing"
22+
)
23+
24+
func TestInMemoryResponseWriter(t *testing.T) {
25+
w := NewInMemoryResponseWriter()
26+
27+
h := w.Header()
28+
h.Set("Content-Type", "application/json")
29+
30+
w.WriteHeader(http.StatusCreated)
31+
32+
_, err := w.Write([]byte(`{"message": "hello"}`))
33+
if err != nil {
34+
t.Errorf("Write() returned an error: %v", err)
35+
}
36+
37+
if w.RespCode() != http.StatusCreated {
38+
t.Errorf("RespCode() returned unexpected code: %d, want %d", w.RespCode(), http.StatusCreated)
39+
}
40+
41+
if string(w.Data()) != `{"message": "hello"}` {
42+
t.Errorf("Data() returned unexpected body: %s, want %s", string(w.Data()), `{"message": "hello"}`)
43+
}
44+
45+
expectedString := "ResponseCode: 201, Body: {\"message\": \"hello\"}, Header: map[Content-Type:[application/json]]"
46+
if w.String() != expectedString {
47+
t.Errorf("String() returned unexpected output: %s, want %s", w.String(), expectedString)
48+
}
49+
}
50+
51+
func TestInMemoryResponseWriter_DefaultHeader(t *testing.T) {
52+
w := NewInMemoryResponseWriter()
53+
54+
_, err := w.Write([]byte(`{"message": "hello"}`))
55+
if err != nil {
56+
t.Errorf("Write() returned an error: %v", err)
57+
}
58+
59+
// should be StatusOK (200) by default
60+
if w.RespCode() != http.StatusOK {
61+
t.Errorf("RespCode() returned unexpected code: %d, want %d", w.RespCode(), http.StatusOK)
62+
}
63+
}

0 commit comments

Comments
 (0)