Skip to content

Commit

Permalink
Improve Response.String format
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur White committed Aug 2, 2017
1 parent 28a45b2 commit 8d01005
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"os"
"os/exec"
"runtime"
"strconv"
"strings"
"time"
)
Expand Down Expand Up @@ -117,16 +118,36 @@ func openFile(url string) error {
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"
s += "\tHeader\n"
keys := make([]string, 0, len(r.Header))
for k := range r.Header {
if k == "Set-Cookie" {
continue
}
keys = append(keys, k)
}
keyLen := stringMaxLen(keys)
for k, v := range r.Header {
s += "\t\t" + k + ": " + strings.Join(v, ", ") + "\n"
if k == "Set-Cookie" {
continue
}
s += fmt.Sprintf("\t\t%"+strconv.Itoa(keyLen)+"s %s\n", k, strings.Join(v, ", "))
}
}
if len(r.Cookies()) > 0 {
s += "\tCookies:\n"
s += "\tCookies\n"
for _, v := range r.Cookies() {
s += "\t\t" + fmt.Sprint(v)
}
}
return s
}

func stringMaxLen(ss []string) (l int) {
for _, s := range ss {
if len(s) > l {
l = len(s)
}
}
return
}

0 comments on commit 8d01005

Please sign in to comment.