From 5c2bffe5ded0cf5f1dc259e196377ec03c25a6af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20=C3=81lvarez?= Date: Tue, 4 Jun 2019 15:32:45 +0200 Subject: [PATCH 1/2] Export Kibana Client's Transport and Version Required to mock a Kibana connection outside its own package --- libbeat/kibana/client.go | 14 +++++++------- libbeat/kibana/client_test.go | 6 +++--- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/libbeat/kibana/client.go b/libbeat/kibana/client.go index 27851be4a5d..3ba0356f13e 100644 --- a/libbeat/kibana/client.go +++ b/libbeat/kibana/client.go @@ -41,8 +41,8 @@ type Connection struct { Username string Password string - http *http.Client - version common.Version + Http *http.Client + Version common.Version } type Client struct { @@ -134,7 +134,7 @@ func NewClientWithConfig(config *ClientConfig) (*Client, error) { URL: kibanaURL, Username: username, Password: password, - http: &http.Client{ + Http: &http.Client{ Transport: &http.Transport{ Dial: dialer.Dial, DialTLS: tlsDialer.Dial, @@ -195,7 +195,7 @@ func (conn *Connection) Send(method, extraPath string, req.Header.Add("Accept", "application/json") req.Header.Set("kbn-xsrf", "1") if method != "GET" { - req.Header.Set("kbn-version", conn.version.String()) + req.Header.Set("kbn-version", conn.Version.String()) } for header, values := range headers { @@ -209,7 +209,7 @@ func (conn *Connection) Send(method, extraPath string, // Implements RoundTrip interface func (conn *Connection) RoundTrip(r *http.Request) (*http.Response, error) { - return conn.http.Do(r) + return conn.Http.Do(r) } func (client *Client) readVersion() error { @@ -253,13 +253,13 @@ func (client *Client) readVersion() error { return fmt.Errorf("fail to parse kibana version (%v): %+v", versionString, err) } - client.version = *version + client.Version = *version return nil } // GetVersion returns the version read from kibana. The version is not set if // IgnoreVersion was set when creating the client. -func (client *Client) GetVersion() common.Version { return client.version } +func (client *Client) GetVersion() common.Version { return client.Version } func (client *Client) ImportJSON(url string, params url.Values, jsonBody map[string]interface{}) error { diff --git a/libbeat/kibana/client_test.go b/libbeat/kibana/client_test.go index ba30b69496f..a9dbc50d3ef 100644 --- a/libbeat/kibana/client_test.go +++ b/libbeat/kibana/client_test.go @@ -35,7 +35,7 @@ func TestErrorJson(t *testing.T) { conn := Connection{ URL: kibanaTs.URL, - http: http.DefaultClient, + Http: http.DefaultClient, } code, _, err := conn.Request(http.MethodPost, "", url.Values{}, nil, nil) assert.Equal(t, http.StatusOK, code) @@ -50,7 +50,7 @@ func TestErrorBadJson(t *testing.T) { conn := Connection{ URL: kibanaTs.URL, - http: http.DefaultClient, + Http: http.DefaultClient, } code, _, err := conn.Request(http.MethodPost, "", url.Values{}, nil, nil) assert.Equal(t, http.StatusOK, code) @@ -68,7 +68,7 @@ func TestSuccess(t *testing.T) { conn := Connection{ URL: kibanaTs.URL, - http: http.DefaultClient, + Http: http.DefaultClient, } code, _, err := conn.Request(http.MethodPost, "", url.Values{}, http.Header{"foo": []string{"bar"}}, nil) assert.Equal(t, http.StatusOK, code) From 2269dec5bd1cb966b4b0c68f6b0e483c94288ecb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20=C3=81lvarez?= Date: Thu, 6 Jun 2019 10:38:17 +0200 Subject: [PATCH 2/2] make linter happy --- libbeat/kibana/client.go | 6 +++--- libbeat/kibana/client_test.go | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/libbeat/kibana/client.go b/libbeat/kibana/client.go index 3ba0356f13e..300828070f3 100644 --- a/libbeat/kibana/client.go +++ b/libbeat/kibana/client.go @@ -41,7 +41,7 @@ type Connection struct { Username string Password string - Http *http.Client + HTTP *http.Client Version common.Version } @@ -134,7 +134,7 @@ func NewClientWithConfig(config *ClientConfig) (*Client, error) { URL: kibanaURL, Username: username, Password: password, - Http: &http.Client{ + HTTP: &http.Client{ Transport: &http.Transport{ Dial: dialer.Dial, DialTLS: tlsDialer.Dial, @@ -209,7 +209,7 @@ func (conn *Connection) Send(method, extraPath string, // Implements RoundTrip interface func (conn *Connection) RoundTrip(r *http.Request) (*http.Response, error) { - return conn.Http.Do(r) + return conn.HTTP.Do(r) } func (client *Client) readVersion() error { diff --git a/libbeat/kibana/client_test.go b/libbeat/kibana/client_test.go index a9dbc50d3ef..00d736e60d6 100644 --- a/libbeat/kibana/client_test.go +++ b/libbeat/kibana/client_test.go @@ -35,7 +35,7 @@ func TestErrorJson(t *testing.T) { conn := Connection{ URL: kibanaTs.URL, - Http: http.DefaultClient, + HTTP: http.DefaultClient, } code, _, err := conn.Request(http.MethodPost, "", url.Values{}, nil, nil) assert.Equal(t, http.StatusOK, code) @@ -50,7 +50,7 @@ func TestErrorBadJson(t *testing.T) { conn := Connection{ URL: kibanaTs.URL, - Http: http.DefaultClient, + HTTP: http.DefaultClient, } code, _, err := conn.Request(http.MethodPost, "", url.Values{}, nil, nil) assert.Equal(t, http.StatusOK, code) @@ -68,7 +68,7 @@ func TestSuccess(t *testing.T) { conn := Connection{ URL: kibanaTs.URL, - Http: http.DefaultClient, + HTTP: http.DefaultClient, } code, _, err := conn.Request(http.MethodPost, "", url.Values{}, http.Header{"foo": []string{"bar"}}, nil) assert.Equal(t, http.StatusOK, code)