-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c73a5f4
commit b0e10ec
Showing
3 changed files
with
102 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package request | ||
|
||
import ( | ||
"io" | ||
"io/ioutil" | ||
"net/http" | ||
|
||
api "github.com/guonaihong/gout/interface" | ||
) | ||
|
||
//上传进度条 | ||
type progressBar struct { | ||
callback func(currBytes, totalBytes int) | ||
r io.Reader | ||
currBytes int | ||
totalBytes int | ||
} | ||
|
||
func (g *progressBar) Read(p []byte) (n int, err error) { | ||
n, err = g.r.Read(p) | ||
g.currBytes += n | ||
if n > 0 && g.callback != nil { | ||
g.callback(g.currBytes, g.totalBytes) | ||
} | ||
|
||
return | ||
} | ||
|
||
func (g *progressBar) ModifyRequest(req *http.Request) error { | ||
g.r = req.Body | ||
req.Body = ioutil.NopCloser(g) | ||
g.totalBytes = int(req.ContentLength) | ||
return nil | ||
} | ||
|
||
func ProgressBar(callback func(currBytes, totalBytes int)) api.RequestMiddler { | ||
return &progressBar{callback: callback} | ||
} |
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,46 @@ | ||
package request | ||
|
||
import ( | ||
"io" | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/guonaihong/gout" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type readWait struct { | ||
r io.Reader | ||
} | ||
|
||
func (r *readWait) Read(p []byte) (n int, err error) { | ||
n, err = r.r.Read(p[:512]) | ||
return | ||
} | ||
|
||
func createProgressBarServer() *httptest.Server { | ||
r := gin.New() | ||
r.POST("/", func(c *gin.Context) { | ||
r := &readWait{r: c.Request.Body} | ||
_, _ = ioutil.ReadAll(r) | ||
|
||
c.String(200, "hello") | ||
}) | ||
|
||
return httptest.NewServer(http.HandlerFunc(r.ServeHTTP)) | ||
} | ||
|
||
func TestProgressBar(t *testing.T) { | ||
ts := createProgressBarServer() | ||
call := false | ||
gout.POST(ts.URL).RequestUse(ProgressBar(func(currBytes, totalBytes int) { | ||
call = true | ||
//fmt.Printf("%d:%d\n", currBytes, totalBytes) | ||
})).SetBody(strings.Repeat("1", 100000)).Do() | ||
|
||
assert.True(t, call) | ||
} |