Skip to content

Commit

Permalink
Set json escape html (#346)
Browse files Browse the repository at this point in the history
* SetJSONNotEscape接口不转义HTML符号

* 完善文档

* 适应老的编译器

* 适应老版编译器
  • Loading branch information
guonaihong authored Dec 18, 2022
1 parent cc27f1a commit faf5d11
Show file tree
Hide file tree
Showing 16 changed files with 234 additions and 126 deletions.
25 changes: 23 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ gout 是go写的http 客户端,为提高工作效率而开发
- [Set the data to the http request body](#Set-the-data-to-the-http-request-body)
- [Parse the response body into a variable](#Parse-the-response-body-into-a-variable)
- [json](#json)
- [Serialize json to request body](#Serialize-json-to-request-body)
- [Parsed http response body in json format](#Parsed-http-response-body-in-json-format)
- [Serialize json to request body](#serialize-json-to-request-body)
- [Parsed http response body in json format](#parsed-http-response-body-in-json-format)
- [Do not escape html characters](#do-not-escape-html-characters)
- [yaml](#yaml)
- [xml](#xml)
- [form-data](#form-data)
Expand Down Expand Up @@ -862,6 +863,26 @@ func main() {
}
```
#### do not escape html characters
* SetJSONNotEscape 和SetJSON唯一的区别就是不转义HTML字符
```go
err := gout.POST(ts.URL).
Debug(true).
SetJSONNotEscape(gout.H{"url": "http://www.com?a=b&c=d"}).
Do()
//> POST / HTTP/1.1
//> Content-Type: application/json
//>
//{
// "url": "http://www.com?a=b&c=d"
//}
//< HTTP/1.1 200 OK
//< Date: Sun, 18 Dec 2022 14:05:21 GMT
//< Content-Length: 0
```
### yaml
* SetYAML() 设置请求http body为yaml
* BindYAML() 解析响应http body里面的yaml到结构体里面
Expand Down
8 changes: 6 additions & 2 deletions color/color_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"sort"
"strconv"
"strings"

"github.com/guonaihong/gout/enjson"
)

// BodyType 区分body的类型
Expand Down Expand Up @@ -44,6 +46,7 @@ const emptyArray = startArray + endArray

// Formatter 是颜色高亮核心结构体
type Formatter struct {
escapeHTML bool
KeyColor *Color // 设置key的颜色
StringColor *Color // 设置string的颜色
BoolColor *Color // 设置bool的颜色
Expand Down Expand Up @@ -73,7 +76,7 @@ func strToObject(all []byte) (interface{}, error) {
}

// NewFormatEncoder 着色json/yaml/xml构造函数
func NewFormatEncoder(r io.Reader, openColor bool, bodyType BodyType) *Formatter {
func NewFormatEncoder(r io.Reader, openColor bool, bodyType BodyType, escapeHTML bool) *Formatter {
// 如果颜色没打开,或者bodyType为txt
if !openColor || bodyType == TxtType {
return nil
Expand All @@ -99,6 +102,7 @@ func NewFormatEncoder(r io.Reader, openColor bool, bodyType BodyType) *Formatter
}

f := &Formatter{
escapeHTML: escapeHTML,
KeyColor: New(true, FgWhite),
StringColor: New(true, FgGreen),
BoolColor: New(true, FgYellow),
Expand Down Expand Up @@ -215,7 +219,7 @@ func (f *Formatter) marshalValue(val interface{}, buf *bytes.Buffer, depth int)

func (f *Formatter) marshalString(str string, buf *bytes.Buffer) {
if !f.RawStrings {
strBytes, _ := json.Marshal(str)
strBytes, _ := enjson.Marshal(str, f.escapeHTML)
str = string(strBytes)
}

Expand Down
4 changes: 2 additions & 2 deletions color/color_core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func Test_ColorCore_NewFormat_Nil(t *testing.T) {
}

for index, d := range data {
assert.Nil(t, NewFormatEncoder(d.r, d.openColor, d.bodyType), fmt.Sprintf("fail index:%d", index))
assert.Nil(t, NewFormatEncoder(d.r, d.openColor, d.bodyType, true), fmt.Sprintf("fail index:%d", index))
}
}

Expand All @@ -49,7 +49,7 @@ func Test_ColorCore_Read(t *testing.T) {
}`

NoColor = false
f := NewFormatEncoder(strings.NewReader(j), true /*open color*/, JSONType)
f := NewFormatEncoder(strings.NewReader(j), true /*open color*/, JSONType, true)
var out bytes.Buffer

_, err := io.Copy(&out, f)
Expand Down
12 changes: 11 additions & 1 deletion dataflow/dataflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/guonaihong/gout/debug"
"github.com/guonaihong/gout/decode"
"github.com/guonaihong/gout/encode"
"github.com/guonaihong/gout/enjson"
"github.com/guonaihong/gout/middler"
"github.com/guonaihong/gout/middleware/rsp/autodecodebody"
"github.com/guonaihong/gout/setting"
Expand Down Expand Up @@ -182,7 +183,16 @@ func (df *DataFlow) SetHeader(obj ...interface{}) *DataFlow {
// SetJSON send json to the http body, Support raw json(string, []byte)/struct/map types
func (df *DataFlow) SetJSON(obj interface{}) *DataFlow {
df.ReqBodyType = "json"
df.Req.bodyEncoder = encode.NewJSONEncode(obj)
df.EscapeHTML = true
df.Req.bodyEncoder = enjson.NewJSONEncode(obj, true)
return df
}

// SetJSON send json to the http body, Support raw json(string, []byte)/struct/map types
// 与SetJSON的区一区别就是不转义HTML里面的标签
func (df *DataFlow) SetJSONNotEscape(obj interface{}) *DataFlow {
df.ReqBodyType = "json"
df.Req.bodyEncoder = enjson.NewJSONEncode(obj, false)
return df
}

Expand Down
23 changes: 23 additions & 0 deletions dataflow/dataflow_json_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package dataflow

import (
"bytes"
"fmt"
"testing"

"github.com/guonaihong/gout/debug"
"github.com/stretchr/testify/assert"
)

func Test_SetJSONNotEscape(t *testing.T) {
ts := createGeneral("")
fmt.Println("url", ts.URL)
var buf bytes.Buffer
//POST(ts.URL).Debug(true).SetJSONNotEscape(map[string]any{"url": "http://www.com?a=b&c=d"}).Do()
POST(ts.URL).Debug(debug.ToWriter(&buf, false)).SetJSONNotEscape(map[string]interface{}{"url": "http://www.com?a=b&c=d"}).Do()
assert.True(t, bytes.Contains(buf.Bytes(), []byte("&")), buf.String())
buf.Reset()
//POST(ts.URL).Debug(true).SetJSON(map[string]any{"url": "http://www.com?a=b&c=d"}).Do()
POST(ts.URL).Debug(debug.ToWriter(&buf, false)).SetJSON(map[string]interface{}{"url": "http://www.com?a=b&c=d"}).Do()
assert.False(t, bytes.Contains(buf.Bytes(), []byte("&")), buf.String())
}
3 changes: 2 additions & 1 deletion dataflow/req.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/guonaihong/gout/debug"
"github.com/guonaihong/gout/decode"
"github.com/guonaihong/gout/encode"
"github.com/guonaihong/gout/encoder"
"github.com/guonaihong/gout/middler"
"github.com/guonaihong/gout/setting"
)
Expand All @@ -31,7 +32,7 @@ type Req struct {
wwwForm []interface{}

// http body
bodyEncoder encode.Encoder
bodyEncoder encoder.Encoder
bodyDecoder []decode.Decoder

// http header
Expand Down
5 changes: 3 additions & 2 deletions debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func ToBodyType(s string) color.BodyType {

// Options Debug mode core data structure
type Options struct {
EscapeHTML bool
Write io.Writer
Debug bool
Color bool
Expand Down Expand Up @@ -106,7 +107,7 @@ func (do *Options) debugPrint(req *http.Request, rsp *http.Response) error {
}

var r = io.Reader(b)
format := color.NewFormatEncoder(r, do.Color, ToBodyType(do.ReqBodyType))
format := color.NewFormatEncoder(r, do.Color, ToBodyType(do.ReqBodyType), do.EscapeHTML)
if format != nil {
r = format
}
Expand All @@ -126,7 +127,7 @@ func (do *Options) debugPrint(req *http.Request, rsp *http.Response) error {
fmt.Fprintf(w, "\r\n\r\n")
// write rsp body
var r = io.Reader(rsp.Body)
format := color.NewFormatEncoder(r, do.Color, ToBodyType(do.RspBodyType))
format := color.NewFormatEncoder(r, do.Color, ToBodyType(do.RspBodyType), do.EscapeHTML)
if format != nil {
r = format
}
Expand Down
6 changes: 4 additions & 2 deletions encode/body.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package encode

import (
"fmt"
"github.com/guonaihong/gout/core"
"io"
"reflect"

"github.com/guonaihong/gout/core"
"github.com/guonaihong/gout/encoder"
)

// BodyEncode body encoder structure
Expand All @@ -13,7 +15,7 @@ type BodyEncode struct {
}

// NewBodyEncode create a new body encoder
func NewBodyEncode(obj interface{}) Encoder {
func NewBodyEncode(obj interface{}) encoder.Encoder {
if obj == nil {
return nil
}
Expand Down
51 changes: 0 additions & 51 deletions encode/json.go

This file was deleted.

58 changes: 0 additions & 58 deletions encode/json_test.go

This file was deleted.

3 changes: 2 additions & 1 deletion encode/protobuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"

"github.com/guonaihong/gout/core"
"github.com/guonaihong/gout/encoder"
"google.golang.org/protobuf/proto"
)

Expand All @@ -14,7 +15,7 @@ type ProtoBufEncode struct {
obj interface{}
}

func NewProtoBufEncode(obj interface{}) Encoder {
func NewProtoBufEncode(obj interface{}) encoder.Encoder {
if nil == obj {
return nil
}
Expand Down
6 changes: 4 additions & 2 deletions encode/xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"bytes"
"encoding/xml"
"errors"
"github.com/guonaihong/gout/core"
"io"

"github.com/guonaihong/gout/core"
"github.com/guonaihong/gout/encoder"
)

var ErrNotXML = errors.New("Not xml data")
Expand All @@ -16,7 +18,7 @@ type XMLEncode struct {
}

// NewXMLEncode create a new xml encoder
func NewXMLEncode(obj interface{}) Encoder {
func NewXMLEncode(obj interface{}) encoder.Encoder {
if obj == nil {
return nil
}
Expand Down
3 changes: 2 additions & 1 deletion encode/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"

"github.com/guonaihong/gout/core"
"github.com/guonaihong/gout/encoder"
"gopkg.in/yaml.v2"
)

Expand All @@ -16,7 +17,7 @@ type YAMLEncode struct {
}

// NewYAMLEncode create a new yaml encoder
func NewYAMLEncode(obj interface{}) Encoder {
func NewYAMLEncode(obj interface{}) encoder.Encoder {
if obj == nil {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion encode/encoder.go → encoder/encoder.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package encode
package encoder

import (
"io"
Expand Down
Loading

0 comments on commit faf5d11

Please sign in to comment.