diff --git a/debugutil/README.md b/debugutil/README.md index 96a30a9..01703cb 100644 --- a/debugutil/README.md +++ b/debugutil/README.md @@ -1,6 +1,6 @@ # Usage -PrettySprint generates a human readable representation of the value v. +PrettySprint creates a human readable representation of the value v. ## Example ```go @@ -12,6 +12,30 @@ import ( ) func main() { - log.Println(debugutil.PrettySprint([]string{}) + log.Println(debugutil.PrettySprint([]string{})) +} +``` + +PrettyResponseDump creates a human readable representation of the value http.Response. + +## Example + +```go +package main + +import ( + "github.com/donutloop/toolkit/debugutil" + "log" + "net/http" +) + +func main() { + + resp := &http.Response{} + s , err := debugutil.PrettySprintResponse(resp) + if err != nil { + log.Fatal(err) + } + log.Println(s) } ``` \ No newline at end of file diff --git a/debugutil/doc_test.go b/debugutil/doc_test.go index 2253af5..f72e7f4 100644 --- a/debugutil/doc_test.go +++ b/debugutil/doc_test.go @@ -2,7 +2,6 @@ package debugutil_test import ( "fmt" - "github.com/donutloop/toolkit/debugutil" ) @@ -12,4 +11,4 @@ func ExamplePrettySprint() { fmt.Println(str) // Output: []string{ //} -} +} \ No newline at end of file diff --git a/debugutil/http_dump.go b/debugutil/http_dump.go new file mode 100644 index 0000000..b3f316d --- /dev/null +++ b/debugutil/http_dump.go @@ -0,0 +1,42 @@ +package debugutil + +import ( + "bytes" + "encoding/json" + "net/http" + "net/http/httputil" + "strings" +) + + +// PrettyPrintResponse is pretty printing a http response +func PrettySprintResponse(resp *http.Response) (string, error) { + dump, err := PrettyDumpResponse(resp, true) + if err != nil { + return "", err + } + return string(dump), nil +} + +// PrettyDumpResponse is like DumpResponse but dump is pretty formatted. +func PrettyDumpResponse(resp *http.Response, body bool) ([]byte, error) { + + b, err := httputil.DumpResponse(resp, body) + if err != nil { + return nil, err + } + + header := resp.Header.Get("Content-type") + if body && strings.Contains(header, "application/json") && resp.ContentLength > 0 { + buffer := new(bytes.Buffer) + jsonRaw := b[int64(len(b))-resp.ContentLength:] + b = b[:int64(len(b))-resp.ContentLength] + buffer.Write(b) + if err := json.Indent(buffer, jsonRaw, "", "\t"); err != nil { + return nil, err + } + return buffer.Bytes(), nil + } + + return b, nil +} \ No newline at end of file diff --git a/debugutil/http_dump_test.go b/debugutil/http_dump_test.go new file mode 100644 index 0000000..a17f1a1 --- /dev/null +++ b/debugutil/http_dump_test.go @@ -0,0 +1,49 @@ +package debugutil_test + +import ( + "bufio" + "bytes" + "github.com/donutloop/toolkit/debugutil" + "net/http" + "testing" +) + +func TestPrettyDumpResponse(t *testing.T) { + + r := []byte(`HTTP/2.0 200 OK +Content-Length: 1288 +Cache-Control: no-cache, no-store, must-revalidate, max-age=0 +Content-Type: application/json; charset=utf-8 +Date: Wed, 08 May 2019 16:14:10 GMT +Expires: Wed, 08 May 2019 16:14:10 GMT +Server: nginx/1.10.3 (Ubuntu) +Set-Cookie: isLoggedIn=True; Path=/ +Set-Cookie: sessionid_access=701229f3-491c-46c9-bcd8-cbe07cee05da; expires=Thu, 07-May-2020 16:14:10 GMT; httponly; Max-Age=31536000; Path=/ +Vary: Cookie +X-Content-Type-Options: nosniff +X-Frame-Options: SAMEORIGIN +X-Iwoca: 844475 +X-Partner: fincompare +X-State-Key: 8e4db383-2ead-4a47-87df-220432714c47 +X-Xss-Protection: 1; mode=block + +{"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"}]}}} +`) + reader := bufio.NewReader(bytes.NewReader(r)) + req, err := http.NewRequest(http.MethodGet, "/api/resource", nil) + if err != nil { + t.Fatal(err) + } + + resp, err := http.ReadResponse(reader, req) + if err != nil { + t.Fatal(err) + } + + s, err := debugutil.PrettySprintResponse(resp) + if err != nil { + t.Fatal(err) + } + + t.Log(s) +}