Skip to content

Commit

Permalink
API: Add convenience method for accessing the deprecation warnings in…
Browse files Browse the repository at this point in the history
… the response headers

(cherry picked from commit 6c66577)
  • Loading branch information
karmi committed Apr 28, 2020
1 parent 07fbad3 commit 4a10f12
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
12 changes: 12 additions & 0 deletions esapi/esapi.response.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,15 @@ func (r *Response) Status() string {
func (r *Response) IsError() bool {
return r.StatusCode > 299
}

// Warnings returns the deprecation warnings from response headers.
//
func (r *Response) Warnings() []string {
return r.Header["Warning"]
}

// HasWarnings returns true when the response headers contain deprecation warnings.
//
func (r *Response) HasWarnings() bool {
return len(r.Warnings()) > 0
}
16 changes: 16 additions & 0 deletions esapi/esapi_response_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package esapi
import (
"errors"
"io/ioutil"
"net/http"
"strings"
"testing"
)
Expand Down Expand Up @@ -79,4 +80,19 @@ func TestAPIResponse(t *testing.T) {
t.Errorf("Expected error for response: %s", res.Status())
}
})

t.Run("Warnings", func(t *testing.T) {
hdr := http.Header{}
hdr.Add("Warning", "Foo 1")
hdr.Add("Warning", "Foo 2")
res = &Response{StatusCode: 201, Header: hdr}

if !res.HasWarnings() {
t.Errorf("Expected response to have warnings")
}

if len(res.Warnings()) != 2 {
t.Errorf("Expected [2] warnings, got: %d", len(res.Warnings()))
}
})
}

0 comments on commit 4a10f12

Please sign in to comment.