ok requests 6.345s coverage: 64.2% of statements

Success: Tests passed.
This commit is contained in:
eson 2019-09-05 00:50:44 +08:00
parent 8e2bcbaf02
commit 0fa10daf8d
7 changed files with 135 additions and 21 deletions

4
go.mod
View File

@ -4,10 +4,8 @@ go 1.12
require ( require (
474420502.top/eson/gjson v1.1.3 474420502.top/eson/gjson v1.1.3
github.com/ahmetb/go-httpbin v0.0.0-20190607190512-d5c855a2c9eb
github.com/elazarl/goproxy v0.0.0-20190711103511-473e67f1d7d2 github.com/elazarl/goproxy v0.0.0-20190711103511-473e67f1d7d2
github.com/gorilla/mux v1.7.3 // indirect github.com/elazarl/goproxy/ext v0.0.0-20190711103511-473e67f1d7d2 // indirect
github.com/pkg/errors v0.8.1 // indirect
github.com/tidwall/match v1.0.1 // indirect github.com/tidwall/match v1.0.1 // indirect
golang.org/x/net v0.0.0-20190902185345-cdfb69ac37fc6fa907650654115ebebb3aae2087 golang.org/x/net v0.0.0-20190902185345-cdfb69ac37fc6fa907650654115ebebb3aae2087
) )

14
go.sum
View File

@ -1,19 +1,11 @@
474420502.top/eson/gjson v1.1.3 h1:SDeD1/SWm1YknuokcPww8ZmsOOguQqFAYLWnQTMMX98= 474420502.top/eson/gjson v1.1.3 h1:SDeD1/SWm1YknuokcPww8ZmsOOguQqFAYLWnQTMMX98=
474420502.top/eson/gjson v1.1.3/go.mod h1:95mdr7XPHsGvsGZj/FeQ+iT7mggI6LKc3JT/ZnveebI= 474420502.top/eson/gjson v1.1.3/go.mod h1:95mdr7XPHsGvsGZj/FeQ+iT7mggI6LKc3JT/ZnveebI=
github.com/ahmetb/go-httpbin v0.0.0-20190607190512-d5c855a2c9eb h1:MCH9va3v6SaOiA2pLA1YekSGvId2qPvJuJPdMeq/u7Y=
github.com/ahmetb/go-httpbin v0.0.0-20190607190512-d5c855a2c9eb/go.mod h1:iB3NbHoh0P/9AZepPBcH+gM1PhQJGmsres+ZHf72M3k=
github.com/elazarl/goproxy v0.0.0-20190711103511-473e67f1d7d2 h1:aZtFdDNWY/yH86JPR2WX/PN63635VsE/f/nXNPAbYxY= github.com/elazarl/goproxy v0.0.0-20190711103511-473e67f1d7d2 h1:aZtFdDNWY/yH86JPR2WX/PN63635VsE/f/nXNPAbYxY=
github.com/elazarl/goproxy v0.0.0-20190711103511-473e67f1d7d2/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc= github.com/elazarl/goproxy v0.0.0-20190711103511-473e67f1d7d2/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc=
github.com/elazarl/goproxy/ext v0.0.0-20190711103511-473e67f1d7d2 h1:dWB6v3RcOy03t/bUadywsbyrQwCqZeNIEX6M1OtSZOM=
github.com/elazarl/goproxy/ext v0.0.0-20190711103511-473e67f1d7d2/go.mod h1:gNh8nYJoAm43RfaxurUnxr+N1PwuFV3ZMl/efxlIlY8=
github.com/golang/net v0.0.0-20190902185345-cdfb69ac37fc6fa907650654115ebebb3aae2087 h1:haK1T12C0CO79KUdu+ZzLL9+l9BwM9PRkd2/mQqdg8E= github.com/golang/net v0.0.0-20190902185345-cdfb69ac37fc6fa907650654115ebebb3aae2087 h1:haK1T12C0CO79KUdu+ZzLL9+l9BwM9PRkd2/mQqdg8E=
github.com/golang/net v0.0.0-20190902185345-cdfb69ac37fc6fa907650654115ebebb3aae2087/go.mod h1:98y8FxUyMjTdJ5eOj/8vzuiVO14/dkJ98NYhEPG8QGY= github.com/golang/net v0.0.0-20190902185345-cdfb69ac37fc6fa907650654115ebebb3aae2087/go.mod h1:98y8FxUyMjTdJ5eOj/8vzuiVO14/dkJ98NYhEPG8QGY=
github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw= github.com/rogpeppe/go-charset v0.0.0-20180617210344-2471d30d28b4/go.mod h1:qgYeAmZ5ZIpBWTGllZSQnw97Dj+woV0toclVaRGI8pc=
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/tidwall/match v1.0.1 h1:PnKP62LPNxHKTwvHHZZzdOAOCtsJTjo6dZLCwpKm5xc= github.com/tidwall/match v1.0.1 h1:PnKP62LPNxHKTwvHHZZzdOAOCtsJTjo6dZLCwpKm5xc=
github.com/tidwall/match v1.0.1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0+pL9E= 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-20190628185345-da137c7871d7 h1:rTIdg5QFRR7XCaK4LCjBiPbx8j4DQRpdYMnGn/bJUEU=
golang.org/x/net v0.0.0-20190628185345-da137c7871d7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
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=

View File

@ -9,7 +9,7 @@ import (
"github.com/elazarl/goproxy" "github.com/elazarl/goproxy"
) )
var ProxyAddress = "localhost:58080" const ProxyAddress = "localhost:58080"
func init() { func init() {
log.SetFlags(log.Lshortfile | log.LstdFlags) log.SetFlags(log.Lshortfile | log.LstdFlags)
@ -22,7 +22,7 @@ func init() {
cmd := exec.Command("/bin/bash", "-c", "docker ps | grep httpbin") cmd := exec.Command("/bin/bash", "-c", "docker ps | grep httpbin")
_, err := cmd.Output() _, err := cmd.Output()
if err != nil { if err != nil {
log.Println("recommend docker run -p 80:80 kennethreitz/httpbin ") log.Println("recommend 1. docker run -p 80:80 kennethreitz/httpbin \n2. echo \"127.0.0.1 httpbin.org\" >> /etc/hosts")
} }
time.Sleep(time.Millisecond * 100) time.Sleep(time.Millisecond * 100)

View File

@ -64,3 +64,23 @@ func (gresp *Response) Content() string {
func (gresp *Response) GetSrcResponse() *http.Response { func (gresp *Response) GetSrcResponse() *http.Response {
return gresp.readResponse return gresp.readResponse
} }
// GetStatue 获取Statue String
func (gresp *Response) GetStatue() string {
return gresp.readResponse.Status
}
// GetStatueCode 获取Statue int
func (gresp *Response) GetStatueCode() int {
return gresp.readResponse.StatusCode
}
// GetHeader Header map[string][]string
func (gresp *Response) GetHeader() http.Header {
return gresp.readResponse.Header
}
// GetContentLength 获取Content的内容长度, 如果存在 IsDecompressNoAccept 可能是压缩级别的长度, 非GetContent长度
func (gresp *Response) GetContentLength() int64 {
return gresp.readResponse.ContentLength
}

View File

@ -31,6 +31,14 @@ func TestFromHTTPResponse(t *testing.T) {
if len(resp.GetSrcResponse().Header) == 0 { if len(resp.GetSrcResponse().Header) == 0 {
t.Error("esp.GetSrcResponse().Header == nil") t.Error("esp.GetSrcResponse().Header == nil")
} }
if resp.GetStatue() != "200 OK" && resp.GetStatueCode() != 200 {
t.Error(" resp.GetStatue() != 200 OK")
}
if len(resp.GetHeader()["Content-Length"]) != 1 {
t.Error("resp.GetHeader() is error ?")
}
} }
func TestResponseDeflate(t *testing.T) { func TestResponseDeflate(t *testing.T) {

View File

@ -225,7 +225,7 @@ func (ses *Session) SetConfig(typeConfig TypeConfig, values interface{}) {
case CIsDecompressNoAccept: case CIsDecompressNoAccept:
ses.Is.isDecompressNoAccept = values.(bool) ses.Is.isDecompressNoAccept = values.(bool)
case CKeepAlives: case CKeepAlives:
println(ses.transport.DisableKeepAlives) // println(ses.transport.DisableKeepAlives)
ses.transport.DisableKeepAlives = !values.(bool) ses.transport.DisableKeepAlives = !values.(bool)
case CIsWithCookiejar: case CIsWithCookiejar:
v := values.(bool) v := values.(bool)
@ -261,13 +261,13 @@ func (ses *Session) SetConfig(typeConfig TypeConfig, values interface{}) {
switch v := values.(type) { switch v := values.(type) {
case *BasicAuth: case *BasicAuth:
ses.auth.User = v.User ses.auth.User = v.User
ses.auth.User = v.Password ses.auth.Password = v.Password
case BasicAuth: case BasicAuth:
ses.auth.User = v.User ses.auth.User = v.User
ses.auth.User = v.Password ses.auth.Password = v.Password
case []string: case []string:
ses.auth.User = v[0] ses.auth.User = v[0]
ses.auth.User = v[1] ses.auth.Password = v[1]
case nil: case nil:
ses.auth = nil ses.auth = nil
} }
@ -313,6 +313,7 @@ func (ses *Session) DelCookies(u *url.URL, name string) {
for _, c := range cookies { for _, c := range cookies {
if c.Name == name { if c.Name == name {
c.MaxAge = -1 c.MaxAge = -1
break
} }
} }
ses.SetCookies(u, cookies) ses.SetCookies(u, cookies)

View File

@ -391,6 +391,7 @@ func TestSession_ConfigEx(t *testing.T) {
t.Error(err) t.Error(err)
} }
} }
ses.SetConfig(CRequestTimeout, float32(0.0000001)) ses.SetConfig(CRequestTimeout, float32(0.0000001))
resp, err = ses.Get("http://httpbin.org/get").Execute() resp, err = ses.Get("http://httpbin.org/get").Execute()
if err == nil { if err == nil {
@ -409,7 +410,7 @@ func TestSession_ConfigEx(t *testing.T) {
t.Error(err) t.Error(err)
} else { } else {
// jar.SetCookies(u, []*http.Cookie{&http.Cookie{Name: "Request", Value: "Cookiejar"}}) // jar.SetCookies(u, []*http.Cookie{&http.Cookie{Name: "Request", Value: "Cookiejar"}})
ses.SetConfig(CIsWithCookiejar, true) ses.SetConfig(CIsWithCookiejar, false)
ses.SetConfig(CIsWithCookiejar, true) ses.SetConfig(CIsWithCookiejar, true)
ses.SetCookies(u, []*http.Cookie{&http.Cookie{Name: "Request", Value: "Cookiejar"}, &http.Cookie{Name: "eson", Value: "bad"}}) ses.SetCookies(u, []*http.Cookie{&http.Cookie{Name: "Request", Value: "Cookiejar"}, &http.Cookie{Name: "eson", Value: "bad"}})
resp, err = ses.Get("http://httpbin.org/get").Execute() resp, err = ses.Get("http://httpbin.org/get").Execute()
@ -426,4 +427,98 @@ func TestSession_ConfigEx(t *testing.T) {
} }
} }
ses.SetConfig(CProxy, nil)
if u, err := url.Parse("http://" + ProxyAddress); err != nil {
t.Error(err)
} else {
ses.SetConfig(CProxy, u)
}
resp, err = ses.Get("http://httpbin.org/get").Execute()
if err != nil {
t.Error(err)
}
ses.DelCookies(u, "eson")
resp, err = ses.Get("http://httpbin.org/cookies").Execute()
if err != nil {
t.Error(err)
}
cookies := ses.GetCookies(u)
if len(cookies) != 1 && cookies[0].String() != "Request=Cookiejar" {
t.Error("cookies del get error please check it")
}
ses.ClearCookies()
resp, err = ses.Get("http://httpbin.org/cookies").Execute()
if err != nil {
t.Error(err)
}
if gjson.Get(resp.Content(), "cookies").String() != "{}" {
t.Error(resp.Content())
}
}
func TestSession_SetQuery(t *testing.T) {
ses := NewSession()
ses.SetQuery(url.Values{"query": []string{"a", "b"}})
resp, err := ses.Get("http://httpbin.org/get").Execute()
if err != nil {
t.Error(err)
}
if gjson.Get(resp.Content(), "url").String() != "http://httpbin.org/get?query=a&query=b" {
t.Error("url", resp.Content())
}
}
func TestSession_SetHeader(t *testing.T) {
ses := NewSession()
var header http.Header
header = make(http.Header)
header["xx-xx"] = []string{"Header"}
ses.SetHeader(header)
resp, err := ses.Get("http://httpbin.org/headers").Execute()
if err != nil {
t.Error(err)
}
if gjson.Get(resp.Content(), "headers.Xx-Xx").String() != "Header" {
t.Error("Xx-Xx is not exists", resp.Content())
}
if ses.GetHeader()["xx-xx"][0] != "Header" {
t.Error("header error")
}
}
func TestSession_SetBasicAuth(t *testing.T) {
ses := NewSession()
ses.SetConfig(CBasicAuth, &BasicAuth{User: "eson", Password: "123456"})
resp, err := ses.Get("http://httpbin.org/basic-auth/eson/123456").Execute()
if err != nil {
t.Error(err)
}
if resp.GetSrcResponse().StatusCode != 200 {
t.Error("code != 200, code = ", resp.GetStatue())
}
ses.SetConfig(CBasicAuth, &BasicAuth{User: "eson", Password: "12345"})
resp, err = ses.Get("http://httpbin.org/basic-auth/eson/123456").Execute()
if err != nil {
t.Error(err)
}
if resp.GetSrcResponse().StatusCode != 401 {
t.Error("code != 401, code = ", resp.GetStatue())
}
ses.SetConfig(CBasicAuth, []string{"son", "123456"})
resp, err = ses.Get("http://httpbin.org/basic-auth/eson/123456").Execute()
if err != nil {
t.Error(err)
}
if resp.GetSrcResponse().StatusCode != 401 {
t.Error("code != 401, code = ", resp.GetStatue())
}
} }