Skip to content

Commit

Permalink
fix: resolve String() npe bug of response when response is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
yndu13 committed Aug 21, 2023
1 parent 38368f2 commit 3c96299
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
14 changes: 10 additions & 4 deletions sdk/responses/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,22 @@ func (baseResponse *BaseResponse) String() string {
resultBuilder := bytes.Buffer{}
// statusCode
// resultBuilder.WriteString("\n")
resultBuilder.WriteString(fmt.Sprintf("%s %s\n", baseResponse.originHttpResponse.Proto, baseResponse.originHttpResponse.Status))
if baseResponse.originHttpResponse != nil {
resultBuilder.WriteString(fmt.Sprintf("%s %s\n", baseResponse.originHttpResponse.Proto, baseResponse.originHttpResponse.Status))
}
// httpHeaders
//resultBuilder.WriteString("Headers:\n")
for key, value := range baseResponse.httpHeaders {
resultBuilder.WriteString(key + ": " + strings.Join(value, ";") + "\n")
if baseResponse.httpHeaders != nil {
for key, value := range baseResponse.httpHeaders {
resultBuilder.WriteString(key + ": " + strings.Join(value, ";") + "\n")
}
resultBuilder.WriteString("\n")
}
resultBuilder.WriteString("\n")

// content
//resultBuilder.WriteString("Content:\n")
resultBuilder.WriteString(baseResponse.httpContentString + "\n")

return resultBuilder.String()
}

Expand Down
2 changes: 2 additions & 0 deletions sdk/responses/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ func Test_CommonResponse(t *testing.T) {

func Test_CommonResponse_parseFromHttpResponse(t *testing.T) {
r := NewCommonResponse()
assert.Equal(t, "\n", r.String())

res := makeHTTPResponse(200, "")
res.Header.Add("Server", "GitHub.com")
r.parseFromHttpResponse(res)
Expand Down

0 comments on commit 3c96299

Please sign in to comment.