ds_proxy_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package pluginproxy
  2. import (
  3. "net/http"
  4. "testing"
  5. "github.com/grafana/grafana/pkg/components/simplejson"
  6. m "github.com/grafana/grafana/pkg/models"
  7. "github.com/grafana/grafana/pkg/plugins"
  8. "github.com/grafana/grafana/pkg/setting"
  9. "github.com/grafana/grafana/pkg/util"
  10. . "github.com/smartystreets/goconvey/convey"
  11. )
  12. func TestDSRouteRule(t *testing.T) {
  13. Convey("When applying ds route rule", t, func() {
  14. plugin := &plugins.DataSourcePlugin{
  15. Routes: []*plugins.AppPluginRoute{
  16. {
  17. Path: "api/v4/",
  18. Url: "https://www.google.com",
  19. Headers: []plugins.AppPluginRouteHeader{
  20. {Name: "x-header", Content: "my secret {{.SecureJsonData.key}}"},
  21. },
  22. },
  23. },
  24. }
  25. setting.SecretKey = "password"
  26. key, _ := util.Encrypt([]byte("123"), "password")
  27. ds := &m.DataSource{
  28. JsonData: simplejson.NewFromAny(map[string]interface{}{
  29. "clientId": "asd",
  30. }),
  31. SecureJsonData: map[string][]byte{
  32. "key": key,
  33. },
  34. }
  35. req, _ := http.NewRequest("GET", "http://localhost/asd", nil)
  36. Convey("When not matching route path", func() {
  37. ApplyDataSourceRouteRules(req, plugin, ds, "/asdas/asd")
  38. Convey("should not touch req", func() {
  39. So(len(req.Header), ShouldEqual, 0)
  40. So(req.URL.String(), ShouldEqual, "http://localhost/asd")
  41. })
  42. })
  43. Convey("When matching route path", func() {
  44. ApplyDataSourceRouteRules(req, plugin, ds, "api/v4/some/method")
  45. Convey("should add headers and update url", func() {
  46. So(req.URL.String(), ShouldEqual, "https://www.google.com/some/method")
  47. So(req.Header.Get("x-header"), ShouldEqual, "my secret 123")
  48. })
  49. })
  50. })
  51. }