完善单元测试和修改Header一定会修改成大小写的问题.
This commit is contained in:
parent
965d1e6d16
commit
54a47b2073
10
go.mod
Normal file
10
go.mod
Normal file
|
@ -0,0 +1,10 @@
|
|||
module requests
|
||||
|
||||
go 1.12
|
||||
|
||||
require (
|
||||
474420502.top/eson/gjson v1.1.3
|
||||
github.com/tidwall/match v1.0.1 // indirect
|
||||
golang.org/x/net v0.0.0-20190628185345-da137c7871d7
|
||||
golang.org/x/tools v0.0.0-20190716221150-e98af2309876 // indirect
|
||||
)
|
13
go.sum
Normal file
13
go.sum
Normal file
|
@ -0,0 +1,13 @@
|
|||
474420502.top/eson/gjson v1.1.3 h1:SDeD1/SWm1YknuokcPww8ZmsOOguQqFAYLWnQTMMX98=
|
||||
474420502.top/eson/gjson v1.1.3/go.mod h1:95mdr7XPHsGvsGZj/FeQ+iT7mggI6LKc3JT/ZnveebI=
|
||||
github.com/tidwall/match v1.0.1 h1:PnKP62LPNxHKTwvHHZZzdOAOCtsJTjo6dZLCwpKm5xc=
|
||||
github.com/tidwall/match v1.0.1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0+pL9E=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20190628185345-da137c7871d7 h1:rTIdg5QFRR7XCaK4LCjBiPbx8j4DQRpdYMnGn/bJUEU=
|
||||
golang.org/x/net v0.0.0-20190628185345-da137c7871d7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/tools v0.0.0-20190716221150-e98af2309876 h1:XskQVJACjxHVCFlm4GsSEh6ZLclQPLobzuvB4DQYHtw=
|
||||
golang.org/x/tools v0.0.0-20190716221150-e98af2309876/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI=
|
26
response.go
26
response.go
|
@ -4,19 +4,20 @@ import (
|
|||
"bytes"
|
||||
"compress/gzip"
|
||||
"compress/zlib"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Response 响应内容包含http.Response
|
||||
// Response 响应内容包含http.Response 已读
|
||||
type Response struct {
|
||||
DContent string
|
||||
GResponse *http.Response
|
||||
readContent string
|
||||
readResponse *http.Response
|
||||
}
|
||||
|
||||
// FromHTTPResponse 生成Response 从标准http.Response
|
||||
func FromHTTPResponse(resp *http.Response) (*Response, error) {
|
||||
|
||||
var err error
|
||||
// 复制response 返回内容 并且测试是否有解压的需求
|
||||
srcbuf, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
|
@ -27,16 +28,17 @@ func FromHTTPResponse(resp *http.Response) (*Response, error) {
|
|||
content := ""
|
||||
srcReader := bytes.NewReader(srcbuf)
|
||||
|
||||
if r, err := gzip.NewReader(srcReader); err == nil {
|
||||
defer r.Close()
|
||||
buf, err := ioutil.ReadAll(r)
|
||||
var reader io.ReadCloser
|
||||
if reader, err = gzip.NewReader(srcReader); err == nil {
|
||||
defer reader.Close()
|
||||
buf, err := ioutil.ReadAll(reader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
content = string(buf)
|
||||
} else if r, err := zlib.NewReader(srcReader); err == nil {
|
||||
defer r.Close()
|
||||
buf, err := ioutil.ReadAll(r)
|
||||
} else if reader, err = zlib.NewReader(srcReader); err == nil {
|
||||
defer reader.Close()
|
||||
buf, err := ioutil.ReadAll(reader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -45,10 +47,10 @@ func FromHTTPResponse(resp *http.Response) (*Response, error) {
|
|||
content = string(srcbuf)
|
||||
}
|
||||
|
||||
return &Response{DContent: content, GResponse: resp}, nil
|
||||
return &Response{readContent: content, readResponse: resp}, nil
|
||||
}
|
||||
|
||||
// Content 返回解压后的内容
|
||||
func (gresp *Response) Content() string {
|
||||
return gresp.DContent
|
||||
return gresp.readContent
|
||||
}
|
||||
|
|
|
@ -288,8 +288,8 @@ func (ses *Session) SetCookies(u *url.URL, cookies []*http.Cookie) {
|
|||
ses.cookiejar.SetCookies(u, cookies)
|
||||
}
|
||||
|
||||
// Cookies 返回 Cookies
|
||||
func (ses *Session) Cookies(u *url.URL) []*http.Cookie {
|
||||
// GetCookies 返回 Cookies
|
||||
func (ses *Session) GetCookies(u *url.URL) []*http.Cookie {
|
||||
return ses.cookiejar.Cookies(u)
|
||||
}
|
||||
|
||||
|
|
|
@ -83,7 +83,7 @@ func TestSession_Post(t *testing.T) {
|
|||
return
|
||||
}
|
||||
|
||||
if tt.want.MatchString(got.DContent) == false {
|
||||
if tt.want.MatchString(got.readContent) == false {
|
||||
t.Errorf("Metchod = %v, want %v", got, tt.want)
|
||||
}
|
||||
|
||||
|
@ -133,7 +133,7 @@ func TestSession_Setparams(t *testing.T) {
|
|||
return
|
||||
}
|
||||
|
||||
if tt.want.MatchString(got.DContent) == false {
|
||||
if tt.want.MatchString(got.readContent) == false {
|
||||
t.Errorf("Metchod = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
|
@ -176,7 +176,7 @@ func TestSession_PostUploadFile(t *testing.T) {
|
|||
return
|
||||
}
|
||||
|
||||
if tt.want.MatchString(got.DContent) == false {
|
||||
if tt.want.MatchString(got.readContent) == false {
|
||||
t.Errorf("Metchod = %v, want %v", got, tt.want)
|
||||
}
|
||||
|
||||
|
@ -220,7 +220,7 @@ func TestSession_Put(t *testing.T) {
|
|||
return
|
||||
}
|
||||
|
||||
if tt.want.MatchString(got.DContent) == false {
|
||||
if tt.want.MatchString(got.readContent) == false {
|
||||
t.Errorf("Metchod = %v, want %v", got, tt.want)
|
||||
}
|
||||
|
||||
|
@ -264,7 +264,7 @@ func TestSession_Patch(t *testing.T) {
|
|||
return
|
||||
}
|
||||
|
||||
if tt.want.MatchString(got.DContent) == false {
|
||||
if tt.want.MatchString(got.readContent) == false {
|
||||
t.Errorf("Metchod = %v, want %v", got, tt.want)
|
||||
}
|
||||
|
||||
|
@ -330,8 +330,8 @@ func TestSession_SetConfigInsecure(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Error("Unable to make request", err)
|
||||
}
|
||||
if resp.GResponse.StatusCode != 200 {
|
||||
t.Error("Request did not return OK, is ", resp.GResponse.StatusCode)
|
||||
if resp.readResponse.StatusCode != 200 {
|
||||
t.Error("Request did not return OK, is ", resp.readResponse.StatusCode)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -341,14 +341,13 @@ func TestSession_Cookies(t *testing.T) {
|
|||
ses := NewSession()
|
||||
|
||||
t.Run("set cookie", func(t *testing.T) {
|
||||
|
||||
resp, err := ses.Get("http://httpbin.org/cookies/set").AddKVCookie("a", "1").Execute()
|
||||
if err != nil {
|
||||
t.Error("cookies set error", err)
|
||||
}
|
||||
|
||||
if !regexp.MustCompile(`"a": "1"`).MatchString(resp.DContent) {
|
||||
t.Error(resp.DContent)
|
||||
if !regexp.MustCompile(`"a": "1"`).MatchString(resp.readContent) {
|
||||
t.Error(resp.readContent)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -365,8 +364,8 @@ func TestSession_Header(t *testing.T) {
|
|||
t.Error("cookies set error", err)
|
||||
}
|
||||
|
||||
if len(resp.DContent) <= 5000 {
|
||||
t.Error(resp.DContent, len(resp.DContent))
|
||||
if len(resp.readContent) <= 5000 {
|
||||
t.Error(resp.readContent, len(resp.readContent))
|
||||
}
|
||||
|
||||
ses = NewSession()
|
||||
|
@ -375,8 +374,8 @@ func TestSession_Header(t *testing.T) {
|
|||
t.Error("cookies set error", err)
|
||||
}
|
||||
|
||||
if len(resp.DContent) <= 5000 {
|
||||
t.Error(resp.DContent, len(resp.DContent))
|
||||
if len(resp.readContent) <= 5000 {
|
||||
t.Error(resp.readContent, len(resp.readContent))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
// Workflow 工作流
|
||||
// Workflow 工作流 设计点: 这个并不影响Session的属性变化 如 NewWorkflow(ses, url).AddHeader() 对ses没影响
|
||||
type Workflow struct {
|
||||
session *Session
|
||||
ParsedURL *url.URL
|
||||
|
@ -18,11 +18,11 @@ type Workflow struct {
|
|||
}
|
||||
|
||||
// NewWorkflow new and init workflow
|
||||
func NewWorkflow(ses *Session, u string) *Workflow {
|
||||
func NewWorkflow(ses *Session, urlstr string) *Workflow {
|
||||
wf := &Workflow{}
|
||||
wf.SwitchSession(ses)
|
||||
|
||||
wf.SetRawURL(u)
|
||||
wf.SetRawURL(urlstr)
|
||||
|
||||
wf.Body = NewBody()
|
||||
wf.Header = make(http.Header)
|
||||
|
@ -37,7 +37,7 @@ func (wf *Workflow) SwitchSession(ses *Session) {
|
|||
|
||||
// AddHeader 添加头信息 Get方法从Header参数上获取
|
||||
func (wf *Workflow) AddHeader(key, value string) *Workflow {
|
||||
wf.Header.Add(key, value)
|
||||
wf.Header[key] = append(wf.Header[key], value)
|
||||
return wf
|
||||
}
|
||||
|
||||
|
|
60
workflow_test.go
Normal file
60
workflow_test.go
Normal file
|
@ -0,0 +1,60 @@
|
|||
package requests
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"testing"
|
||||
|
||||
"474420502.top/eson/gjson"
|
||||
)
|
||||
|
||||
func TestWorkflow(t *testing.T) {
|
||||
ses := NewSession()
|
||||
|
||||
t.Run("set cookie", func(t *testing.T) {
|
||||
resp, err := ses.Get("http://httpbin.org/cookies/set").AddKVCookie("a", "1").Execute()
|
||||
if err != nil {
|
||||
t.Error("cookies set error", err)
|
||||
}
|
||||
|
||||
if !regexp.MustCompile(`"a": "1"`).MatchString(resp.readContent) {
|
||||
t.Error(resp.readContent)
|
||||
}
|
||||
|
||||
wf := ses.Get("http://httpbin.org/cookies/set")
|
||||
resp, err = wf.AddKVCookie("b", "2").Execute()
|
||||
if err != nil {
|
||||
t.Error("cookies set error", err)
|
||||
}
|
||||
|
||||
result := gjson.Get(resp.readContent, "cookies.a")
|
||||
if result.Exists() {
|
||||
t.Error(resp.readContent)
|
||||
}
|
||||
|
||||
result = gjson.Get(resp.readContent, "cookies.b")
|
||||
if result.Int() != 2 {
|
||||
t.Error(resp.readContent)
|
||||
}
|
||||
|
||||
resp, err = wf.AddKVCookie("a", "3").Execute()
|
||||
results := gjson.GetMany(resp.readContent, "cookies.a", "cookies.b")
|
||||
if results[0].Int() != 3 {
|
||||
t.Error(resp.readContent)
|
||||
}
|
||||
|
||||
if results[1].Int() != 2 {
|
||||
t.Error(resp.readContent)
|
||||
}
|
||||
|
||||
resp, err = wf.AddHeader("XX", "123").SetRawURL("http://httpbin.org/headers").Execute()
|
||||
if err != nil {
|
||||
t.Error("cookies set error", err)
|
||||
}
|
||||
|
||||
// headers 只能是String 表示
|
||||
result = gjson.Get(resp.readContent, "headers.Xx")
|
||||
if result.String() != "123" {
|
||||
t.Error(resp.readContent)
|
||||
}
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue
Block a user