cookie保持优化

This commit is contained in:
zhucheer 2020-01-01 22:09:52 +08:00
parent a8808b54ef
commit f6b988f19b
2 changed files with 30 additions and 4 deletions

View File

@ -25,12 +25,15 @@ type ClientHttp struct {
timeout int
mutex sync.Mutex
cooJar *cookiejar.Jar
cookieData map[string]*http.Cookie
}
type ClientResp struct {
Body []byte // response body
BodyRaw *http.Response
RequestTime time.Duration
CookieData map[string]*http.Cookie
}
func (c *ClientResp) String() string {
@ -49,6 +52,7 @@ func NewClient() *ClientHttp {
header: defaultHeader,
timeout: 10,
errorRaw: "",
cookieData:make(map[string]*http.Cookie),
}
}
@ -119,6 +123,8 @@ func (c *ClientHttp) RunGet(clientUrl string) (clientResp *ClientResp, err error
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(c.timeout)*time.Second)
defer cancel()
req = req.WithContext(ctx)
c.header.Set("Cookie","")
req.Header = c.header
resp, err := defaultClient.Do(req)
@ -127,12 +133,17 @@ func (c *ClientHttp) RunGet(clientUrl string) (clientResp *ClientResp, err error
}
defer resp.Body.Close()
for _,item:=range resp.Cookies(){
c.setCookieData(item.Name, item)
}
requestTime := time.Now().Sub(timeSt)
body, err := ioutil.ReadAll(resp.Body)
clientResp = &ClientResp{
BodyRaw: resp,
Body: body,
RequestTime: requestTime,
CookieData:c.cookieData,
}
return clientResp, err
@ -163,6 +174,7 @@ func (c *ClientHttp) RunPost(clientUrl string) (clientResp *ClientResp, err erro
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(c.timeout)*time.Second)
defer cancel()
req = req.WithContext(ctx)
c.header.Set("Cookie","")
req.Header = c.header
req.Header.Set("Content-Type", c.contentType)
@ -170,15 +182,19 @@ func (c *ClientHttp) RunPost(clientUrl string) (clientResp *ClientResp, err erro
if err != nil {
return nil, err
}
defer resp.Body.Close()
for _,item:=range resp.Cookies(){
c.setCookieData(item.Name, item)
}
requestTime := time.Now().Sub(timeSt)
body, err := ioutil.ReadAll(resp.Body)
clientResp = &ClientResp{
BodyRaw: resp,
Body: body,
RequestTime: requestTime,
CookieData:c.cookieData,
}
return clientResp, err
@ -245,3 +261,11 @@ func (c *ClientHttp) Error() error {
}
return errors.New(c.errorRaw)
}
func (c *ClientHttp) setCookieData(name string, cookie *http.Cookie){
c.mutex.Lock()
defer c.mutex.Unlock()
c.cookieData[name] = cookie
}

View File

@ -86,8 +86,8 @@ func TestRunPost(t *testing.T) {
}
if r.Header.Get("Content-Type") == "application/x-www-form-urlencoded" {
if string(body) != `aid=1002&auth=ss&word=你好` {
t.Error("RunPost WithFormRequest type func error")
if string(body) != `word=你好` {
t.Error("RunPost WithFormRequest type func error 'word=你好' != ", string(body))
}
}
@ -113,7 +113,9 @@ func TestRunPost(t *testing.T) {
client := NewClient().SetTimeout(5)
client.FormParams(params).WithJsonRequest().RunPost(httpServer.URL)
params["word"] = "你好"
params = map[string]interface{}{
"word": "你好",
}
client.FormParams(params).WithFormRequest().RunPost(httpServer.URL)
client.ResetParam().ContentType("application/text").WithBody("123456789").RunPost(httpServer.URL)