Skip to content

Commit d7a3491

Browse files
committed
API: Add convenience method for accessing the deprecation warnings in the response headers
(cherry picked from commit 6c66577)
1 parent 25467d0 commit d7a3491

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

esapi/esapi.response.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,15 @@ func (r *Response) Status() string {
7878
func (r *Response) IsError() bool {
7979
return r.StatusCode > 299
8080
}
81+
82+
// Warnings returns the deprecation warnings from response headers.
83+
//
84+
func (r *Response) Warnings() []string {
85+
return r.Header["Warning"]
86+
}
87+
88+
// HasWarnings returns true when the response headers contain deprecation warnings.
89+
//
90+
func (r *Response) HasWarnings() bool {
91+
return len(r.Warnings()) > 0
92+
}

esapi/esapi_response_internal_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package esapi
99
import (
1010
"errors"
1111
"io/ioutil"
12+
"net/http"
1213
"strings"
1314
"testing"
1415
)
@@ -79,4 +80,19 @@ func TestAPIResponse(t *testing.T) {
7980
t.Errorf("Expected error for response: %s", res.Status())
8081
}
8182
})
83+
84+
t.Run("Warnings", func(t *testing.T) {
85+
hdr := http.Header{}
86+
hdr.Add("Warning", "Foo 1")
87+
hdr.Add("Warning", "Foo 2")
88+
res = &Response{StatusCode: 201, Header: hdr}
89+
90+
if !res.HasWarnings() {
91+
t.Errorf("Expected response to have warnings")
92+
}
93+
94+
if len(res.Warnings()) != 2 {
95+
t.Errorf("Expected [2] warnings, got: %d", len(res.Warnings()))
96+
}
97+
})
8298
}

0 commit comments

Comments
 (0)