diff --git a/pkg/api/pluginproxy/ds_proxy.go b/pkg/api/pluginproxy/ds_proxy.go index eb2bffec570..95e023efe8f 100644 --- a/pkg/api/pluginproxy/ds_proxy.go +++ b/pkg/api/pluginproxy/ds_proxy.go @@ -27,6 +27,7 @@ import ( var ( logger = log.New("data-proxy-log") tokenCache = map[string]*jwtToken{} + client = newHTTPClient() ) type jwtToken struct { @@ -36,16 +37,15 @@ type jwtToken struct { } type DataSourceProxy struct { - ds *m.DataSource - ctx *m.ReqContext - targetUrl *url.URL - proxyPath string - route *plugins.AppPluginRoute - plugin *plugins.DataSourcePlugin - httpClient HttpClient + ds *m.DataSource + ctx *m.ReqContext + targetUrl *url.URL + proxyPath string + route *plugins.AppPluginRoute + plugin *plugins.DataSourcePlugin } -type HttpClient interface { +type httpClient interface { Do(req *http.Request) (*http.Response, error) } @@ -58,10 +58,13 @@ func NewDataSourceProxy(ds *m.DataSource, plugin *plugins.DataSourcePlugin, ctx ctx: ctx, proxyPath: proxyPath, targetUrl: targetURL, - httpClient: &http.Client{ - Timeout: time.Second * 30, - Transport: &http.Transport{Proxy: http.ProxyFromEnvironment}, - }, + } +} + +func newHTTPClient() httpClient { + return &http.Client{ + Timeout: time.Second * 30, + Transport: &http.Transport{Proxy: http.ProxyFromEnvironment}, } } @@ -341,7 +344,7 @@ func (proxy *DataSourceProxy) getAccessToken(data templateData) (string, error) getTokenReq.Header.Add("Content-Type", "application/x-www-form-urlencoded") getTokenReq.Header.Add("Content-Length", strconv.Itoa(len(params.Encode()))) - resp, err := proxy.httpClient.Do(getTokenReq) + resp, err := client.Do(getTokenReq) if err != nil { return "", err } diff --git a/pkg/api/pluginproxy/ds_proxy_test.go b/pkg/api/pluginproxy/ds_proxy_test.go index 47919201180..615a64c7bea 100644 --- a/pkg/api/pluginproxy/ds_proxy_test.go +++ b/pkg/api/pluginproxy/ds_proxy_test.go @@ -165,7 +165,8 @@ func TestDSRouteRule(t *testing.T) { json, err := ioutil.ReadFile("./test-data/access-token-1.json") So(err, ShouldBeNil) - proxy1 := NewDataSourceProxyWithMock(ds, plugin, ctx, "pathwithtoken1", json) + client = newFakeHTTPClient(json) + proxy1 := NewDataSourceProxy(ds, plugin, ctx, "pathwithtoken1") proxy1.route = plugin.Routes[0] proxy1.applyRoute(req) @@ -178,7 +179,8 @@ func TestDSRouteRule(t *testing.T) { So(err, ShouldBeNil) req, _ := http.NewRequest("GET", "http://localhost/asd", nil) - proxy2 := NewDataSourceProxyWithMock(ds, plugin, ctx, "pathwithtoken2", json2) + client = newFakeHTTPClient(json2) + proxy2 := NewDataSourceProxy(ds, plugin, ctx, "pathwithtoken2") proxy2.route = plugin.Routes[1] proxy2.applyRoute(req) @@ -192,7 +194,8 @@ func TestDSRouteRule(t *testing.T) { Convey("third call to first route should add cached access token", func() { req, _ := http.NewRequest("GET", "http://localhost/asd", nil) - proxy3 := NewDataSourceProxyWithMock(ds, plugin, ctx, "pathwithtoken1", []byte{}) + client = newFakeHTTPClient([]byte{}) + proxy3 := NewDataSourceProxy(ds, plugin, ctx, "pathwithtoken1") proxy3.route = plugin.Routes[0] proxy3.applyRoute(req) @@ -322,11 +325,11 @@ func TestDSRouteRule(t *testing.T) { }) } -type HttpClientStub struct { +type httpClientStub struct { fakeBody []byte } -func (c *HttpClientStub) Do(req *http.Request) (*http.Response, error) { +func (c *httpClientStub) Do(req *http.Request) (*http.Response, error) { bodyJSON, _ := simplejson.NewJson(c.fakeBody) _, passedTokenCacheTest := bodyJSON.CheckGet("expires_on") So(passedTokenCacheTest, ShouldBeTrue) @@ -340,17 +343,8 @@ func (c *HttpClientStub) Do(req *http.Request) (*http.Response, error) { return resp, nil } -func NewDataSourceProxyWithMock(ds *m.DataSource, plugin *plugins.DataSourcePlugin, ctx *m.ReqContext, proxyPath string, fakeBody []byte) *DataSourceProxy { - targetURL, _ := url.Parse(ds.Url) - - return &DataSourceProxy{ - ds: ds, - plugin: plugin, - ctx: ctx, - proxyPath: proxyPath, - targetUrl: targetURL, - httpClient: &HttpClientStub{ - fakeBody: fakeBody, - }, +func newFakeHTTPClient(fakeBody []byte) httpClient { + return &httpClientStub{ + fakeBody: fakeBody, } }