| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"net/http" | 
					
						
							|  |  |  | 	"net/url" | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	. "github.com/smartystreets/goconvey/convey" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-05 17:37:13 +08:00
										 |  |  | 	m "github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-24 03:48:43 +08:00
										 |  |  | func TestDataSourceProxy(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | 	Convey("When getting graphite datasource proxy", t, func() { | 
					
						
							|  |  |  | 		ds := m.DataSource{Url: "htttp://graphite:8080", Type: m.DS_GRAPHITE} | 
					
						
							| 
									
										
										
										
											2016-11-16 16:54:26 +08:00
										 |  |  | 		targetUrl, err := url.Parse(ds.Url) | 
					
						
							| 
									
										
										
										
											2015-09-10 21:27:45 +08:00
										 |  |  | 		proxy := NewReverseProxy(&ds, "/render", targetUrl) | 
					
						
							| 
									
										
										
										
											2016-12-07 18:10:42 +08:00
										 |  |  | 		proxy.Transport, err = ds.GetHttpTransport() | 
					
						
							| 
									
										
										
										
											2016-11-16 16:54:26 +08:00
										 |  |  | 		So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		transport, ok := proxy.Transport.(*http.Transport) | 
					
						
							|  |  |  | 		So(ok, ShouldBeTrue) | 
					
						
							|  |  |  | 		So(transport.TLSClientConfig.InsecureSkipVerify, ShouldBeTrue) | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		requestUrl, _ := url.Parse("http://grafana.com/sub") | 
					
						
							|  |  |  | 		req := http.Request{URL: requestUrl} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		proxy.Director(&req) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Convey("Can translate request url and path", func() { | 
					
						
							|  |  |  | 			So(req.URL.Host, ShouldEqual, "graphite:8080") | 
					
						
							|  |  |  | 			So(req.URL.Path, ShouldEqual, "/render") | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Convey("When getting influxdb datasource proxy", t, func() { | 
					
						
							|  |  |  | 		ds := m.DataSource{ | 
					
						
							| 
									
										
										
										
											2015-02-28 19:34:51 +08:00
										 |  |  | 			Type:     m.DS_INFLUXDB_08, | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | 			Url:      "http://influxdb:8083", | 
					
						
							|  |  |  | 			Database: "site", | 
					
						
							|  |  |  | 			User:     "user", | 
					
						
							|  |  |  | 			Password: "password", | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-10 21:27:45 +08:00
										 |  |  | 		targetUrl, _ := url.Parse(ds.Url) | 
					
						
							|  |  |  | 		proxy := NewReverseProxy(&ds, "", targetUrl) | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		requestUrl, _ := url.Parse("http://grafana.com/sub") | 
					
						
							|  |  |  | 		req := http.Request{URL: requestUrl} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		proxy.Director(&req) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Convey("Should add db to url", func() { | 
					
						
							|  |  |  | 			So(req.URL.Path, ShouldEqual, "/db/site/") | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Convey("Should add username and password", func() { | 
					
						
							|  |  |  | 			queryVals := req.URL.Query() | 
					
						
							|  |  |  | 			So(queryVals["u"][0], ShouldEqual, "user") | 
					
						
							|  |  |  | 			So(queryVals["p"][0], ShouldEqual, "password") | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2016-11-16 16:54:26 +08:00
										 |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | } |