package requests import ( "log" "net/http" "regexp" "testing" ) func init() { log.SetFlags(log.Lshortfile | log.LstdFlags) } 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, } resp, err := ses.Get(tt.args.url).Execute() if err != nil { t.Error(err) } if len(resp.Content()) <= 200 { t.Error(resp.Content()) } }) } } func TestSession_Post(t *testing.T) { type args struct { params []interface{} } tests := []struct { name string args args want *regexp.Regexp }{ { name: "Post test", args: args{params: nil}, want: regexp.MustCompile(`"form": \{\}`), }, { name: "Post data", args: args{params: []interface{}{[]byte("a=1&b=2")}}, want: regexp.MustCompile(`"form": \{[^"]+"a": "1"[^"]+"b": "2"[^\}]+\}`), }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { ses := NewSession() got, err := ses.Post("http://httpbin.org/post").SetBodyParams(tt.args.params...).Execute() if err != nil { t.Errorf("Metchod error = %v", err) return } if tt.want.MatchString(got.DContent) == false { t.Errorf("Metchod = %v, want %v", got, tt.want) } }) } } func TestSession_Setparams(t *testing.T) { type fields struct { client *http.Client params *Body } type args struct { params []interface{} } tests := []struct { name string fields fields args args want *regexp.Regexp wantErr bool }{ { name: "test Setparams", args: args{params: []interface{}{map[string]string{"a": "1", "b": "2"}}}, want: regexp.MustCompile(`"form": \{[^"]+"a": "1"[^"]+"b": "2"[^\}]+\}`), }, { name: "test json", args: args{params: []interface{}{`{"a":"1","b":"2"}`, TypeJSON}}, want: regexp.MustCompile(`"json": \{[^"]+"a": "1"[^"]+"b": "2"[^\}]+\}`), }, { name: "test xml", fields: fields{client: &http.Client{}, params: &Body{}}, args: args{params: []interface{}{`test`, TypeXML}}, want: regexp.MustCompile(`"data": "test"`), }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { ses := NewSession() got, err := ses.Post("http://httpbin.org/post").SetBodyParams(tt.args.params...).Execute() if (err != nil) != tt.wantErr { t.Errorf("Metchod error = %v, wantErr %v", err, tt.wantErr) return } if tt.want.MatchString(got.DContent) == false { t.Errorf("Metchod = %v, want %v", got, tt.want) } }) } } 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() got, err := ses.Post("http://httpbin.org/post").SetBodyParams(tt.args.params, TypeFormData).Execute() if err != nil { t.Errorf("Metchod error = %v", err) return } if tt.want.MatchString(got.DContent) == false { t.Errorf("Metchod = %v, want %v", got, tt.want) } }) } } 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() got, err := ses.Put("http://httpbin.org/put").SetBodyParams(tt.args.params, TypeFormData).Execute() if err != nil { t.Errorf("Metchod error = %v", err) return } if tt.want.MatchString(got.DContent) == false { t.Errorf("Metchod = %v, want %v", got, tt.want) } }) } } 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() got, err := ses.Patch("http://httpbin.org/patch").SetBodyParams(tt.args.params, TypeFormData).Execute() if err != nil { t.Errorf("Metchod error = %v", err) return } if tt.want.MatchString(got.DContent) == false { t.Errorf("Metchod = %v, want %v", got, tt.want) } }) } } func TestSession_SetConfig(t *testing.T) { type args struct { typeConfig TypeConfig values interface{} } tests := []struct { name string args args wantErr bool }{ { name: "test timeout", args: args{typeConfig: CRequestTimeout, values: 0.01}, wantErr: true, }, { name: "test not timeout", args: args{typeConfig: CRequestTimeout, values: 5}, wantErr: false, }, { name: "test proxy", args: args{typeConfig: CProxy, values: "http://474420502.top:7070"}, wantErr: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { ses := NewSession() ses.SetConfig(tt.args.typeConfig, tt.args.values) _, err := ses.Get("https://httpbin.org/get").Execute() if (err != nil) != tt.wantErr { t.Errorf("Metchod error = %v", err) return } }) } } func TestSession_SetConfigInsecure(t *testing.T) { ses := NewSession() ses.SetConfig(CInsecure, false) 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) } if resp.GResponse.StatusCode != 200 { t.Error("Request did not return OK, is ", resp.GResponse.StatusCode) } } } 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) } }) } 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() t.Run("ua header test", func(t *testing.T) { ses.Header.Add(HeaderKeyUA, chromeua) resp, err := ses.Get("https://www.baidu.com").Execute() if err != nil { t.Error("cookies set error", err) } if len(resp.DContent) <= 5000 { t.Error(resp.DContent, len(resp.DContent)) } ses = NewSession() resp, err = ses.Get("https://www.baidu.com").AddHeader(HeaderKeyUA, chromeua).Execute() if err != nil { t.Error("cookies set error", err) } if len(resp.DContent) <= 5000 { t.Error(resp.DContent, len(resp.DContent)) } }) }