Skip to content

Commit

Permalink
Add Response String and Dump methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur White committed Jul 18, 2017
1 parent 513ba3b commit 0325f51
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 4 deletions.
1 change: 1 addition & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Request interface {
DisableRedirect() Request
ForceMultipart() Request
Do() (*Response, error)
String() string
}

type request struct {
Expand Down
79 changes: 75 additions & 4 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,26 @@
package client

import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"mime"
"net/http"
"os"
"os/exec"
"runtime"
"strings"
"time"
)

// Response is a request response wrapping the original *http.Request.
type Response struct {
*http.Response
}

func (r *Response) String() string {
return fmt.Sprint(r.Response)
}

// Close closes the response body.
// It must be called after body is no longer used.
func (r *Response) Close() error {
Expand Down Expand Up @@ -59,3 +63,70 @@ func (r *Response) JSON(v interface{}) error {
func (r *Response) Path() string {
return r.Request.URL.Path
}

// Dump is for debug purpose.
// It prints the request info, writes the body in a file and opens it in the browser.
// It panics on error.
func (r *Response) Dump() {
log.Println("-", r)

var ext string
exts, _ := mime.ExtensionsByType(r.Header.Get("Content-Type"))
if len(exts) > 0 {
ext = exts[0]
}
name := fmt.Sprintf("response-dump-%d%s", time.Now().Unix(), ext)
f, err := os.Create(name)
if err != nil {
panic(err)
}
defer f.Close()

if strings.HasPrefix(r.Header.Get("Content-Type"), "text/html") {
buf := bytes.NewBufferString("<pre style=\"background:#000;color:#0f0;font:13px/1.2 monospace\">\n\n")
log.New(buf, "", log.LstdFlags).Println("-", r)
buf.WriteString("</pre>")
if _, err = io.Copy(f, buf); err != nil {
panic(err)
}
}

if _, err = io.Copy(f, r.Body); err != nil {
panic(err)
}

openFile(name)
}

func openFile(url string) error {
var cmd string
var args []string
switch runtime.GOOS {
case "windows":
cmd = "cmd"
args = []string{"/c", "start"}
case "darwin":
cmd = "open"
default:
cmd = "xdg-open"
}
args = append(args, url)
return exec.Command(cmd, args...).Start()
}

func (r *Response) String() string {
s := r.Status + " - " + r.Proto + " " + r.Request.Method + " " + r.Request.URL.String() + "\n"
if len(r.Header) > 0 {
s += "\tHeader:\n"
for k, v := range r.Header {
s += "\t\t" + k + ": " + strings.Join(v, ", ") + "\n"
}
}
if len(r.Cookies()) > 0 {
s += "\tCookies:\n"
for _, v := range r.Cookies() {
s += "\t\t" + fmt.Sprint(v)
}
}
return s
}
13 changes: 13 additions & 0 deletions response_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package client

import (
"testing"
)

func TestResponseDump(t *testing.T) {
// res, err := Get("http://example.com").Do()
// if err != nil {
// panic(err)
// }
// res.Dump()
}

0 comments on commit 0325f51

Please sign in to comment.