Skip to content

Commit 5508979

Browse files
authored
Merge pull request #41 from donutloop/feature/prettydumpresponse
Feature/prettydumpresponse
2 parents 8efa542 + 43940bd commit 5508979

File tree

4 files changed

+118
-4
lines changed

4 files changed

+118
-4
lines changed

debugutil/README.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Usage
22

3-
PrettySprint generates a human readable representation of the value v.
3+
PrettySprint creates a human readable representation of the value v.
44

55
## Example
66
```go
@@ -12,6 +12,30 @@ import (
1212
)
1313

1414
func main() {
15-
log.Println(debugutil.PrettySprint([]string{})
15+
log.Println(debugutil.PrettySprint([]string{}))
16+
}
17+
```
18+
19+
PrettyResponseDump creates a human readable representation of the value http.Response.
20+
21+
## Example
22+
23+
```go
24+
package main
25+
26+
import (
27+
"github.com/donutloop/toolkit/debugutil"
28+
"log"
29+
"net/http"
30+
)
31+
32+
func main() {
33+
34+
resp := &http.Response{}
35+
s , err := debugutil.PrettySprintResponse(resp)
36+
if err != nil {
37+
log.Fatal(err)
38+
}
39+
log.Println(s)
1640
}
1741
```

debugutil/doc_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package debugutil_test
22

33
import (
44
"fmt"
5-
65
"github.com/donutloop/toolkit/debugutil"
76
)
87

@@ -12,4 +11,4 @@ func ExamplePrettySprint() {
1211
fmt.Println(str)
1312
// Output: []string{
1413
//}
15-
}
14+
}

debugutil/http_dump.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package debugutil
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"net/http"
7+
"net/http/httputil"
8+
"strings"
9+
)
10+
11+
12+
// PrettyPrintResponse is pretty printing a http response
13+
func PrettySprintResponse(resp *http.Response) (string, error) {
14+
dump, err := PrettyDumpResponse(resp, true)
15+
if err != nil {
16+
return "", err
17+
}
18+
return string(dump), nil
19+
}
20+
21+
// PrettyDumpResponse is like DumpResponse but dump is pretty formatted.
22+
func PrettyDumpResponse(resp *http.Response, body bool) ([]byte, error) {
23+
24+
b, err := httputil.DumpResponse(resp, body)
25+
if err != nil {
26+
return nil, err
27+
}
28+
29+
header := resp.Header.Get("Content-type")
30+
if body && strings.Contains(header, "application/json") && resp.ContentLength > 0 {
31+
buffer := new(bytes.Buffer)
32+
jsonRaw := b[int64(len(b))-resp.ContentLength:]
33+
b = b[:int64(len(b))-resp.ContentLength]
34+
buffer.Write(b)
35+
if err := json.Indent(buffer, jsonRaw, "", "\t"); err != nil {
36+
return nil, err
37+
}
38+
return buffer.Bytes(), nil
39+
}
40+
41+
return b, nil
42+
}

debugutil/http_dump_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package debugutil_test
2+
3+
import (
4+
"bufio"
5+
"bytes"
6+
"github.com/donutloop/toolkit/debugutil"
7+
"net/http"
8+
"testing"
9+
)
10+
11+
func TestPrettyDumpResponse(t *testing.T) {
12+
13+
r := []byte(`HTTP/2.0 200 OK
14+
Content-Length: 1288
15+
Cache-Control: no-cache, no-store, must-revalidate, max-age=0
16+
Content-Type: application/json; charset=utf-8
17+
Date: Wed, 08 May 2019 16:14:10 GMT
18+
Expires: Wed, 08 May 2019 16:14:10 GMT
19+
Server: nginx/1.10.3 (Ubuntu)
20+
Set-Cookie: isLoggedIn=True; Path=/
21+
Set-Cookie: sessionid_access=701229f3-491c-46c9-bcd8-cbe07cee05da; expires=Thu, 07-May-2020 16:14:10 GMT; httponly; Max-Age=31536000; Path=/
22+
Vary: Cookie
23+
X-Content-Type-Options: nosniff
24+
X-Frame-Options: SAMEORIGIN
25+
X-Iwoca: 844475
26+
X-Partner: fincompare
27+
X-State-Key: 8e4db383-2ead-4a47-87df-220432714c47
28+
X-Xss-Protection: 1; mode=block
29+
30+
{"data": {"partner": {"verified_data": {"people": [{"identity_document_checks": [{"status": "passed", "file_links": [{"file_type": "photo", "link": "https://static.iwoca.com/assets/iwoca.4c17fef7de62.png"}], "check_id": "REFERENCE_0001", "datetime": "2017-06-12T14:05:51.666Z", "identity_document_type": "passport", "document_issuing_country": "gb", "provider_name": "test"}], "uid": "6cf7319e-f9ec-4038-ba4f-3561a6097484"}]}}, "state_key": "8e4db383-2ead-4a47-87df-220432714c47", "schema_version": "v1", "application": {"company": {"last_12_months_turnover": {"amount": 700000, "datetime": "2016-10-12T14:05:51.666Z"}, "type": "gmbh", "company_number": "01111112", "bank_details": {"iban": "DE89370400440532013000"}}, "requested_products": {"credit_facility": {"approval": {"amount": 15000}}}, "people": [{"residential_addresses": [{"town": "Ely", "uid": "cf9aa203-4e0c-4d7f-b42b-90c7b3d193d3", "house_number": "286", "date_from": "2014-02-03", "street_line_1": "Idverifier St", "postcode": "CB62AG"}], "last_name": "Norton", "uid": "6cf7319e-f9ec-4038-ba4f-3561a6097484", "roles": ["applicant", "shareholder", "guarantor", "director"], "title": "herr", "first_name": "Ervin", "privacy_policy": {"agreed": true, "datetime": "2016-10-12T14:05:51.666Z"}, "date_of_birth": "1980-01-01"}]}}}
31+
`)
32+
reader := bufio.NewReader(bytes.NewReader(r))
33+
req, err := http.NewRequest(http.MethodGet, "/api/resource", nil)
34+
if err != nil {
35+
t.Fatal(err)
36+
}
37+
38+
resp, err := http.ReadResponse(reader, req)
39+
if err != nil {
40+
t.Fatal(err)
41+
}
42+
43+
s, err := debugutil.PrettySprintResponse(resp)
44+
if err != nil {
45+
t.Fatal(err)
46+
}
47+
48+
t.Log(s)
49+
}

0 commit comments

Comments
 (0)