dataproxy_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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, _ := url.Parse(ds.Url)
  13. proxy := NewReverseProxy(&ds, "/render", targetUrl)
  14. requestUrl, _ := url.Parse("http://grafana.com/sub")
  15. req := http.Request{URL: requestUrl}
  16. proxy.Director(&req)
  17. Convey("Can translate request url and path", func() {
  18. So(req.URL.Host, ShouldEqual, "graphite:8080")
  19. So(req.URL.Path, ShouldEqual, "/render")
  20. })
  21. })
  22. Convey("When getting influxdb datasource proxy", t, func() {
  23. ds := m.DataSource{
  24. Type: m.DS_INFLUXDB_08,
  25. Url: "http://influxdb:8083",
  26. Database: "site",
  27. User: "user",
  28. Password: "password",
  29. }
  30. targetUrl, _ := url.Parse(ds.Url)
  31. proxy := NewReverseProxy(&ds, "", targetUrl)
  32. requestUrl, _ := url.Parse("http://grafana.com/sub")
  33. req := http.Request{URL: requestUrl}
  34. proxy.Director(&req)
  35. Convey("Should add db to url", func() {
  36. So(req.URL.Path, ShouldEqual, "/db/site/")
  37. })
  38. Convey("Should add username and password", func() {
  39. queryVals := req.URL.Query()
  40. So(queryVals["u"][0], ShouldEqual, "user")
  41. So(queryVals["p"][0], ShouldEqual, "password")
  42. })
  43. })
  44. }