-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
16 changed files
with
307 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package dataflow | ||
|
||
import ( | ||
"bytes" | ||
"compress/gzip" | ||
"compress/zlib" | ||
"log" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
"github.com/andybalholm/brotli" | ||
"github.com/gin-gonic/gin" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var test_autoDecodeBody_data = "test auto decode boyd function" | ||
|
||
// 测试服务 | ||
func create_AutoDecodeBody() *httptest.Server { | ||
r := gin.New() | ||
r.GET("/gzip", func(c *gin.Context) { | ||
|
||
var buf bytes.Buffer | ||
|
||
zw := gzip.NewWriter(&buf) | ||
// Setting the Header fields is optional. | ||
zw.Name = "a-new-hope.txt" | ||
zw.Comment = "an epic space opera by George Lucas" | ||
zw.ModTime = time.Date(1977, time.May, 25, 0, 0, 0, 0, time.UTC) | ||
_, err := zw.Write([]byte(test_autoDecodeBody_data)) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
if err := zw.Close(); err != nil { | ||
log.Fatal(err) | ||
} | ||
c.Header("Content-Encoding", "gzip") | ||
c.String(200, buf.String()) | ||
}) | ||
|
||
r.GET("/br", func(c *gin.Context) { | ||
|
||
c.Header("Content-Encoding", "br") | ||
var buf bytes.Buffer | ||
w := brotli.NewWriter(&buf) | ||
w.Write([]byte(test_autoDecodeBody_data)) | ||
w.Flush() | ||
w.Close() | ||
c.String(200, buf.String()) | ||
}) | ||
|
||
r.GET("/deflate", func(c *gin.Context) { | ||
|
||
var buf bytes.Buffer | ||
w := zlib.NewWriter(&buf) | ||
w.Write([]byte(test_autoDecodeBody_data)) | ||
w.Close() | ||
c.Header("Content-Encoding", "deflate") | ||
c.String(200, buf.String()) | ||
}) | ||
|
||
r.GET("/compress", func(c *gin.Context) { | ||
c.Header("Content-Encoding", "compress") | ||
}) | ||
return httptest.NewServer(http.HandlerFunc(r.ServeHTTP)) | ||
} | ||
|
||
func Test_AutoDecodeBody(t *testing.T) { | ||
ts := create_AutoDecodeBody() | ||
var err error | ||
for _, path := range []string{"/gzip", "/br", "/deflate"} { | ||
s := "" | ||
if path == "/gzip" { | ||
err = New().GET(ts.URL + path).Debug(true).BindBody(&s).Do() | ||
} else { | ||
err = New().GET(ts.URL + path).AutoDecodeBody().Debug(true).BindBody(&s).Do() | ||
|
||
} | ||
assert.NoError(t, err) | ||
assert.Equal(t, s, test_autoDecodeBody_data) | ||
} | ||
} | ||
|
||
func Test_AutoDecodeBody_Fail(t *testing.T) { | ||
ts := create_AutoDecodeBody() | ||
err := New().GET(ts.URL + "/compress").AutoDecodeBody().Do() | ||
assert.Error(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
## 不建议使用这个目录下的代码 | ||
建议使用如下目录 | ||
```go | ||
github.com/guonaihong/gout/middler | ||
``` | ||
|
||
## 这个目录的代码将在未来4-5个版本迭代之后删除 | ||
请做好迁移 v0.3.1版本开始算起,大约在v0.3.6版本移除 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package middler | ||
|
||
import "net/http" | ||
|
||
type Do interface { | ||
Do(*http.Request) (*http.Response, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package middler | ||
|
||
import "net/http" | ||
|
||
type RequestMiddlerFunc func(req *http.Request) error | ||
|
||
type RequestMiddler interface { | ||
ModifyRequest(req *http.Request) error | ||
} | ||
|
||
func (f RequestMiddlerFunc) ModifyRequest(req *http.Request) error { | ||
return f(req) | ||
} | ||
|
||
// WithRequestMiddlerFunc 是创建一个 RequestMiddler 的helper | ||
// 如果我们只需要简单的逻辑,只关注闭包本身,则可以使用这个helper快速创建一个 RequestMiddler | ||
func WithRequestMiddlerFunc(f RequestMiddlerFunc) RequestMiddler { | ||
return f | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package middler | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
type ResponseMiddlerFunc func(response *http.Response) error | ||
|
||
// ResponseMiddler 响应拦截器 | ||
type ResponseMiddler interface { | ||
ModifyResponse(response *http.Response) error | ||
} | ||
|
||
func (f ResponseMiddlerFunc) ModifyResponse(response *http.Response) error { | ||
return f(response) | ||
} | ||
|
||
// WithResponseMiddlerFunc 是创建一个 ResponseMiddler 的helper | ||
// 如果我们只需要简单的逻辑,只关注闭包本身,则可以使用这个helper快速创建一个 ResponseMiddler | ||
func WithResponseMiddlerFunc(f ResponseMiddlerFunc) ResponseMiddler { | ||
return f | ||
} |
Oops, something went wrong.