2018-10-17 06:25:17 +00:00
|
|
|
package requests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2019-09-04 10:29:52 +00:00
|
|
|
"net/url"
|
2018-10-17 06:25:17 +00:00
|
|
|
"regexp"
|
2019-09-04 10:29:52 +00:00
|
|
|
"strings"
|
2018-10-17 06:25:17 +00:00
|
|
|
"testing"
|
2019-09-04 10:29:52 +00:00
|
|
|
"time"
|
2018-10-17 06:25:17 +00:00
|
|
|
|
2019-09-04 10:29:52 +00:00
|
|
|
"474420502.top/eson/gjson"
|
|
|
|
)
|
2018-10-17 10:38:04 +00:00
|
|
|
|
2018-10-17 06:25:17 +00:00
|
|
|
func TestNewSession(t *testing.T) {
|
|
|
|
ses := NewSession()
|
|
|
|
if ses == nil {
|
|
|
|
t.Error("session create fail, value is nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSession_Get(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
client *http.Client
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
url string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Get test",
|
|
|
|
fields: fields{client: &http.Client{}},
|
|
|
|
args: args{url: "http://httpbin.org/get"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
ses := &Session{
|
|
|
|
client: tt.fields.client,
|
|
|
|
}
|
2018-10-17 10:38:04 +00:00
|
|
|
resp, err := ses.Get(tt.args.url).Execute()
|
2018-10-17 06:25:17 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2019-09-04 02:16:33 +00:00
|
|
|
if len(resp.Content()) <= 150 {
|
2018-10-17 06:25:17 +00:00
|
|
|
t.Error(resp.Content())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSession_Post(t *testing.T) {
|
|
|
|
type args struct {
|
2018-10-17 10:38:04 +00:00
|
|
|
params []interface{}
|
2018-10-17 06:25:17 +00:00
|
|
|
}
|
2018-10-17 10:38:04 +00:00
|
|
|
|
2018-10-17 06:25:17 +00:00
|
|
|
tests := []struct {
|
2018-10-17 10:38:04 +00:00
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want *regexp.Regexp
|
2018-10-17 06:25:17 +00:00
|
|
|
}{
|
|
|
|
{
|
2018-10-17 10:38:04 +00:00
|
|
|
name: "Post test",
|
|
|
|
args: args{params: nil},
|
|
|
|
want: regexp.MustCompile(`"form": \{\}`),
|
2018-10-17 06:25:17 +00:00
|
|
|
},
|
|
|
|
{
|
2018-10-17 10:38:04 +00:00
|
|
|
name: "Post data",
|
|
|
|
args: args{params: []interface{}{[]byte("a=1&b=2")}},
|
|
|
|
want: regexp.MustCompile(`"form": \{[^"]+"a": "1"[^"]+"b": "2"[^\}]+\}`),
|
2018-10-17 06:25:17 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2018-10-17 10:38:04 +00:00
|
|
|
ses := NewSession()
|
2019-09-03 19:08:28 +00:00
|
|
|
got, err := ses.Post("http://httpbin.org/post").SetBodyAuto(tt.args.params...).Execute()
|
2018-10-17 10:38:04 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Metchod error = %v", err)
|
2018-10-17 06:25:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-17 18:20:09 +00:00
|
|
|
if tt.want.MatchString(got.readContent) == false {
|
2018-10-17 10:38:04 +00:00
|
|
|
t.Errorf("Metchod = %v, want %v", got, tt.want)
|
2018-10-17 06:25:17 +00:00
|
|
|
}
|
2018-10-17 10:38:04 +00:00
|
|
|
|
2018-10-17 06:25:17 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSession_Setparams(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
client *http.Client
|
2018-10-23 09:08:50 +00:00
|
|
|
params *Body
|
2018-10-17 06:25:17 +00:00
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
params []interface{}
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
want *regexp.Regexp
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
2018-10-17 10:38:04 +00:00
|
|
|
name: "test Setparams",
|
|
|
|
args: args{params: []interface{}{map[string]string{"a": "1", "b": "2"}}},
|
|
|
|
want: regexp.MustCompile(`"form": \{[^"]+"a": "1"[^"]+"b": "2"[^\}]+\}`),
|
2018-10-17 06:25:17 +00:00
|
|
|
},
|
|
|
|
{
|
2018-10-17 10:38:04 +00:00
|
|
|
name: "test json",
|
|
|
|
args: args{params: []interface{}{`{"a":"1","b":"2"}`, TypeJSON}},
|
|
|
|
want: regexp.MustCompile(`"json": \{[^"]+"a": "1"[^"]+"b": "2"[^\}]+\}`),
|
2018-10-17 06:25:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test xml",
|
2018-11-19 10:07:37 +00:00
|
|
|
fields: fields{client: &http.Client{}, params: NewBody()},
|
2018-10-17 06:25:17 +00:00
|
|
|
args: args{params: []interface{}{`<request><parameters><password>test</password></parameters></request>`, TypeXML}},
|
|
|
|
want: regexp.MustCompile(`"data": "<request><parameters><password>test</password></parameters></request>"`),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
ses := NewSession()
|
|
|
|
|
2019-09-03 19:08:28 +00:00
|
|
|
got, err := ses.Post("http://httpbin.org/post").SetBodyAuto(tt.args.params...).Execute()
|
2018-10-17 06:25:17 +00:00
|
|
|
if (err != nil) != tt.wantErr {
|
2018-10-17 10:38:04 +00:00
|
|
|
t.Errorf("Metchod error = %v, wantErr %v", err, tt.wantErr)
|
2018-10-17 06:25:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-17 18:20:09 +00:00
|
|
|
if tt.want.MatchString(got.readContent) == false {
|
2018-10-17 10:38:04 +00:00
|
|
|
t.Errorf("Metchod = %v, want %v", got, tt.want)
|
2018-10-17 06:25:17 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSession_PostUploadFile(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
params interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want *regexp.Regexp
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "test post uploadfile glob",
|
|
|
|
args: args{params: "tests/*.js"},
|
|
|
|
want: regexp.MustCompile(`"file0": "data:application/octet-stream;base64`),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test post uploadfile only one file",
|
|
|
|
args: args{params: "tests/json.file"},
|
|
|
|
want: regexp.MustCompile(`"file0": "json.file.+jsonjsonjsonjson"`),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test post uploadfile key values",
|
|
|
|
args: args{params: map[string]string{"a": "32"}},
|
|
|
|
want: regexp.MustCompile(`"a": "32"`),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
ses := NewSession()
|
2019-09-03 19:08:28 +00:00
|
|
|
got, err := ses.Post("http://httpbin.org/post").SetBodyAuto(tt.args.params, TypeFormData).Execute()
|
2018-10-17 06:25:17 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2018-10-17 10:38:04 +00:00
|
|
|
t.Errorf("Metchod error = %v", err)
|
2018-10-17 06:25:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-17 18:20:09 +00:00
|
|
|
if tt.want.MatchString(got.readContent) == false {
|
2018-10-17 10:38:04 +00:00
|
|
|
t.Errorf("Metchod = %v, want %v", got, tt.want)
|
2018-10-17 06:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSession_Put(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
params interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want *regexp.Regexp
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "test post uploadfile glob",
|
|
|
|
args: args{params: "tests/*.js"},
|
|
|
|
want: regexp.MustCompile(`"file0": "data:application/octet-stream;base64`),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test post uploadfile only one file",
|
|
|
|
args: args{params: "tests/json.file"},
|
|
|
|
want: regexp.MustCompile(`"file0": "json.file.+jsonjsonjsonjson"`),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test post uploadfile key values",
|
|
|
|
args: args{params: map[string]string{"a": "32"}},
|
|
|
|
want: regexp.MustCompile(`"a": "32"`),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
ses := NewSession()
|
2019-09-03 19:08:28 +00:00
|
|
|
got, err := ses.Put("http://httpbin.org/put").SetBodyAuto(tt.args.params, TypeFormData).Execute()
|
2018-10-17 06:25:17 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2018-10-17 10:38:04 +00:00
|
|
|
t.Errorf("Metchod error = %v", err)
|
2018-10-17 06:25:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-17 18:20:09 +00:00
|
|
|
if tt.want.MatchString(got.readContent) == false {
|
2018-10-17 10:38:04 +00:00
|
|
|
t.Errorf("Metchod = %v, want %v", got, tt.want)
|
2018-10-17 06:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSession_Patch(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
params interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want *regexp.Regexp
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "test post uploadfile glob",
|
|
|
|
args: args{params: "tests/*.js"},
|
|
|
|
want: regexp.MustCompile(`"file0": "data:application/octet-stream;base64`),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test post uploadfile only one file",
|
|
|
|
args: args{params: "tests/json.file"},
|
|
|
|
want: regexp.MustCompile(`"file0": "json.file.+jsonjsonjsonjson"`),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test post uploadfile key values",
|
|
|
|
args: args{params: map[string]string{"a": "32"}},
|
|
|
|
want: regexp.MustCompile(`"a": "32"`),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
ses := NewSession()
|
2019-09-03 19:08:28 +00:00
|
|
|
got, err := ses.Patch("http://httpbin.org/patch").SetBodyAuto(tt.args.params, TypeFormData).Execute()
|
2018-10-17 06:25:17 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2018-10-17 10:38:04 +00:00
|
|
|
t.Errorf("Metchod error = %v", err)
|
2018-10-17 06:25:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-17 18:20:09 +00:00
|
|
|
if tt.want.MatchString(got.readContent) == false {
|
2018-10-17 10:38:04 +00:00
|
|
|
t.Errorf("Metchod = %v, want %v", got, tt.want)
|
2018-10-17 06:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2018-10-19 08:26:50 +00:00
|
|
|
|
|
|
|
func TestSession_SetConfig(t *testing.T) {
|
|
|
|
|
|
|
|
type args struct {
|
|
|
|
typeConfig TypeConfig
|
|
|
|
values interface{}
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "test timeout",
|
2019-09-04 02:16:33 +00:00
|
|
|
args: args{typeConfig: CRequestTimeout, values: 0.0001},
|
2018-10-19 08:26:50 +00:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "test not timeout",
|
2018-11-08 06:26:51 +00:00
|
|
|
args: args{typeConfig: CRequestTimeout, values: 5},
|
2018-10-19 08:26:50 +00:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "test proxy",
|
2019-09-04 10:29:52 +00:00
|
|
|
args: args{typeConfig: CProxy, values: "http://" + ProxyAddress},
|
2018-10-19 08:26:50 +00:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
ses := NewSession()
|
|
|
|
ses.SetConfig(tt.args.typeConfig, tt.args.values)
|
2019-09-04 02:16:33 +00:00
|
|
|
_, err := ses.Get("http://httpbin.org/get").Execute()
|
2018-10-19 08:26:50 +00:00
|
|
|
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("Metchod error = %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSession_SetConfigInsecure(t *testing.T) {
|
|
|
|
|
|
|
|
ses := NewSession()
|
2018-12-17 16:21:27 +00:00
|
|
|
ses.SetConfig(CInsecure, true)
|
2018-10-19 08:26:50 +00:00
|
|
|
|
|
|
|
for _, badSSL := range []string{
|
|
|
|
"https://self-signed.badssl.com/",
|
|
|
|
"https://expired.badssl.com/",
|
|
|
|
"https://wrong.host.badssl.com/",
|
|
|
|
} {
|
|
|
|
resp, err := ses.Get(badSSL).Execute()
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Unable to make request", err)
|
|
|
|
}
|
2019-07-17 18:20:09 +00:00
|
|
|
if resp.readResponse.StatusCode != 200 {
|
|
|
|
t.Error("Request did not return OK, is ", resp.readResponse.StatusCode)
|
2018-10-19 08:26:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-10-23 09:08:50 +00:00
|
|
|
|
|
|
|
func TestSession_Cookies(t *testing.T) {
|
|
|
|
ses := NewSession()
|
|
|
|
|
2018-10-25 08:12:43 +00:00
|
|
|
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)
|
|
|
|
}
|
2018-10-23 09:08:50 +00:00
|
|
|
|
2019-07-17 18:20:09 +00:00
|
|
|
if !regexp.MustCompile(`"a": "1"`).MatchString(resp.readContent) {
|
|
|
|
t.Error(resp.readContent)
|
2018-10-25 08:12:43 +00:00
|
|
|
}
|
|
|
|
})
|
2018-10-23 09:08:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSession_Header(t *testing.T) {
|
|
|
|
chromeua := "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
|
|
|
|
ses := NewSession()
|
|
|
|
|
2018-10-25 08:12:43 +00:00
|
|
|
t.Run("ua header test", func(t *testing.T) {
|
2018-10-23 09:08:50 +00:00
|
|
|
|
2018-10-25 08:12:43 +00:00
|
|
|
ses.Header.Add(HeaderKeyUA, chromeua)
|
|
|
|
resp, err := ses.Get("https://www.baidu.com").Execute()
|
|
|
|
if err != nil {
|
|
|
|
t.Error("cookies set error", err)
|
|
|
|
}
|
2018-10-23 09:08:50 +00:00
|
|
|
|
2019-07-17 18:20:09 +00:00
|
|
|
if len(resp.readContent) <= 5000 {
|
|
|
|
t.Error(resp.readContent, len(resp.readContent))
|
2018-10-25 08:12:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ses = NewSession()
|
|
|
|
resp, err = ses.Get("https://www.baidu.com").AddHeader(HeaderKeyUA, chromeua).Execute()
|
|
|
|
if err != nil {
|
|
|
|
t.Error("cookies set error", err)
|
|
|
|
}
|
|
|
|
|
2019-07-17 18:20:09 +00:00
|
|
|
if len(resp.readContent) <= 5000 {
|
|
|
|
t.Error(resp.readContent, len(resp.readContent))
|
2018-10-25 08:12:43 +00:00
|
|
|
}
|
|
|
|
})
|
2018-10-23 09:08:50 +00:00
|
|
|
}
|
2019-09-04 10:29:52 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2019-09-04 16:50:44 +00:00
|
|
|
|
2019-09-04 10:29:52 +00:00
|
|
|
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"}})
|
2019-09-04 16:50:44 +00:00
|
|
|
ses.SetConfig(CIsWithCookiejar, false)
|
2019-09-04 10:29:52 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-04 16:50:44 +00:00
|
|
|
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())
|
|
|
|
}
|
2019-09-04 10:29:52 +00:00
|
|
|
}
|