请求客户端包添加
This commit is contained in:
parent
0817ed8a11
commit
a942598e5c
|
@ -1,14 +1,16 @@
|
|||
package httpclient
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/cookiejar"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ClientHttp struct {
|
||||
|
@ -17,21 +19,31 @@ type ClientHttp struct {
|
|||
params []byte
|
||||
contentType string
|
||||
errorRaw string
|
||||
timeout int
|
||||
mutex sync.Mutex
|
||||
cooJar map[string]*cookiejar.Jar
|
||||
cooJar *cookiejar.Jar
|
||||
}
|
||||
|
||||
type ClientResp struct {
|
||||
Body []byte // response body
|
||||
BodyRaw *http.Response
|
||||
Body []byte // response body
|
||||
BodyRaw *http.Response
|
||||
RequestTime time.Duration
|
||||
}
|
||||
|
||||
func (c *ClientResp) String() string {
|
||||
return string(c.Body)
|
||||
}
|
||||
|
||||
func (c *ClientResp) GetRequestTime() time.Duration {
|
||||
return c.RequestTime
|
||||
}
|
||||
|
||||
func NewClient() *ClientHttp {
|
||||
return &ClientHttp{
|
||||
contentType: "application/x-www-form-urlencoded",
|
||||
header: make(http.Header),
|
||||
timeout: 10,
|
||||
errorRaw: "",
|
||||
cooJar: make(map[string]*cookiejar.Jar),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -54,40 +66,68 @@ func (c *ClientHttp) ContentType(value string) *ClientHttp {
|
|||
return c
|
||||
}
|
||||
|
||||
func (c *ClientHttp) RunGet(clientUrl string) (clientResp *ClientResp, err error) {
|
||||
var defaultClient = &http.Client{}
|
||||
|
||||
if c.cooJar != nil {
|
||||
defaultClient.Jar = c.cooJar
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("GET", clientUrl, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
timeSt := time.Now()
|
||||
// 超时设置
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(c.timeout)*time.Second)
|
||||
defer cancel()
|
||||
req = req.WithContext(ctx)
|
||||
req.Header = c.header
|
||||
|
||||
resp, err := defaultClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
requestTime := time.Now().Sub(timeSt)
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
clientResp = &ClientResp{
|
||||
BodyRaw: resp,
|
||||
Body: body,
|
||||
RequestTime: requestTime,
|
||||
}
|
||||
|
||||
return clientResp, err
|
||||
}
|
||||
|
||||
func (c *ClientHttp) SetTimeout(timeout int) *ClientHttp {
|
||||
c.timeout = timeout
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *ClientHttp) RequestBody(body []byte) *ClientHttp {
|
||||
c.requestBody = body
|
||||
return c
|
||||
}
|
||||
|
||||
// cookie保持 通过配置请求id将cookie保持
|
||||
// 待续,需要先知道请求url才能配置cookie保持
|
||||
func (c *ClientHttp) HoldCookie(requestId string) *cookiejar.Jar {
|
||||
c.mutex.Lock()
|
||||
defer c.mutex.Unlock()
|
||||
|
||||
if cooJar, ok := c.cooJar[requestId]; ok {
|
||||
return cooJar
|
||||
func (c *ClientHttp) WithCookie() *ClientHttp {
|
||||
if c.cooJar == nil {
|
||||
cooJar, _ := cookiejar.New(nil)
|
||||
c.cooJar = cooJar
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
urlPath, _ := url.Parse("/")
|
||||
|
||||
cooJar, _ := cookiejar.New(nil)
|
||||
|
||||
defaultCookie := &http.Cookie{
|
||||
Name: "orangeRequestId",
|
||||
Value: requestId,
|
||||
Path: "/",
|
||||
}
|
||||
cooJar.SetCookies(urlPath, []*http.Cookie{
|
||||
defaultCookie,
|
||||
})
|
||||
c.cooJar[requestId] = cooJar
|
||||
|
||||
return cooJar
|
||||
// 直接传递body中的参数
|
||||
func (c *ClientHttp) WithBody(bodyStream string) *ClientHttp {
|
||||
c.params = []byte(bodyStream)
|
||||
return c
|
||||
}
|
||||
|
||||
// 参数设置 表单请求支持json和form两种类型
|
||||
func (c *ClientHttp) Params(obj map[string]interface{}) *ClientHttp {
|
||||
func (c *ClientHttp) FormParams(obj map[string]interface{}) *ClientHttp {
|
||||
if strings.Contains(c.contentType, "application/x-www-form-urlencoded") {
|
||||
params := ""
|
||||
val := make([]interface{}, 0, len(obj))
|
||||
|
|
|
@ -2,7 +2,10 @@ package httpclient
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestNewClient(t *testing.T) {
|
||||
|
@ -27,21 +30,69 @@ func TestNewClient(t *testing.T) {
|
|||
"raw": "werwe",
|
||||
}
|
||||
|
||||
client.ContentType("application/x-www-form-urlencoded").Params(params)
|
||||
client.ContentType("application/x-www-form-urlencoded").FormParams(params)
|
||||
if string(client.params) != "aid=1754&raw=werwe" {
|
||||
t.Error("Params func error #1")
|
||||
}
|
||||
client.ContentType("application/json").Params(params)
|
||||
client.ContentType("application/json").FormParams(params)
|
||||
|
||||
if string(client.params) != `{"aid":1754,"raw":"werwe"}` {
|
||||
t.Error("Params func error #2")
|
||||
}
|
||||
|
||||
err := client.ContentType("application/xxx").Params(params).Error()
|
||||
err := client.ContentType("application/xxx").FormParams(params).Error()
|
||||
if err == nil {
|
||||
t.Error("Params func error #3")
|
||||
}
|
||||
|
||||
client.HoldCookie("or01")
|
||||
client.WithBody(`{"aid":1758,"raw":"werwe"}`)
|
||||
if string(client.params) != `{"aid":1758,"raw":"werwe"}` {
|
||||
t.Error("Params func error #4")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunGet(t *testing.T) {
|
||||
httpServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Header.Get("Aid") != "10001" {
|
||||
t.Error("NewClient.Header func error #1")
|
||||
}
|
||||
|
||||
fmt.Fprint(w, "testing client http")
|
||||
}))
|
||||
defer httpServer.Close()
|
||||
|
||||
client := NewClient().
|
||||
Header("User-Agent", "Mozilla/5.0").
|
||||
Header("Aid", "10001")
|
||||
resp, _ := client.RunGet(httpServer.URL)
|
||||
if resp.String() != "testing client http" {
|
||||
t.Error("RunGet func error #1")
|
||||
}
|
||||
|
||||
resp2, _ := client.WithCookie().RunGet(httpServer.URL)
|
||||
if resp2.String() != "testing client http" {
|
||||
t.Error("RunGet func error #2")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequestTime(t *testing.T) {
|
||||
httpServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
time.Sleep(3 * time.Second)
|
||||
fmt.Fprint(w, "testing client http 002")
|
||||
}))
|
||||
|
||||
client := NewClient().SetTimeout(5)
|
||||
err := client.Error()
|
||||
if err != nil {
|
||||
t.Error("Error func error #1")
|
||||
}
|
||||
|
||||
resp, _ := client.RunGet(httpServer.URL)
|
||||
if resp.GetRequestTime().Seconds() < 3 {
|
||||
t.Error("GetRequestTime func error #1")
|
||||
}
|
||||
if resp.String() != "testing client http 002" {
|
||||
t.Error("RunGet func error #1")
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue