Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve handling of nil responses and responses with malformed headers #932

Merged
merged 1 commit into from
Mar 19, 2024
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
33 changes: 22 additions & 11 deletions sdk/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,22 +167,33 @@ func (r *Response) Unmarshal(model interface{}) error {
if model == nil {
return fmt.Errorf("model was nil")
}
if r.Response == nil {
return fmt.Errorf("could not unmarshal as the HTTP response was nil")
}

var contentType string
if r.Response.Header != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, we do't need to check if the Header is nil here r.Response.Header.Get won't panic even the Header is nil when that r.Response is not nil.

contentType = strings.ToLower(r.Response.Header.Get("Content-Type"))

contentType := strings.ToLower(r.Header.Get("Content-Type"))
if contentType == "" {
// some APIs (e.g. Storage Data Plane) don't return a content type... so we'll assume from the Accept header
acc, err := accept.FromString(r.Request.Header.Get("Accept"))
if err != nil {
if preferred := acc.FirstChoice(); preferred != nil {
contentType = preferred.ContentType
}
}
if contentType == "" {
// fall back on request media type
contentType = strings.ToLower(r.Request.Header.Get("Content-Type"))
// some APIs (e.g. Storage Data Plane) don't return a content type... so we'll assume from the Accept header
acc, err := accept.FromString(r.Request.Header.Get("Accept"))
if err != nil {
if preferred := acc.FirstChoice(); preferred != nil {
contentType = preferred.ContentType
}
}
if contentType == "" {
// fall back on request media type
contentType = strings.ToLower(r.Request.Header.Get("Content-Type"))
}
}
}

if contentType == "" {
return fmt.Errorf("could not determine Content-Type for response")
}

// Some APIs (e.g. Maintenance) return 200 without a body, don't unmarshal these
if r.ContentLength == 0 && (r.Body == nil || r.Body == http.NoBody) {
return nil
Expand Down
32 changes: 32 additions & 0 deletions sdk/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,38 @@ func TestUnmarshalXml(t *testing.T) {
}
}

func TestUnmarshalNilHeaders(t *testing.T) {
expected := []byte("any payload")
r := &Response{
Response: &http.Response{
Header: nil,
Body: io.NopCloser(bytes.NewReader(expected)),
},
}
var unmarshaled []byte
if err := r.Unmarshal(&unmarshaled); err != nil {
if err.Error() != "could not determine Content-Type for response" {
t.Fatalf("unexpected error when unmarshaling: %+v", err)
}
} else {
t.Fatalf("expected an error but got no error")
}
}

func TestUnmarshalNilResponse(t *testing.T) {
r := &Response{
Response: nil,
}
var unmarshaled = make([]byte, 0)
if err := r.Unmarshal(&unmarshaled); err != nil {
if err.Error() != "could not unmarshal as the HTTP response was nil" {
t.Fatalf("unexpected error when unmarshaling: %+v", err)
}
} else {
t.Fatalf("expected an error but got no error")
}
}

func unmarshalResponse(body io.ReadCloser, unmarshal func(in []byte) error) error {
respBody, err := io.ReadAll(body)
if err != nil {
Expand Down
Loading