7
. "github.com/onsi/ginkgo/v2"
8
. "github.com/onsi/gomega"
10
util_http "github.com/kumahq/kuma/pkg/util/http"
13
var _ = Describe("Http Util", func() {
14
Describe("ClientWithBaseURL(..)", func() {
15
type testCase struct {
21
DescribeTable("should rewrite request URL by combining `baseURL` and `requestURL`",
22
func(given testCase) {
24
baseURL, err := url.Parse(given.baseURL)
25
Expect(err).ToNot(HaveOccurred())
28
var actualURL *url.URL
29
delegate := util_http.ClientFunc(func(req *http.Request) (*http.Response, error) {
31
return &http.Response{}, nil
35
client := util_http.ClientWithBaseURL(delegate, baseURL, nil)
37
Expect(client).ToNot(BeIdenticalTo(delegate))
40
req, err := http.NewRequest("GET", given.requestURL, nil)
42
Expect(err).ToNot(HaveOccurred())
45
_, err = client.Do(req)
47
Expect(err).ToNot(HaveOccurred())
50
Expect(actualURL.String()).To(Equal(given.expectedURL))
52
Entry("baseURL without path", testCase{
53
baseURL: "https://kuma-control-plane:5681",
54
requestURL: "/meshes/default/dataplanes",
55
expectedURL: "https://kuma-control-plane:5681/meshes/default/dataplanes",
57
Entry("baseURL without path and request with a relative path", testCase{
58
baseURL: "https://kuma-control-plane:5681",
59
requestURL: "meshes/default/dataplanes",
60
expectedURL: "https://kuma-control-plane:5681/meshes/default/dataplanes",
62
Entry("baseURL with path", testCase{
63
baseURL: "https://kuma-control-plane:5681/proxy/foo/bar",
65
expectedURL: "https://kuma-control-plane:5681/proxy/foo/bar/test",
67
Entry("baseURL that ends with /", testCase{
68
baseURL: "https://kuma-control-plane:5681/",
69
requestURL: "/meshes/default/dataplanes",
70
expectedURL: "https://kuma-control-plane:5681/meshes/default/dataplanes",
72
Entry("baseURL and/or requestURL with double slashes", testCase{
73
baseURL: "https://kuma-control-plane:5681//proxy/foo/bar",
74
requestURL: "/test//baz",
75
expectedURL: "https://kuma-control-plane:5681/proxy/foo/bar/test/baz",
79
It("should tolerate nil URL", func() {
81
baseURL, err := url.Parse("https://kuma-control-plane:5681")
82
Expect(err).ToNot(HaveOccurred())
85
var actualURL *url.URL
86
delegate := util_http.ClientFunc(func(req *http.Request) (*http.Response, error) {
88
return &http.Response{}, nil
92
client := util_http.ClientWithBaseURL(delegate, baseURL, nil)
94
Expect(client).ToNot(BeIdenticalTo(delegate))
101
_, err = client.Do(req)
103
Expect(err).ToNot(HaveOccurred())
106
Expect(actualURL).To(BeNil())