9
9
"testing"
10
10
11
11
"github.com/go-openapi/runtime/client"
12
+ grafana_client "github.com/grafana/grafana-openapi-client-go/client"
12
13
"github.com/stretchr/testify/assert"
13
14
"github.com/stretchr/testify/require"
14
15
)
@@ -22,6 +23,52 @@ func TestExtractIncidentClientFromEnv(t *testing.T) {
22
23
assert .Equal (t , "http://my-test-url.grafana.com/api/plugins/grafana-irm-app/resources/api/v1/" , client .RemoteHost )
23
24
}
24
25
26
+ func TestExtractIncidentClientFromHeaders (t * testing.T ) {
27
+ t .Run ("no headers, no env" , func (t * testing.T ) {
28
+ req , err := http .NewRequest ("GET" , "http://example.com" , nil )
29
+ require .NoError (t , err )
30
+ ctx := ExtractIncidentClientFromHeaders (context .Background (), req )
31
+
32
+ client := IncidentClientFromContext (ctx )
33
+ require .NotNil (t , client )
34
+ assert .Equal (t , "http://localhost:3000/api/plugins/grafana-irm-app/resources/api/v1/" , client .RemoteHost )
35
+ })
36
+
37
+ t .Run ("no headers, with env" , func (t * testing.T ) {
38
+ t .Setenv ("GRAFANA_URL" , "http://my-test-url.grafana.com/" )
39
+ req , err := http .NewRequest ("GET" , "http://example.com" , nil )
40
+ require .NoError (t , err )
41
+ ctx := ExtractIncidentClientFromHeaders (context .Background (), req )
42
+
43
+ client := IncidentClientFromContext (ctx )
44
+ require .NotNil (t , client )
45
+ assert .Equal (t , "http://my-test-url.grafana.com/api/plugins/grafana-irm-app/resources/api/v1/" , client .RemoteHost )
46
+ })
47
+
48
+ t .Run ("with headers, no env" , func (t * testing.T ) {
49
+ req , err := http .NewRequest ("GET" , "http://example.com" , nil )
50
+ req .Header .Set (grafanaURLHeader , "http://my-test-url.grafana.com" )
51
+ require .NoError (t , err )
52
+ ctx := ExtractIncidentClientFromHeaders (context .Background (), req )
53
+
54
+ client := IncidentClientFromContext (ctx )
55
+ require .NotNil (t , client )
56
+ assert .Equal (t , "http://my-test-url.grafana.com/api/plugins/grafana-irm-app/resources/api/v1/" , client .RemoteHost )
57
+ })
58
+
59
+ t .Run ("with headers, with env" , func (t * testing.T ) {
60
+ t .Setenv ("GRAFANA_URL" , "will-not-be-used" )
61
+ req , err := http .NewRequest ("GET" , "http://example.com" , nil )
62
+ req .Header .Set (grafanaURLHeader , "http://my-test-url.grafana.com" )
63
+ require .NoError (t , err )
64
+ ctx := ExtractIncidentClientFromHeaders (context .Background (), req )
65
+
66
+ client := IncidentClientFromContext (ctx )
67
+ require .NotNil (t , client )
68
+ assert .Equal (t , "http://my-test-url.grafana.com/api/plugins/grafana-irm-app/resources/api/v1/" , client .RemoteHost )
69
+ })
70
+ }
71
+
25
72
func TestExtractGrafanaInfoFromHeaders (t * testing.T ) {
26
73
t .Run ("no headers, no env" , func (t * testing.T ) {
27
74
req , err := http .NewRequest ("GET" , "http://example.com" , nil )
@@ -106,3 +153,64 @@ func TestExtractGrafanaClientPath(t *testing.T) {
106
153
assert .Equal (t , "/grafana/api" , rt .BasePath )
107
154
})
108
155
}
156
+
157
+ // minURL is a helper struct representing what we can extract from a constructed
158
+ // Grafana client.
159
+ type minURL struct {
160
+ host , basePath string
161
+ }
162
+
163
+ // minURLFromClient extracts some minimal amount of URL info from a Grafana client.
164
+ func minURLFromClient (c * grafana_client.GrafanaHTTPAPI ) minURL {
165
+ rt := c .Transport .(* client.Runtime )
166
+ return minURL {rt .Host , rt .BasePath }
167
+ }
168
+
169
+ func TestExtractGrafanaClientFromHeaders (t * testing.T ) {
170
+ t .Run ("no headers, no env" , func (t * testing.T ) {
171
+ req , err := http .NewRequest ("GET" , "http://example.com" , nil )
172
+ require .NoError (t , err )
173
+ ctx := ExtractGrafanaClientFromHeaders (context .Background (), req )
174
+ c := GrafanaClientFromContext (ctx )
175
+ url := minURLFromClient (c )
176
+ assert .Equal (t , "localhost" , url .host )
177
+ assert .Equal (t , "/api" , url .basePath )
178
+ })
179
+
180
+ t .Run ("no headers, with env" , func (t * testing.T ) {
181
+ t .Setenv ("GRAFANA_URL" , "http://my-test-url.grafana.com" )
182
+
183
+ req , err := http .NewRequest ("GET" , "http://example.com" , nil )
184
+ require .NoError (t , err )
185
+ ctx := ExtractGrafanaClientFromHeaders (context .Background (), req )
186
+ c := GrafanaClientFromContext (ctx )
187
+ url := minURLFromClient (c )
188
+ assert .Equal (t , "my-test-url.grafana.com" , url .host )
189
+ assert .Equal (t , "/api" , url .basePath )
190
+ })
191
+
192
+ t .Run ("with headers, no env" , func (t * testing.T ) {
193
+ req , err := http .NewRequest ("GET" , "http://example.com" , nil )
194
+ require .NoError (t , err )
195
+ req .Header .Set (grafanaURLHeader , "http://my-test-url.grafana.com" )
196
+ ctx := ExtractGrafanaClientFromHeaders (context .Background (), req )
197
+ c := GrafanaClientFromContext (ctx )
198
+ url := minURLFromClient (c )
199
+ assert .Equal (t , "my-test-url.grafana.com" , url .host )
200
+ assert .Equal (t , "/api" , url .basePath )
201
+ })
202
+
203
+ t .Run ("with headers, with env" , func (t * testing.T ) {
204
+ // Env vars should be ignored if headers are present.
205
+ t .Setenv ("GRAFANA_URL" , "will-not-be-used" )
206
+
207
+ req , err := http .NewRequest ("GET" , "http://example.com" , nil )
208
+ require .NoError (t , err )
209
+ req .Header .Set (grafanaURLHeader , "http://my-test-url.grafana.com" )
210
+ ctx := ExtractGrafanaClientFromHeaders (context .Background (), req )
211
+ c := GrafanaClientFromContext (ctx )
212
+ url := minURLFromClient (c )
213
+ assert .Equal (t , "my-test-url.grafana.com" , url .host )
214
+ assert .Equal (t , "/api" , url .basePath )
215
+ })
216
+ }
0 commit comments