Skip to content

Commit 76c52d2

Browse files
authored
New JSON field in HTTP request collector if any (#1511)
* add new raw_json field to http collector * add unit test
1 parent bece801 commit 76c52d2

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

pkg/collect/http.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ import (
1414
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
1515
"k8s.io/client-go/kubernetes"
1616
"k8s.io/client-go/rest"
17+
"k8s.io/klog/v2"
1718
)
1819

1920
type HTTPResponse struct {
2021
Status int `json:"status"`
2122
Body string `json:"body"`
2223
Headers map[string]string `json:"headers"`
24+
RawJSON json.RawMessage `json:"raw_json,omitempty"`
2325
}
2426

2527
type HTTPError struct {
@@ -127,10 +129,17 @@ func responseToOutput(response *http.Response, err error) ([]byte, error) {
127129
headers[k] = strings.Join(v, ",")
128130
}
129131

132+
var rawJSON json.RawMessage
133+
if err := json.Unmarshal(body, &rawJSON); err != nil {
134+
klog.Infof("failed to unmarshal response body as JSON: %v", err)
135+
rawJSON = json.RawMessage{}
136+
}
137+
130138
output["response"] = HTTPResponse{
131139
Status: response.StatusCode,
132140
Body: string(body),
133141
Headers: headers,
142+
RawJSON: rawJSON,
134143
}
135144
}
136145

pkg/collect/http_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package collect
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"fmt"
7+
"io"
68
"net/http"
79
"net/http/httptest"
810
"strings"
@@ -376,3 +378,64 @@ func Test_parseTimeout(t *testing.T) {
376378
})
377379
}
378380
}
381+
382+
func Test_responseToOutput(t *testing.T) {
383+
tests := []struct {
384+
name string
385+
response *http.Response
386+
err error
387+
want []byte
388+
wantErr bool
389+
}{
390+
{
391+
name: "valid JSON response",
392+
response: &http.Response{
393+
Body: io.NopCloser(bytes.NewBufferString(`{"ok": false, "error": "invalid_auth"}`)),
394+
Header: http.Header{"Content-Type": []string{"application/json; charset=utf-8"}},
395+
StatusCode: http.StatusOK,
396+
},
397+
err: nil,
398+
want: []byte(`
399+
{
400+
"response":
401+
{
402+
"status":200,
403+
"body":"{\"ok\": false, \"error\": \"invalid_auth\"}",
404+
"headers":{"Content-Type":"application/json; charset=utf-8"},
405+
"raw_json":{"ok":false,"error":"invalid_auth"}
406+
}
407+
}`),
408+
wantErr: false,
409+
},
410+
{
411+
name: "invalid JSON response",
412+
response: &http.Response{
413+
Body: io.NopCloser(bytes.NewBufferString(`foobar`)),
414+
Header: http.Header{"Content-Type": []string{"text/html; charset=utf-8"}},
415+
StatusCode: http.StatusOK,
416+
},
417+
err: nil,
418+
want: []byte(`
419+
{
420+
"response":
421+
{
422+
"status":200,
423+
"body":"foobar",
424+
"headers":{"Content-Type":"text/html; charset=utf-8"}
425+
}
426+
}`),
427+
wantErr: false,
428+
},
429+
}
430+
for _, tt := range tests {
431+
t.Run(tt.name, func(t *testing.T) {
432+
got, err := responseToOutput(tt.response, tt.err)
433+
if tt.wantErr {
434+
assert.Error(t, err)
435+
return
436+
}
437+
assert.NoError(t, err)
438+
assert.JSONEq(t, string(got), string(tt.want))
439+
})
440+
}
441+
}

0 commit comments

Comments
 (0)