kuma

Форк
0
/
client_test.go 
109 строк · 3.0 Кб
1
package http_test
2

3
import (
4
	"net/http"
5
	"net/url"
6

7
	. "github.com/onsi/ginkgo/v2"
8
	. "github.com/onsi/gomega"
9

10
	util_http "github.com/kumahq/kuma/pkg/util/http"
11
)
12

13
var _ = Describe("Http Util", func() {
14
	Describe("ClientWithBaseURL(..)", func() {
15
		type testCase struct {
16
			baseURL     string
17
			requestURL  string
18
			expectedURL string
19
		}
20

21
		DescribeTable("should rewrite request URL by combining `baseURL` and `requestURL`",
22
			func(given testCase) {
23
				// setup
24
				baseURL, err := url.Parse(given.baseURL)
25
				Expect(err).ToNot(HaveOccurred())
26

27
				// and
28
				var actualURL *url.URL
29
				delegate := util_http.ClientFunc(func(req *http.Request) (*http.Response, error) {
30
					actualURL = req.URL
31
					return &http.Response{}, nil
32
				})
33

34
				// when
35
				client := util_http.ClientWithBaseURL(delegate, baseURL, nil)
36
				// then
37
				Expect(client).ToNot(BeIdenticalTo(delegate))
38

39
				// when
40
				req, err := http.NewRequest("GET", given.requestURL, nil)
41
				// then
42
				Expect(err).ToNot(HaveOccurred())
43

44
				// when
45
				_, err = client.Do(req)
46
				// then
47
				Expect(err).ToNot(HaveOccurred())
48

49
				// and
50
				Expect(actualURL.String()).To(Equal(given.expectedURL))
51
			},
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",
56
			}),
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",
61
			}),
62
			Entry("baseURL with path", testCase{
63
				baseURL:     "https://kuma-control-plane:5681/proxy/foo/bar",
64
				requestURL:  "/test",
65
				expectedURL: "https://kuma-control-plane:5681/proxy/foo/bar/test",
66
			}),
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",
71
			}),
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",
76
			}),
77
		)
78

79
		It("should tolerate nil URL", func() {
80
			// setup
81
			baseURL, err := url.Parse("https://kuma-control-plane:5681")
82
			Expect(err).ToNot(HaveOccurred())
83

84
			// and
85
			var actualURL *url.URL
86
			delegate := util_http.ClientFunc(func(req *http.Request) (*http.Response, error) {
87
				actualURL = req.URL
88
				return &http.Response{}, nil
89
			})
90

91
			// when
92
			client := util_http.ClientWithBaseURL(delegate, baseURL, nil)
93
			// then
94
			Expect(client).ToNot(BeIdenticalTo(delegate))
95

96
			// when
97
			req := &http.Request{
98
				URL: nil,
99
			}
100
			// and
101
			_, err = client.Do(req)
102
			// then
103
			Expect(err).ToNot(HaveOccurred())
104

105
			// and
106
			Expect(actualURL).To(BeNil())
107
		})
108
	})
109
})
110

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.