Skip to content

Commit 8dcabc8

Browse files
fix SupportedAPIs in nmagent client to handle non-200 resp correctly (#2623)
Co-authored-by: Ashvin Deodhar <[email protected]>
1 parent f0ff6cf commit 8dcabc8

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

nmagent/client.go

+3
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ func (c *Client) SupportedAPIs(ctx context.Context) ([]string, error) {
200200
defer resp.Body.Close()
201201

202202
var out SupportedAPIsResponseXML
203+
if resp.StatusCode != http.StatusOK {
204+
return out.SupportedApis, die(resp.StatusCode, resp.Header, resp.Body, req.URL.Path)
205+
}
203206
err = xml.NewDecoder(resp.Body).Decode(&out)
204207
if err != nil {
205208
return nil, errors.Wrap(err, "decoding response")

nmagent/client_test.go

+17-5
Original file line numberDiff line numberDiff line change
@@ -503,24 +503,35 @@ func TestNMAgentDeleteNC(t *testing.T) {
503503

504504
func TestNMAgentSupportedAPIs(t *testing.T) {
505505
tests := []struct {
506-
name string
507-
exp []string
508-
expPath string
509-
resp string
510-
shouldErr bool
506+
name string
507+
exp []string
508+
expPath string
509+
resp string
510+
respStatusCode int
511+
shouldErr bool
511512
}{
512513
{
513514
"empty",
514515
nil,
515516
"/machine/plugins?comp=nmagent&type=GetSupportedApis",
516517
"<SupportedAPIsResponseXML></SupportedAPIsResponseXML>",
518+
http.StatusOK,
517519
false,
518520
},
521+
{
522+
"non-200",
523+
nil,
524+
"/machine/plugins?comp=nmagent&type=GetSupportedApis",
525+
"",
526+
http.StatusForbidden,
527+
true,
528+
},
519529
{
520530
"happy",
521531
[]string{"foo"},
522532
"/machine/plugins?comp=nmagent&type=GetSupportedApis",
523533
"<SupportedAPIsResponseXML><type>foo</type></SupportedAPIsResponseXML>",
534+
http.StatusOK,
524535
false,
525536
},
526537
}
@@ -535,6 +546,7 @@ func TestNMAgentSupportedAPIs(t *testing.T) {
535546
RoundTripF: func(req *http.Request) (*http.Response, error) {
536547
gotPath = req.URL.RequestURI()
537548
rr := httptest.NewRecorder()
549+
rr.WriteHeader(test.respStatusCode)
538550
_, _ = rr.WriteString(test.resp)
539551
return rr.Result(), nil
540552
},

0 commit comments

Comments
 (0)