requests/requests_method_test.go

276 lines
6.1 KiB
Go
Raw Normal View History

2018-10-17 06:25:17 +00:00
package requests
import (
2018-10-17 10:38:04 +00:00
"log"
2018-10-17 06:25:17 +00:00
"net/http"
"regexp"
"testing"
)
2018-10-17 10:38:04 +00:00
func init() {
log.SetFlags(log.Lshortfile | log.LstdFlags)
}
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
}{
// TODO: Add test cases.
{
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)
}
if len(resp.Content()) <= 200 {
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()
got, err := ses.Post("http://httpbin.org/post").SetBodyParams(tt.args.params...).Execute()
if err != nil {
t.Errorf("Metchod error = %v", err)
2018-10-17 06:25:17 +00:00
return
}
if tt.want.MatchString(got.DContent) == 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
params *Params
}
type args struct {
params []interface{}
}
tests := []struct {
name string
fields fields
args args
want *regexp.Regexp
wantErr bool
}{
// TODO: Add test cases.
{
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",
fields: fields{client: &http.Client{}, params: &Params{}},
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()
2018-10-17 10:38:04 +00:00
got, err := ses.Post("http://httpbin.org/post").SetBodyParams(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
}
if tt.want.MatchString(got.DContent) == 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()
2018-10-17 10:38:04 +00:00
got, err := ses.Post("http://httpbin.org/post").SetBodyParams(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
}
if tt.want.MatchString(got.DContent) == 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()
2018-10-17 10:38:04 +00:00
got, err := ses.Put("http://httpbin.org/put").SetBodyParams(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
}
if tt.want.MatchString(got.DContent) == 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()
2018-10-17 10:38:04 +00:00
got, err := ses.Patch("http://httpbin.org/patch").SetBodyParams(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
}
if tt.want.MatchString(got.DContent) == 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
}
})
}
}