Skip to content

Commit

Permalink
Fix authentication in HTTP helper (#11353)
Browse files Browse the repository at this point in the history
After the refactor done in #11032, baseData was not being passed to the
HTTP helper, so authentication was not working.

Added a test to avoid regressions here.

Fix #11351
  • Loading branch information
jsoriano committed Mar 21, 2019
1 parent a9f567b commit 4588257
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions metricbeat/helper/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func newHTTPFromConfig(config Config, name string, hostData mb.HostData) (*HTTP,
}

return &HTTP{
hostData: hostData,
client: &http.Client{
Transport: &http.Transport{
Dial: dialer.Dial,
Expand Down
42 changes: 42 additions & 0 deletions metricbeat/helper/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,48 @@ func TestConnectTimeout(t *testing.T) {
checkTimeout(t, h)
}

func TestAuthentication(t *testing.T) {
expectedUser := "elastic"
expectedPassword := "super1234"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user, password, ok := r.BasicAuth()
if !ok || user != expectedUser || password != expectedPassword {
w.WriteHeader(http.StatusUnauthorized)
}
}))
defer ts.Close()

cfg := defaultConfig()

// Unauthorized
hostData := mb.HostData{
URI: ts.URL,
SanitizedURI: ts.URL,
}
h, err := newHTTPFromConfig(cfg, "test", hostData)
require.NoError(t, err)

response, err := h.FetchResponse()
response.Body.Close()
assert.NoError(t, err)
assert.Equal(t, http.StatusUnauthorized, response.StatusCode, "response status code")

// Authorized
hostData = mb.HostData{
URI: ts.URL,
SanitizedURI: ts.URL,
User: expectedUser,
Password: expectedPassword,
}
h, err = newHTTPFromConfig(cfg, "test", hostData)
require.NoError(t, err)

response, err = h.FetchResponse()
response.Body.Close()
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, response.StatusCode, "response status code")
}

func checkTimeout(t *testing.T, h *HTTP) {
t.Helper()

Expand Down

0 comments on commit 4588257

Please sign in to comment.