Skip to content

Commit

Permalink
修改debug模式,http header里面有%号不会显示奇怪的信息 (#239)
Browse files Browse the repository at this point in the history
* 修改debug模式,http header里面有%号不会显示奇怪的信息

* add 测试代码
  • Loading branch information
guonaihong authored Jun 29, 2020
1 parent 58786d1 commit eacc1cd
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
8 changes: 7 additions & 1 deletion color/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,13 @@ func (c *Color) Sbluef(format string, a ...interface{}) string {
return c.colorf(format, a...)
}

// Spurplef 紫色函数
// Sblue 蓝色函数
func (c *Color) Sblue(a ...interface{}) string {
c.attr = Blue
return c.color(a...)
}

// Spurplef 紫色函数, TODO删除该函数
func (c *Color) Spurplef(format string, a ...interface{}) string {
c.attr = Purple
return c.colorf(format, a...)
Expand Down
7 changes: 7 additions & 0 deletions color/color_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ func Test_Color_Sbluef(t *testing.T) {
assert.Equal(t, New(false).Sbluef("hello"), "hello")
}

func Test_Color_Sblue(t *testing.T) {
NoColor = false

assert.Equal(t, New(true).Sblue("hello"), "\x1b[34;1mhello\x1b[0m")
assert.Equal(t, New(false).Sblue("hello"), "hello")
}

func Test_Color_color(t *testing.T) {
NoColor = false
assert.Equal(t, New(true, Blue).color("hello"), "\x1b[34;1mhello\x1b[0m")
Expand Down
6 changes: 3 additions & 3 deletions dataflow/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ func (do *DebugOption) debugPrint(req *http.Request, rsp *http.Response) error {

// write request header
for k, v := range req.Header {
fmt.Fprintf(w, "> %s: %s\r\n", cl.Spurplef(k),
cl.Sbluef(strings.Join(v, ",")))
fmt.Fprintf(w, "> %s: %s\r\n", cl.Spurple(k),
cl.Sblue(strings.Join(v, ",")))
}

fmt.Fprint(w, ">\r\n")
Expand Down Expand Up @@ -133,7 +133,7 @@ func (do *DebugOption) debugPrint(req *http.Request, rsp *http.Response) error {

fmt.Fprintf(w, "< %s %s\r\n", rsp.Proto, rsp.Status)
for k, v := range rsp.Header {
fmt.Fprintf(w, "< %s: %s\r\n", cl.Spurplef(k), cl.Sbluef(strings.Join(v, ",")))
fmt.Fprintf(w, "< %s: %s\r\n", cl.Spurple(k), cl.Sblue(strings.Join(v, ",")))
}

fmt.Fprintf(w, "\r\n\r\n")
Expand Down
16 changes: 13 additions & 3 deletions dataflow/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"bytes"
"errors"
"fmt"
"github.com/guonaihong/gout/color"
"github.com/guonaihong/gout/core"
"github.com/stretchr/testify/assert"
"io"
"io/ioutil"
"net/http"
"testing"

"github.com/guonaihong/gout/color"
"github.com/guonaihong/gout/core"
"github.com/stretchr/testify/assert"
)

// 测试resetBodyAndPrint出错
Expand Down Expand Up @@ -120,6 +121,15 @@ func TestDebug_Debug(t *testing.T) {
}()

for index, err := range []error{
// 测试http header里面带%的情况
func() error {
buf.Reset()
err := New().POST(ts.URL).SetHeader(core.H{`h%`: "hello%", "Cookie": `username=admin; token=b7ea3ec643e4ea4871dfe515c559d28bc0d23b6d9d6b22daf206f1de9aff13e51591323199; addinfo=%7B%22chkadmin%22%3A1%2C%22chkarticle%22%3A1%2C%22levelname%22%3A%22%5Cu7ba1%5Cu7406%5Cu5458%22%2C%22userid%22%3A%221%22%2C%22useralias%22%3A%22admin%22%7D; Hm_lvt_12d9f8f1740b76bb88c6691ea1672d8b=1589265192,1589341266,1589717172,1589769747; timezone=8`}).Debug(dbug).Do()
assert.NoError(t, err)
//io.Copy(os.Stdout, &buf)
assert.Equal(t, bytes.Index(buf.Bytes(), []byte("NOVERB")), -1)
return err
}(),
// formdata
func() error {
buf.Reset()
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gout

// Version show version
const Version = "v0.0.11"
const Version = "v0.1.0"

0 comments on commit eacc1cd

Please sign in to comment.