From 0fa10daf8ddb47963fcf69b68d99875641e11284 Mon Sep 17 00:00:00 2001 From: eson <474420502@qq.com> Date: Thu, 5 Sep 2019 00:50:44 +0800 Subject: [PATCH] ok requests 6.345s coverage: 64.2% of statements Success: Tests passed. --- go.mod | 4 +- go.sum | 14 ++----- init_test.go | 4 +- response.go | 20 ++++++++++ response_test.go | 8 ++++ session.go | 9 +++-- session_test.go | 97 +++++++++++++++++++++++++++++++++++++++++++++++- 7 files changed, 135 insertions(+), 21 deletions(-) diff --git a/go.mod b/go.mod index 2d02cf4..ac30439 100644 --- a/go.mod +++ b/go.mod @@ -4,10 +4,8 @@ go 1.12 require ( 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/gorilla/mux v1.7.3 // indirect - github.com/pkg/errors v0.8.1 // indirect + github.com/elazarl/goproxy/ext v0.0.0-20190711103511-473e67f1d7d2 // indirect github.com/tidwall/match v1.0.1 // indirect golang.org/x/net v0.0.0-20190902185345-cdfb69ac37fc6fa907650654115ebebb3aae2087 ) diff --git a/go.sum b/go.sum index 459471e..5ab0e60 100644 --- a/go.sum +++ b/go.sum @@ -1,19 +1,11 @@ 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/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/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/go.mod h1:98y8FxUyMjTdJ5eOj/8vzuiVO14/dkJ98NYhEPG8QGY= -github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw= -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/rogpeppe/go-charset v0.0.0-20180617210344-2471d30d28b4/go.mod h1:qgYeAmZ5ZIpBWTGllZSQnw97Dj+woV0toclVaRGI8pc= 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-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= diff --git a/init_test.go b/init_test.go index 6505df8..d51eec8 100644 --- a/init_test.go +++ b/init_test.go @@ -9,7 +9,7 @@ import ( "github.com/elazarl/goproxy" ) -var ProxyAddress = "localhost:58080" +const ProxyAddress = "localhost:58080" func init() { log.SetFlags(log.Lshortfile | log.LstdFlags) @@ -22,7 +22,7 @@ func init() { cmd := exec.Command("/bin/bash", "-c", "docker ps | grep httpbin") _, err := cmd.Output() 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) diff --git a/response.go b/response.go index d8a57b9..df4e0a6 100644 --- a/response.go +++ b/response.go @@ -64,3 +64,23 @@ func (gresp *Response) Content() string { func (gresp *Response) GetSrcResponse() *http.Response { 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 +} diff --git a/response_test.go b/response_test.go index b573a6c..47b7ad1 100644 --- a/response_test.go +++ b/response_test.go @@ -31,6 +31,14 @@ func TestFromHTTPResponse(t *testing.T) { if len(resp.GetSrcResponse().Header) == 0 { 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) { diff --git a/session.go b/session.go index 1ef4729..00b8f7a 100644 --- a/session.go +++ b/session.go @@ -225,7 +225,7 @@ func (ses *Session) SetConfig(typeConfig TypeConfig, values interface{}) { case CIsDecompressNoAccept: ses.Is.isDecompressNoAccept = values.(bool) case CKeepAlives: - println(ses.transport.DisableKeepAlives) + // println(ses.transport.DisableKeepAlives) ses.transport.DisableKeepAlives = !values.(bool) case CIsWithCookiejar: v := values.(bool) @@ -261,13 +261,13 @@ func (ses *Session) SetConfig(typeConfig TypeConfig, values interface{}) { switch v := values.(type) { case *BasicAuth: ses.auth.User = v.User - ses.auth.User = v.Password + ses.auth.Password = v.Password case BasicAuth: ses.auth.User = v.User - ses.auth.User = v.Password + ses.auth.Password = v.Password case []string: ses.auth.User = v[0] - ses.auth.User = v[1] + ses.auth.Password = v[1] case nil: ses.auth = nil } @@ -313,6 +313,7 @@ func (ses *Session) DelCookies(u *url.URL, name string) { for _, c := range cookies { if c.Name == name { c.MaxAge = -1 + break } } ses.SetCookies(u, cookies) diff --git a/session_test.go b/session_test.go index bf26bd4..f1211ef 100644 --- a/session_test.go +++ b/session_test.go @@ -391,6 +391,7 @@ func TestSession_ConfigEx(t *testing.T) { t.Error(err) } } + ses.SetConfig(CRequestTimeout, float32(0.0000001)) resp, err = ses.Get("http://httpbin.org/get").Execute() if err == nil { @@ -409,7 +410,7 @@ func TestSession_ConfigEx(t *testing.T) { t.Error(err) } else { // jar.SetCookies(u, []*http.Cookie{&http.Cookie{Name: "Request", Value: "Cookiejar"}}) - ses.SetConfig(CIsWithCookiejar, true) + ses.SetConfig(CIsWithCookiejar, false) ses.SetConfig(CIsWithCookiejar, true) 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() @@ -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()) + } }