dumpHTTP - dump HTTP request and response.
(req *http.Request, resp *http.Response)
| 555 | |
| 556 | // dumpHTTP - dump HTTP request and response. |
| 557 | func (c *Client) dumpHTTP(req *http.Request, resp *http.Response) error { |
| 558 | // Starts http dump. |
| 559 | _, err := fmt.Fprintln(c.traceOutput, "---------START-HTTP---------") |
| 560 | if err != nil { |
| 561 | return err |
| 562 | } |
| 563 | |
| 564 | // Filter out Signature field from Authorization header. |
| 565 | origAuth := req.Header.Get("Authorization") |
| 566 | if origAuth != "" { |
| 567 | req.Header.Set("Authorization", redactSignature(origAuth)) |
| 568 | } |
| 569 | |
| 570 | // Only display request header. |
| 571 | reqTrace, err := httputil.DumpRequestOut(req, false) |
| 572 | if err != nil { |
| 573 | return err |
| 574 | } |
| 575 | |
| 576 | // Write request to trace output. |
| 577 | _, err = fmt.Fprint(c.traceOutput, string(reqTrace)) |
| 578 | if err != nil { |
| 579 | return err |
| 580 | } |
| 581 | |
| 582 | // Only display response header. |
| 583 | var respTrace []byte |
| 584 | |
| 585 | // For errors we make sure to dump response body as well. |
| 586 | if resp.StatusCode != http.StatusOK && |
| 587 | resp.StatusCode != http.StatusPartialContent && |
| 588 | resp.StatusCode != http.StatusNoContent { |
| 589 | respTrace, err = httputil.DumpResponse(resp, true) |
| 590 | if err != nil { |
| 591 | return err |
| 592 | } |
| 593 | } else { |
| 594 | respTrace, err = httputil.DumpResponse(resp, false) |
| 595 | if err != nil { |
| 596 | return err |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | // Write response to trace output. |
| 601 | _, err = fmt.Fprint(c.traceOutput, strings.TrimSuffix(string(respTrace), "\r\n")) |
| 602 | if err != nil { |
| 603 | return err |
| 604 | } |
| 605 | |
| 606 | // Ends the http dump. |
| 607 | _, err = fmt.Fprintln(c.traceOutput, "---------END-HTTP---------") |
| 608 | if err != nil { |
| 609 | return err |
| 610 | } |
| 611 | |
| 612 | // Returns success. |
| 613 | return nil |
| 614 | } |
no test coverage detected