@@ -2,8 +2,10 @@ package backend
22
33import (
44 "testing"
5+ "time"
56
67 "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
8+ "github.com/grafana/grafana-plugin-sdk-go/backend/proxy"
79 "github.com/stretchr/testify/assert"
810 "github.com/stretchr/testify/require"
911)
@@ -163,6 +165,27 @@ func TestDataSourceInstanceSettings(t *testing.T) {
163165 },
164166 },
165167 },
168+ {
169+ instanceSettings : & DataSourceInstanceSettings {
170+ UID : "uid1" ,
171+ JSONData : []byte ("{ \" enableSecureSocksProxy\" : true }" ),
172+ DecryptedSecureJSONData : map [string ]string {},
173+ },
174+ expectedClientOptions : httpclient.Options {
175+ ProxyOptions : & proxy.Options {
176+ Enabled : true ,
177+ Auth : & proxy.AuthOptions {
178+ Username : "uid1" ,
179+ },
180+ },
181+ CustomOptions : map [string ]interface {}{
182+ dataCustomOptionsKey : map [string ]interface {}{
183+ "enableSecureSocksProxy" : true ,
184+ },
185+ secureDataCustomOptionsKey : map [string ]string {},
186+ },
187+ },
188+ },
166189 }
167190
168191 for _ , tc := range tcs {
@@ -237,3 +260,96 @@ func TestCustomOptions(t *testing.T) {
237260 require .Empty (t , secureJSONData )
238261 })
239262}
263+
264+ func TestProxyOptions (t * testing.T ) {
265+ t .Run ("ProxyOptions() should translate settings as expected" , func (t * testing.T ) {
266+ tcs := []struct {
267+ instanceSettings * DataSourceInstanceSettings
268+ expectedClientOptions * proxy.Options
269+ }{
270+ {
271+ instanceSettings : & DataSourceInstanceSettings {},
272+ expectedClientOptions : nil ,
273+ },
274+ {
275+ instanceSettings : & DataSourceInstanceSettings {
276+ Name : "ds1" ,
277+ UID : "uid1" ,
278+ User : "user" ,
279+ Type : "example-datasource" ,
280+ JSONData : []byte ("{ \" enableSecureSocksProxy\" : false }" ),
281+ BasicAuthEnabled : true ,
282+ BasicAuthUser : "buser" ,
283+ },
284+ expectedClientOptions : nil ,
285+ },
286+ {
287+ instanceSettings : & DataSourceInstanceSettings {
288+ Name : "ds1" ,
289+ UID : "uid1" ,
290+ User : "user" ,
291+ Type : "example-datasource" ,
292+ JSONData : []byte ("{ \" enableSecureSocksProxy\" : true }" ),
293+ BasicAuthEnabled : true ,
294+ BasicAuthUser : "buser" ,
295+ },
296+ expectedClientOptions : & proxy.Options {
297+ Enabled : true ,
298+ Auth : & proxy.AuthOptions {
299+ Username : "uid1" ,
300+ },
301+ Timeouts : & proxy .DefaultTimeoutOptions ,
302+ },
303+ },
304+ {
305+ instanceSettings : & DataSourceInstanceSettings {
306+ Name : "ds1" ,
307+ UID : "uid1" ,
308+ User : "user" ,
309+ Type : "example-datasource" ,
310+ JSONData : []byte ("{ \" enableSecureSocksProxy\" : true, \" secureSocksProxyUsername\" : \" username\" }" ),
311+ BasicAuthEnabled : true ,
312+ BasicAuthUser : "buser" ,
313+ DecryptedSecureJSONData : map [string ]string {
314+ "secureSocksProxyPassword" : "pswd" ,
315+ },
316+ },
317+ expectedClientOptions : & proxy.Options {
318+ Enabled : true ,
319+ Auth : & proxy.AuthOptions {
320+ Username : "username" ,
321+ Password : "pswd" ,
322+ },
323+ Timeouts : & proxy .DefaultTimeoutOptions ,
324+ },
325+ },
326+ {
327+ instanceSettings : & DataSourceInstanceSettings {
328+ Name : "ds1" ,
329+ UID : "uid1" ,
330+ User : "user" ,
331+ Type : "example-datasource" ,
332+ JSONData : []byte ("{ \" enableSecureSocksProxy\" : true, \" timeout\" : 10, \" keepAlive\" : 15 }" ),
333+ BasicAuthEnabled : true ,
334+ BasicAuthUser : "buser" ,
335+ },
336+ expectedClientOptions : & proxy.Options {
337+ Enabled : true ,
338+ Auth : & proxy.AuthOptions {
339+ Username : "uid1" ,
340+ },
341+ Timeouts : & proxy.TimeoutOptions {
342+ KeepAlive : time .Second * 15 ,
343+ Timeout : time .Second * 10 ,
344+ },
345+ },
346+ },
347+ }
348+
349+ for _ , tc := range tcs {
350+ opts , err := tc .instanceSettings .ProxyOptions ()
351+ assert .NoError (t , err )
352+ assert .Equal (t , tc .expectedClientOptions , opts )
353+ }
354+ })
355+ }
0 commit comments