TODO: 1. CIsWithCookiejar true false, 2. Finish All test to 90% coverage
This commit is contained in:
parent
5ca3e9fc0c
commit
8e2bcbaf02
4
go.mod
4
go.mod
|
@ -4,6 +4,10 @@ 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/gorilla/mux v1.7.3 // 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
|
||||||
)
|
)
|
||||||
|
|
8
go.sum
8
go.sum
|
@ -1,7 +1,15 @@
|
||||||
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/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc=
|
||||||
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/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/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
|
|
29
init_test.go
Normal file
29
init_test.go
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
package requests
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os/exec"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/elazarl/goproxy"
|
||||||
|
)
|
||||||
|
|
||||||
|
var ProxyAddress = "localhost:58080"
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
log.SetFlags(log.Lshortfile | log.LstdFlags)
|
||||||
|
go func() {
|
||||||
|
proxy := goproxy.NewProxyHttpServer()
|
||||||
|
proxy.Verbose = true
|
||||||
|
http.ListenAndServe(ProxyAddress, proxy)
|
||||||
|
}()
|
||||||
|
|
||||||
|
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 ")
|
||||||
|
}
|
||||||
|
|
||||||
|
time.Sleep(time.Millisecond * 100)
|
||||||
|
}
|
|
@ -16,7 +16,7 @@ type Response struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// FromHTTPResponse 生成Response 从标准http.Response
|
// FromHTTPResponse 生成Response 从标准http.Response
|
||||||
func FromHTTPResponse(resp *http.Response, IsDecompressNoAccept bool) (*Response, error) {
|
func FromHTTPResponse(resp *http.Response, isDecompressNoAccept bool) (*Response, error) {
|
||||||
var err error
|
var err error
|
||||||
// 复制response 返回内容 并且测试是否有解压的需求
|
// 复制response 返回内容 并且测试是否有解压的需求
|
||||||
srcbuf, err := ioutil.ReadAll(resp.Body)
|
srcbuf, err := ioutil.ReadAll(resp.Body)
|
||||||
|
@ -27,8 +27,7 @@ func FromHTTPResponse(resp *http.Response, IsDecompressNoAccept bool) (*Response
|
||||||
|
|
||||||
content := ""
|
content := ""
|
||||||
|
|
||||||
if IsDecompressNoAccept {
|
if isDecompressNoAccept {
|
||||||
|
|
||||||
srcReader := bytes.NewReader(srcbuf)
|
srcReader := bytes.NewReader(srcbuf)
|
||||||
var reader io.ReadCloser
|
var reader io.ReadCloser
|
||||||
if reader, err = gzip.NewReader(srcReader); err == nil {
|
if reader, err = gzip.NewReader(srcReader); err == nil {
|
||||||
|
|
|
@ -33,7 +33,7 @@ func TestFromHTTPResponse(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestResponseGzipZip(t *testing.T) {
|
func TestResponseDeflate(t *testing.T) {
|
||||||
ses := NewSession()
|
ses := NewSession()
|
||||||
if wf := ses.Get("http://httpbin.org/get"); wf != nil {
|
if wf := ses.Get("http://httpbin.org/get"); wf != nil {
|
||||||
wf.AddHeader("accept-encoding", "deflate")
|
wf.AddHeader("accept-encoding", "deflate")
|
||||||
|
@ -41,7 +41,9 @@ func TestResponseGzipZip(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
} else {
|
} else {
|
||||||
t.Error(resp.Content())
|
if gjson.Get(resp.Content(), "headers.Accept-Encoding").String() != "deflate" {
|
||||||
|
t.Error("Accept-Encoding != deflate ?")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
15
session.go
15
session.go
|
@ -161,7 +161,7 @@ const (
|
||||||
// CDialTimeout 一个Connect过程的Timeout
|
// CDialTimeout 一个Connect过程的Timeout
|
||||||
CDialTimeout // 支持time.Duration 和 int(秒为单位)
|
CDialTimeout // 支持time.Duration 和 int(秒为单位)
|
||||||
|
|
||||||
// CKeepAlives 默认不KeepAlives, 容易被一直KeepAlives 没关闭链接
|
// CKeepAlives 默认KeepAlives false, 如果默认为true容易被一直KeepAlives, 没关闭链接
|
||||||
CKeepAlives
|
CKeepAlives
|
||||||
|
|
||||||
// CProxy 代理链接
|
// CProxy 代理链接
|
||||||
|
@ -176,12 +176,12 @@ const (
|
||||||
// CTLS 帐号认证
|
// CTLS 帐号认证
|
||||||
CTLS // user pwd
|
CTLS // user pwd
|
||||||
|
|
||||||
// CCookiejar 持久化 CookieJar
|
// CIsWithCookiejar 持久化 CookieJar true or false ; default = true
|
||||||
CCookiejar // true or false ; default = true
|
CIsWithCookiejar
|
||||||
|
|
||||||
// CDecompressNoAccept 解压 当response header 不存在 Accept-Encoding
|
// CIsDecompressNoAccept 解压 当response header 不存在 Accept-Encoding
|
||||||
// 很多特殊情景会不返回Accept-Encoding: Gzip. 如 不按照标准的网站
|
// 很多特殊情景会不返回Accept-Encoding: Gzip. 如 不按照标准的网站
|
||||||
CDecompressNoAccept
|
CIsDecompressNoAccept
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewSession 创建Session
|
// NewSession 创建Session
|
||||||
|
@ -222,11 +222,12 @@ func (ses *Session) SetConfig(typeConfig TypeConfig, values interface{}) {
|
||||||
}
|
}
|
||||||
case CDialTimeout:
|
case CDialTimeout:
|
||||||
// TODO: CDialTimeout CRequestTimeout 与 细节
|
// TODO: CDialTimeout CRequestTimeout 与 细节
|
||||||
case CDecompressNoAccept:
|
case CIsDecompressNoAccept:
|
||||||
ses.Is.isDecompressNoAccept = values.(bool)
|
ses.Is.isDecompressNoAccept = values.(bool)
|
||||||
case CKeepAlives:
|
case CKeepAlives:
|
||||||
|
println(ses.transport.DisableKeepAlives)
|
||||||
ses.transport.DisableKeepAlives = !values.(bool)
|
ses.transport.DisableKeepAlives = !values.(bool)
|
||||||
case CCookiejar:
|
case CIsWithCookiejar:
|
||||||
v := values.(bool)
|
v := values.(bool)
|
||||||
if v {
|
if v {
|
||||||
if ses.client.Jar == nil {
|
if ses.client.Jar == nil {
|
||||||
|
|
|
@ -1,15 +1,15 @@
|
||||||
package requests
|
package requests
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
"time"
|
||||||
|
|
||||||
func init() {
|
"474420502.top/eson/gjson"
|
||||||
log.SetFlags(log.Lshortfile | log.LstdFlags)
|
)
|
||||||
}
|
|
||||||
|
|
||||||
func TestNewSession(t *testing.T) {
|
func TestNewSession(t *testing.T) {
|
||||||
ses := NewSession()
|
ses := NewSession()
|
||||||
|
@ -297,7 +297,7 @@ func TestSession_SetConfig(t *testing.T) {
|
||||||
|
|
||||||
{
|
{
|
||||||
name: "test proxy",
|
name: "test proxy",
|
||||||
args: args{typeConfig: CProxy, values: "http://474420502.top:7070"},
|
args: args{typeConfig: CProxy, values: "http://" + ProxyAddress},
|
||||||
wantErr: false,
|
wantErr: false,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -379,3 +379,51 @@ func TestSession_Header(t *testing.T) {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSession_ConfigEx(t *testing.T) {
|
||||||
|
ses := NewSession()
|
||||||
|
ses.SetConfig(CRequestTimeout, time.Microsecond)
|
||||||
|
resp, err := ses.Get("http://httpbin.org/get").Execute()
|
||||||
|
if err == nil {
|
||||||
|
t.Error(resp)
|
||||||
|
} else {
|
||||||
|
if strings.LastIndex(err.Error(), "Client.Timeout exceeded while awaiting headers") < 0 {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ses.SetConfig(CRequestTimeout, float32(0.0000001))
|
||||||
|
resp, err = ses.Get("http://httpbin.org/get").Execute()
|
||||||
|
if err == nil {
|
||||||
|
t.Error(resp)
|
||||||
|
} else {
|
||||||
|
if strings.LastIndex(err.Error(), "Client.Timeout exceeded while awaiting headers") < 0 {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ses.SetConfig(CKeepAlives, true)
|
||||||
|
ses.SetConfig(CRequestTimeout, int64(5))
|
||||||
|
// jar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
|
||||||
|
u, err := url.Parse("http://httpbin.org")
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
} else {
|
||||||
|
// jar.SetCookies(u, []*http.Cookie{&http.Cookie{Name: "Request", Value: "Cookiejar"}})
|
||||||
|
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"}})
|
||||||
|
resp, err = ses.Get("http://httpbin.org/get").Execute()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if gjson.Get(resp.Content(), "headers.Cookie").String() != "Request=Cookiejar; eson=bad" {
|
||||||
|
t.Error(resp.Content())
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.GetSrcResponse().Header["Connection"][0] != "keep-alive" {
|
||||||
|
t.Error("CKeepAlive is error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -21,7 +21,6 @@ type Workflow struct {
|
||||||
func NewWorkflow(ses *Session, urlstr string) *Workflow {
|
func NewWorkflow(ses *Session, urlstr string) *Workflow {
|
||||||
wf := &Workflow{}
|
wf := &Workflow{}
|
||||||
wf.SwitchSession(ses)
|
wf.SwitchSession(ses)
|
||||||
|
|
||||||
wf.SetRawURL(urlstr)
|
wf.SetRawURL(urlstr)
|
||||||
|
|
||||||
wf.Body = NewBody()
|
wf.Body = NewBody()
|
||||||
|
|
|
@ -57,4 +57,5 @@ func TestWorkflow(t *testing.T) {
|
||||||
t.Error(resp.readContent)
|
t.Error(resp.readContent)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user