Skip to content

Commit 12c9f00

Browse files
authored
Adding a simpler interface for the HTTP request library. (#8862)
1 parent 79a311d commit 12c9f00

File tree

18 files changed

+954
-102
lines changed

18 files changed

+954
-102
lines changed

go.mod

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ require (
66
github.com/armon/go-proxyproto v0.0.0-20210323213023-7e956b284f0a
77
github.com/eapache/channels v1.1.0
88
github.com/fsnotify/fsnotify v1.5.4
9-
github.com/gavv/httpexpect/v2 v2.3.1
109
github.com/imdario/mergo v0.3.13
1110
github.com/json-iterator/go v1.1.12
1211
github.com/kylelemons/godebug v1.1.0
@@ -24,6 +23,7 @@ require (
2423
github.com/spf13/cobra v1.5.0
2524
github.com/spf13/pflag v1.0.5
2625
github.com/stretchr/testify v1.8.0
26+
github.com/yudai/gojsondiff v1.0.0
2727
github.com/zakjan/cert-chain-resolver v0.0.0-20211122211144-c6b0b792af9a
2828
golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd
2929
golang.org/x/net v0.0.0-20220722155237-a158d28d115b
@@ -55,8 +55,6 @@ require (
5555
github.com/BurntSushi/toml v0.3.1 // indirect
5656
github.com/PuerkitoBio/purell v1.1.1 // indirect
5757
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
58-
github.com/ajg/form v1.5.1 // indirect
59-
github.com/andybalholm/brotli v1.0.4 // indirect
6058
github.com/beorn7/perks v1.0.1 // indirect
6159
github.com/blang/semver/v4 v4.0.0 // indirect
6260
github.com/cespare/xxhash/v2 v2.1.2 // indirect
@@ -66,13 +64,13 @@ require (
6664
github.com/eapache/queue v1.1.0 // indirect
6765
github.com/emicklei/go-restful/v3 v3.8.0 // indirect
6866
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
69-
github.com/fatih/structs v1.0.0 // indirect
7067
github.com/fullsailor/pkcs7 v0.0.0-20160414161337-2585af45975b // indirect
7168
github.com/go-errors/errors v1.0.1 // indirect
7269
github.com/go-logr/logr v1.2.3 // indirect
7370
github.com/go-openapi/jsonpointer v0.19.5 // indirect
7471
github.com/go-openapi/jsonreference v0.19.5 // indirect
7572
github.com/go-openapi/swag v0.19.14 // indirect
73+
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect
7674
github.com/godbus/dbus/v5 v5.0.6 // indirect
7775
github.com/gogo/protobuf v1.3.2 // indirect
7876
github.com/golang-jwt/jwt/v4 v4.2.0 // indirect
@@ -82,16 +80,13 @@ require (
8280
github.com/google/btree v1.0.1 // indirect
8381
github.com/google/gnostic v0.5.7-v3refs // indirect
8482
github.com/google/go-cmp v0.5.8 // indirect
85-
github.com/google/go-querystring v1.0.0 // indirect
8683
github.com/google/gofuzz v1.1.0 // indirect
84+
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 // indirect
8785
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
8886
github.com/google/uuid v1.2.0 // indirect
89-
github.com/gorilla/websocket v1.4.2 // indirect
9087
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
91-
github.com/imkira/go-interpol v1.0.0 // indirect
9288
github.com/inconshreveable/mousetrap v1.0.0 // indirect
9389
github.com/josharian/intern v1.0.0 // indirect
94-
github.com/klauspost/compress v1.15.7 // indirect
9590
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
9691
github.com/mailru/easyjson v0.7.6 // indirect
9792
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
@@ -108,15 +103,9 @@ require (
108103
github.com/prometheus/procfs v0.8.0 // indirect
109104
github.com/sergi/go-diff v1.1.0 // indirect
110105
github.com/sirupsen/logrus v1.8.1 // indirect
111-
github.com/valyala/bytebufferpool v1.0.0 // indirect
112-
github.com/valyala/fasthttp v1.38.0 // indirect
113-
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
114-
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
115-
github.com/xeipuuv/gojsonschema v1.1.0 // indirect
116106
github.com/xlab/treeprint v1.1.0 // indirect
117-
github.com/yalp/jsonpath v0.0.0-20180802001716-5cc68e5049a0 // indirect
118-
github.com/yudai/gojsondiff v1.0.0 // indirect
119107
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect
108+
github.com/yudai/pp v2.0.1+incompatible // indirect
120109
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
121110
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
122111
golang.org/x/oauth2 v0.0.0-20220223155221-ee480838109b // indirect
@@ -135,7 +124,6 @@ require (
135124
k8s.io/gengo v0.0.0-20210813121822-485abfe95c7c // indirect
136125
k8s.io/kube-openapi v0.0.0-20220803162953-67bda5d908f1 // indirect
137126
k8s.io/utils v0.0.0-20220728103510-ee6ede2d64ed // indirect
138-
moul.io/http2curl v1.0.1-0.20190925090545-5cd742060b0e // indirect
139127
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
140128
sigs.k8s.io/kustomize/api v0.12.1 // indirect
141129
sigs.k8s.io/kustomize/kyaml v0.13.9 // indirect

go.sum

Lines changed: 5 additions & 51 deletions
Large diffs are not rendered by default.

test/e2e/annotations/affinitymode.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ var _ = framework.DescribeAnnotation("affinitymode", func() {
4242
host := "affinity-mode-balance.com"
4343
annotations := make(map[string]string)
4444
annotations["nginx.ingress.kubernetes.io/affinity"] = "cookie"
45-
annotations["ginx.ingress.kubernetes.io/session-cookie-name"] = "hello-cookie"
45+
annotations["nginx.ingress.kubernetes.io/session-cookie-name"] = "hello-cookie"
4646
annotations["nginx.ingress.kubernetes.io/session-cookie-expires"] = "172800"
4747
annotations["nginx.ingress.kubernetes.io/session-cookie-max-age"] = "172800"
4848
annotations["nginx.ingress.kubernetes.io/ssl-redirect"] = "false"
@@ -75,7 +75,7 @@ var _ = framework.DescribeAnnotation("affinitymode", func() {
7575
host := "affinity-mode-persistent.com"
7676
annotations := make(map[string]string)
7777
annotations["nginx.ingress.kubernetes.io/affinity"] = "cookie"
78-
annotations["ginx.ingress.kubernetes.io/session-cookie-name"] = "hello-cookie"
78+
annotations["nginx.ingress.kubernetes.io/session-cookie-name"] = "hello-cookie"
7979
annotations["nginx.ingress.kubernetes.io/session-cookie-expires"] = "172800"
8080
annotations["nginx.ingress.kubernetes.io/session-cookie-max-age"] = "172800"
8181
annotations["nginx.ingress.kubernetes.io/ssl-redirect"] = "false"

test/e2e/defaultbackend/default_backend.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ package defaultbackend
1919
import (
2020
"net/http"
2121

22-
"github.com/gavv/httpexpect/v2"
22+
"k8s.io/ingress-nginx/test/e2e/framework/httpexpect"
23+
2324
"github.com/onsi/ginkgo/v2"
2425
"github.com/stretchr/testify/assert"
2526

@@ -64,15 +65,13 @@ var _ = framework.IngressNginxDescribe("[Default Backend]", func() {
6465
for _, test := range testCases {
6566
ginkgo.By(test.Name)
6667

67-
var req *httpexpect.Request
68+
var req *httpexpect.HTTPRequest
6869

6970
switch test.Scheme {
7071
case framework.HTTP:
71-
req = f.HTTPTestClient().Request(test.Method, test.Path)
72-
req.WithURL(f.GetURL(framework.HTTP) + test.Path)
72+
req = f.HTTPTestClient().DoRequest(test.Method, test.Path).WithURL(f.GetURL(framework.HTTP) + test.Path)
7373
case framework.HTTPS:
74-
req = f.HTTPTestClient().Request(test.Method, test.Path)
75-
req.WithURL(f.GetURL(framework.HTTPS) + test.Path)
74+
req = f.HTTPTestClient().DoRequest(test.Method, test.Path).WithURL(f.GetURL(framework.HTTPS) + test.Path)
7675
default:
7776
ginkgo.Fail("Unexpected request scheme")
7877
}

test/e2e/framework/framework.go

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ import (
2222
"strings"
2323
"time"
2424

25-
"github.com/gavv/httpexpect/v2"
25+
"k8s.io/ingress-nginx/test/e2e/framework/httpexpect"
26+
2627
"github.com/onsi/ginkgo/v2"
2728
"github.com/stretchr/testify/assert"
2829
appsv1 "k8s.io/api/apps/v1"
@@ -445,22 +446,22 @@ func (f *Framework) DeleteNGINXPod(grace int64) {
445446
}
446447

447448
// HTTPDumbTestClient returns a new httpexpect client without BaseURL.
448-
func (f *Framework) HTTPDumbTestClient() *httpexpect.Expect {
449-
return f.newTestClient(nil, false)
449+
func (f *Framework) HTTPDumbTestClient() *httpexpect.HTTPRequest {
450+
return f.newHTTPTestClient(nil, false)
450451
}
451452

452-
// HTTPTestClient returns a new httpexpect client for end-to-end HTTP testing.
453-
func (f *Framework) HTTPTestClient() *httpexpect.Expect {
454-
return f.newTestClient(nil, true)
453+
// HTTPTestClient returns a new HTTPRequest client for end-to-end HTTP testing.
454+
func (f *Framework) HTTPTestClient() *httpexpect.HTTPRequest {
455+
return f.newHTTPTestClient(nil, true)
455456
}
456457

457458
// HTTPTestClientWithTLSConfig returns a new httpexpect client for end-to-end
458459
// HTTP testing with a custom TLS configuration.
459-
func (f *Framework) HTTPTestClientWithTLSConfig(config *tls.Config) *httpexpect.Expect {
460-
return f.newTestClient(config, true)
460+
func (f *Framework) HTTPTestClientWithTLSConfig(config *tls.Config) *httpexpect.HTTPRequest {
461+
return f.newHTTPTestClient(config, true)
461462
}
462463

463-
func (f *Framework) newTestClient(config *tls.Config, setIngressURL bool) *httpexpect.Expect {
464+
func (f *Framework) newHTTPTestClient(config *tls.Config, setIngressURL bool) *httpexpect.HTTPRequest {
464465
if config == nil {
465466
config = &tls.Config{
466467
InsecureSkipVerify: true,
@@ -471,24 +472,14 @@ func (f *Framework) newTestClient(config *tls.Config, setIngressURL bool) *httpe
471472
baseURL = f.GetURL(HTTP)
472473
}
473474

474-
return httpexpect.WithConfig(httpexpect.Config{
475-
BaseURL: baseURL,
476-
Client: &http.Client{
477-
Transport: &http.Transport{
478-
TLSClientConfig: config,
479-
},
480-
CheckRedirect: func(req *http.Request, via []*http.Request) error {
481-
return http.ErrUseLastResponse
482-
},
475+
return httpexpect.NewRequest(baseURL, &http.Client{
476+
Transport: &http.Transport{
477+
TLSClientConfig: config,
483478
},
484-
Reporter: httpexpect.NewAssertReporter(
485-
httpexpect.NewAssertReporter(ginkgo.GinkgoT()),
486-
),
487-
Printers: []httpexpect.Printer{
488-
// TODO: enable conditionally?
489-
// httpexpect.NewDebugPrinter(ginkgo.GinkgoT(), false),
479+
CheckRedirect: func(req *http.Request, via []*http.Request) error {
480+
return http.ErrUseLastResponse
490481
},
491-
})
482+
}, httpexpect.NewAssertReporter())
492483
}
493484

494485
// WaitForNginxListening waits until NGINX starts accepting connections on a port
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This module is based in the deprecated library `github.com/gavv/httpexpect`, and contains slightly adaptations.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 httpexpect
18+
19+
// Array provides methods to inspect attached []interface{} object
20+
// (Go representation of JSON array).
21+
type Array struct {
22+
chain chain
23+
value []interface{}
24+
}
25+
26+
// Iter returns a new slice of Values attached to array elements.
27+
func (a *Array) Iter() []Value {
28+
if a.chain.failed() {
29+
return []Value{}
30+
}
31+
ret := []Value{}
32+
for n := range a.value {
33+
ret = append(ret, Value{a.chain, a.value[n]})
34+
}
35+
return ret
36+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 httpexpect
18+
19+
type chain struct {
20+
reporter Reporter
21+
failbit bool
22+
}
23+
24+
func makeChain(reporter Reporter) chain {
25+
return chain{reporter, false}
26+
}
27+
28+
func (c *chain) failed() bool {
29+
return c.failbit
30+
}
31+
32+
func (c *chain) fail(message string, args ...interface{}) {
33+
if c.failbit {
34+
return
35+
}
36+
c.failbit = true
37+
c.reporter.Errorf(message, args...)
38+
}
39+
40+
func (c *chain) reset() {
41+
c.failbit = false
42+
}
43+
44+
func (c *chain) assertFailed(r Reporter) {
45+
if !c.failbit {
46+
r.Errorf("expected chain is failed, but it's ok")
47+
}
48+
}
49+
50+
func (c *chain) assertOK(r Reporter) {
51+
if c.failbit {
52+
r.Errorf("expected chain is ok, but it's failed")
53+
}
54+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 httpexpect
18+
19+
import "net/http"
20+
21+
// Cookie provides methods to inspect attached http.Cookie value.
22+
type Cookie struct {
23+
chain chain
24+
value *http.Cookie
25+
}
26+
27+
func (c *Cookie) Raw() *http.Cookie {
28+
return c.value
29+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 httpexpect
18+
19+
// Match provides methods to inspect attached regexp match results.
20+
type Match struct {
21+
chain chain
22+
submatches []string
23+
names map[string]int
24+
}
25+
26+
func makeMatch(chain chain, submatches []string, names []string) *Match {
27+
if submatches == nil {
28+
submatches = []string{}
29+
}
30+
namemap := map[string]int{}
31+
for n, name := range names {
32+
if name != "" {
33+
namemap[name] = n
34+
}
35+
}
36+
return &Match{chain, submatches, namemap}
37+
}

0 commit comments

Comments
 (0)