Skip to content

Commit

Permalink
Merge pull request #385 from TaoZou1/httpbody
Browse files Browse the repository at this point in the history
Fix dumping http response error
  • Loading branch information
TaoZou1 authored Nov 16, 2023
2 parents 93dc5c6 + 135b6ee commit 706a0dc
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
1 change: 1 addition & 0 deletions pkg/controllers/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,4 @@ func NodeIsMaster(node *v1.Node) bool {
}
return false
}

6 changes: 3 additions & 3 deletions pkg/nsx/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package nsx
import (
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -275,13 +275,13 @@ func (ep *Endpoint) createAuthSession(certProvider auth.ClientCertProvider, toke
log.Error(err, "session creation failed", "endpoint", u.Host)
return err
}
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
err = fmt.Errorf("session creation failed, unexpected status code %d", resp.StatusCode)
}
if err != nil {
log.Error(err, "session creation failed", "endpoint", u.Host, "statusCode", resp.StatusCode, "headerDate", resp.Header["Date"], "body", body)
log.Error(err, "session creation failed", "endpoint", u.Host, "statusCode", resp.StatusCode, "headerDate", resp.Header["Date"], "body", string(body))
return err
}
tokens, ok := resp.Header["X-Xsrf-Token"]
Expand Down
14 changes: 7 additions & 7 deletions pkg/nsx/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"reflect"
"sort"
Expand Down Expand Up @@ -233,20 +233,20 @@ func httpErrortoNSXError(detail *ErrorDetail) NsxError {
}

func HandleHTTPResponse(response *http.Response, result interface{}, debug bool) (error, []byte) {
body, err := io.ReadAll(response.Body)
defer response.Body.Close()
if !(response.StatusCode == http.StatusOK || response.StatusCode == http.StatusAccepted) {
err := errors.New("received HTTP Error")
log.Error(err, "handle http response", "status", response.StatusCode, "requestUrl", response.Request.URL, "response", response)
log.Error(err, "handle http response", "status", response.StatusCode, "requestUrl", response.Request.URL, "response body", string(body))
return err, nil
}
if err != nil || body == nil {
return err, body
}
if result == nil {
return nil, nil
}

body, err := ioutil.ReadAll(response.Body)
defer response.Body.Close()
if err != nil || body == nil {
return err, body
}
if debug {
log.V(2).Info("received HTTP response", "response", string(body))
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/nsx/util/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -169,6 +170,7 @@ func TestVCClient_handleHTTPResponse(t *testing.T) {
response.Request = &http.Request{}
response.Request.URL = &url.URL{Host: "10.0.0.1"}
response.StatusCode = 301
response.Body = io.NopCloser(strings.NewReader("Hello, World!"))
var sessionData map[string]string

// http status code > 300
Expand Down

0 comments on commit 706a0dc

Please sign in to comment.