dataproxy_test.go 1.3 KB

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