Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/mimir/client/client_test.go
4096 views
1
package client
2
3
import (
4
"net/http"
5
"net/url"
6
"testing"
7
8
"github.com/stretchr/testify/require"
9
)
10
11
func TestBuildURL(t *testing.T) {
12
tc := []struct {
13
name string
14
path string
15
method string
16
url string
17
resultURL string
18
}{
19
{
20
name: "builds the correct URL with a trailing slash",
21
path: "/prometheus/config/v1/rules",
22
method: http.MethodPost,
23
url: "http://mimir.local/",
24
resultURL: "http://mimir.local/prometheus/config/v1/rules",
25
},
26
{
27
name: "builds the correct URL without a trailing slash",
28
path: "/prometheus/config/v1/rules",
29
method: http.MethodPost,
30
url: "http://mimir.local",
31
resultURL: "http://mimir.local/prometheus/config/v1/rules",
32
},
33
{
34
name: "builds the correct URL when the base url has a path",
35
path: "/prometheus/config/v1/rules",
36
method: http.MethodPost,
37
url: "http://mimir.local/apathto",
38
resultURL: "http://mimir.local/apathto/prometheus/config/v1/rules",
39
},
40
{
41
name: "builds the correct URL when the base url has a path with trailing slash",
42
path: "/prometheus/config/v1/rules",
43
method: http.MethodPost,
44
url: "http://mimir.local/apathto/",
45
resultURL: "http://mimir.local/apathto/prometheus/config/v1/rules",
46
},
47
{
48
name: "builds the correct URL with a trailing slash and the target path contains special characters",
49
path: "/prometheus/config/v1/rules/%20%2Fspace%F0%9F%8D%BB",
50
method: http.MethodPost,
51
url: "http://mimir.local/",
52
resultURL: "http://mimir.local/prometheus/config/v1/rules/%20%2Fspace%F0%9F%8D%BB",
53
},
54
{
55
name: "builds the correct URL without a trailing slash and the target path contains special characters",
56
path: "/prometheus/config/v1/rules/%20%2Fspace%F0%9F%8D%BB",
57
method: http.MethodPost,
58
url: "http://mimir.local",
59
resultURL: "http://mimir.local/prometheus/config/v1/rules/%20%2Fspace%F0%9F%8D%BB",
60
},
61
{
62
name: "builds the correct URL when the base url has a path and the target path contains special characters",
63
path: "/prometheus/config/v1/rules/%20%2Fspace%F0%9F%8D%BB",
64
method: http.MethodPost,
65
url: "http://mimir.local/apathto",
66
resultURL: "http://mimir.local/apathto/prometheus/config/v1/rules/%20%2Fspace%F0%9F%8D%BB",
67
},
68
{
69
name: "builds the correct URL when the base url has a path and the target path starts with a escaped slash",
70
path: "/prometheus/config/v1/rules/%2F-first-char-slash",
71
method: http.MethodPost,
72
url: "http://mimir.local/apathto",
73
resultURL: "http://mimir.local/apathto/prometheus/config/v1/rules/%2F-first-char-slash",
74
},
75
{
76
name: "builds the correct URL when the base url has a path and the target path ends with a escaped slash",
77
path: "/prometheus/config/v1/rules/last-char-slash%2F",
78
method: http.MethodPost,
79
url: "http://mimir.local/apathto",
80
resultURL: "http://mimir.local/apathto/prometheus/config/v1/rules/last-char-slash%2F",
81
},
82
}
83
84
for _, tt := range tc {
85
t.Run(tt.name, func(t *testing.T) {
86
url, err := url.Parse(tt.url)
87
require.NoError(t, err)
88
89
req, err := buildRequest("op", tt.path, tt.method, *url, []byte{})
90
require.NoError(t, err)
91
require.Equal(t, tt.resultURL, req.URL.String())
92
})
93
}
94
}
95
96