Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions registry/kubernetes/client/watch/body.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ func NewBodyWatcher(req *http.Request, client *http.Client) (Watch, error) {
return nil, errors.Wrap(err, "body watcher failed to make http request")
}

if res.StatusCode != http.StatusOK {
cancel()
//nolint:errcheck
res.Body.Close()
return nil, errors.Errorf("body watcher request failed: %s", res.Status)
}

wr := &bodyWatcher{
ctx: ctx,
results: make(chan Event),
Expand Down
27 changes: 27 additions & 0 deletions registry/kubernetes/client/watch/watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
Expand Down Expand Up @@ -73,3 +74,29 @@ func TestBodyWatcher(t *testing.T) {
w.Stop()
close(ch)
}

func TestBodyWatcherRejectsHTTPError(t *testing.T) {
for _, status := range []int{http.StatusUnauthorized, http.StatusForbidden, http.StatusInternalServerError} {
t.Run(http.StatusText(status), func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(status)
fmt.Fprintln(w, `{"kind":"Status","status":"Failure","message":"watch not permitted"}`)
}))
defer server.Close()
req, err := http.NewRequest(http.MethodGet, server.URL, nil)
if err != nil {
t.Fatal(err)
}
watcher, err := NewBodyWatcher(req, server.Client())
if watcher != nil {
watcher.Stop()
}
if err == nil || !strings.Contains(err.Error(), http.StatusText(status)) {
t.Fatalf("expected an HTTP %d error, got watcher=%v, err=%v", status, watcher != nil, err)
}
if watcher != nil {
t.Fatal("rejected watch returned a watcher")
}
})
}
}
Loading