dataproxy_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package api
  2. import (
  3. "net/http"
  4. "net/url"
  5. "testing"
  6. . "github.com/smartystreets/goconvey/convey"
  7. m "github.com/grafana/grafana/pkg/models"
  8. )
  9. func TestDataSourceProxy(t *testing.T) {
  10. Convey("When getting graphite datasource proxy", t, func() {
  11. ds := m.DataSource{Url: "htttp://graphite:8080", Type: m.DS_GRAPHITE}
  12. targetUrl, err := url.Parse(ds.Url)
  13. proxy := NewReverseProxy(&ds, "/render", targetUrl)
  14. proxy.Transport, err = ds.GetHttpTransport()
  15. So(err, ShouldBeNil)
  16. transport, ok := proxy.Transport.(*http.Transport)
  17. So(ok, ShouldBeTrue)
  18. So(transport.TLSClientConfig.InsecureSkipVerify, ShouldBeTrue)
  19. requestUrl, _ := url.Parse("http://grafana.com/sub")
  20. req := http.Request{URL: requestUrl}
  21. proxy.Director(&req)
  22. Convey("Can translate request url and path", func() {
  23. So(req.URL.Host, ShouldEqual, "graphite:8080")
  24. So(req.URL.Path, ShouldEqual, "/render")
  25. })
  26. })
  27. Convey("When getting influxdb datasource proxy", t, func() {
  28. ds := m.DataSource{
  29. Type: m.DS_INFLUXDB_08,
  30. Url: "http://influxdb:8083",
  31. Database: "site",
  32. User: "user",
  33. Password: "password",
  34. }
  35. targetUrl, _ := url.Parse(ds.Url)
  36. proxy := NewReverseProxy(&ds, "", targetUrl)
  37. requestUrl, _ := url.Parse("http://grafana.com/sub")
  38. req := http.Request{URL: requestUrl}
  39. proxy.Director(&req)
  40. Convey("Should add db to url", func() {
  41. So(req.URL.Path, ShouldEqual, "/db/site/")
  42. })
  43. Convey("Should add username and password", func() {
  44. queryVals := req.URL.Query()
  45. So(queryVals["u"][0], ShouldEqual, "user")
  46. So(queryVals["p"][0], ShouldEqual, "password")
  47. })
  48. })
  49. }