mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
dsproxy: move http client variable back
After refactoring to be able to mock the client in a test, the client wasn't a global variable anymore. This change moves the client back to being a package- level variable.
This commit is contained in:
parent
719ebdc24d
commit
10d30f0b73
@ -27,6 +27,7 @@ import (
|
|||||||
var (
|
var (
|
||||||
logger = log.New("data-proxy-log")
|
logger = log.New("data-proxy-log")
|
||||||
tokenCache = map[string]*jwtToken{}
|
tokenCache = map[string]*jwtToken{}
|
||||||
|
client = newHTTPClient()
|
||||||
)
|
)
|
||||||
|
|
||||||
type jwtToken struct {
|
type jwtToken struct {
|
||||||
@ -42,10 +43,9 @@ type DataSourceProxy struct {
|
|||||||
proxyPath string
|
proxyPath string
|
||||||
route *plugins.AppPluginRoute
|
route *plugins.AppPluginRoute
|
||||||
plugin *plugins.DataSourcePlugin
|
plugin *plugins.DataSourcePlugin
|
||||||
httpClient HttpClient
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type HttpClient interface {
|
type httpClient interface {
|
||||||
Do(req *http.Request) (*http.Response, error)
|
Do(req *http.Request) (*http.Response, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -58,10 +58,13 @@ func NewDataSourceProxy(ds *m.DataSource, plugin *plugins.DataSourcePlugin, ctx
|
|||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
proxyPath: proxyPath,
|
proxyPath: proxyPath,
|
||||||
targetUrl: targetURL,
|
targetUrl: targetURL,
|
||||||
httpClient: &http.Client{
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func newHTTPClient() httpClient {
|
||||||
|
return &http.Client{
|
||||||
Timeout: time.Second * 30,
|
Timeout: time.Second * 30,
|
||||||
Transport: &http.Transport{Proxy: http.ProxyFromEnvironment},
|
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-Type", "application/x-www-form-urlencoded")
|
||||||
getTokenReq.Header.Add("Content-Length", strconv.Itoa(len(params.Encode())))
|
getTokenReq.Header.Add("Content-Length", strconv.Itoa(len(params.Encode())))
|
||||||
|
|
||||||
resp, err := proxy.httpClient.Do(getTokenReq)
|
resp, err := client.Do(getTokenReq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
@ -165,7 +165,8 @@ func TestDSRouteRule(t *testing.T) {
|
|||||||
json, err := ioutil.ReadFile("./test-data/access-token-1.json")
|
json, err := ioutil.ReadFile("./test-data/access-token-1.json")
|
||||||
So(err, ShouldBeNil)
|
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.route = plugin.Routes[0]
|
||||||
proxy1.applyRoute(req)
|
proxy1.applyRoute(req)
|
||||||
|
|
||||||
@ -178,7 +179,8 @@ func TestDSRouteRule(t *testing.T) {
|
|||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
|
|
||||||
req, _ := http.NewRequest("GET", "http://localhost/asd", nil)
|
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.route = plugin.Routes[1]
|
||||||
proxy2.applyRoute(req)
|
proxy2.applyRoute(req)
|
||||||
|
|
||||||
@ -192,7 +194,8 @@ func TestDSRouteRule(t *testing.T) {
|
|||||||
Convey("third call to first route should add cached access token", func() {
|
Convey("third call to first route should add cached access token", func() {
|
||||||
req, _ := http.NewRequest("GET", "http://localhost/asd", nil)
|
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.route = plugin.Routes[0]
|
||||||
proxy3.applyRoute(req)
|
proxy3.applyRoute(req)
|
||||||
|
|
||||||
@ -322,11 +325,11 @@ func TestDSRouteRule(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
type HttpClientStub struct {
|
type httpClientStub struct {
|
||||||
fakeBody []byte
|
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)
|
bodyJSON, _ := simplejson.NewJson(c.fakeBody)
|
||||||
_, passedTokenCacheTest := bodyJSON.CheckGet("expires_on")
|
_, passedTokenCacheTest := bodyJSON.CheckGet("expires_on")
|
||||||
So(passedTokenCacheTest, ShouldBeTrue)
|
So(passedTokenCacheTest, ShouldBeTrue)
|
||||||
@ -340,17 +343,8 @@ func (c *HttpClientStub) Do(req *http.Request) (*http.Response, error) {
|
|||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDataSourceProxyWithMock(ds *m.DataSource, plugin *plugins.DataSourcePlugin, ctx *m.ReqContext, proxyPath string, fakeBody []byte) *DataSourceProxy {
|
func newFakeHTTPClient(fakeBody []byte) httpClient {
|
||||||
targetURL, _ := url.Parse(ds.Url)
|
return &httpClientStub{
|
||||||
|
|
||||||
return &DataSourceProxy{
|
|
||||||
ds: ds,
|
|
||||||
plugin: plugin,
|
|
||||||
ctx: ctx,
|
|
||||||
proxyPath: proxyPath,
|
|
||||||
targetUrl: targetURL,
|
|
||||||
httpClient: &HttpClientStub{
|
|
||||||
fakeBody: fakeBody,
|
fakeBody: fakeBody,
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user