286 lines
6.5 KiB
Go
286 lines
6.5 KiB
Go
package requests
|
|
|
|
import (
|
|
"net/http"
|
|
"regexp"
|
|
"testing"
|
|
)
|
|
|
|
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,
|
|
}
|
|
resp, err := ses.Get(tt.args.url)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if len(resp.Content()) <= 200 {
|
|
t.Error(resp.Content())
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSession_Post(t *testing.T) {
|
|
type fields struct {
|
|
client *http.Client
|
|
params *Params
|
|
}
|
|
type args struct {
|
|
url string
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
fields fields
|
|
args args
|
|
want *regexp.Regexp
|
|
wantErr bool
|
|
}{
|
|
// TODO: Add test cases.
|
|
{
|
|
name: "Post test",
|
|
fields: fields{client: &http.Client{}, params: &Params{}},
|
|
args: args{url: "http://httpbin.org/post"},
|
|
want: regexp.MustCompile(`"form": \{\}`),
|
|
},
|
|
{
|
|
name: "Post data",
|
|
fields: fields{client: &http.Client{}, params: &Params{Body: []byte("a=1&b=2")}},
|
|
args: args{url: "http://httpbin.org/post"},
|
|
want: regexp.MustCompile(`"form": \{[^"]+"a": "1"[^"]+"b": "2"[^\}]+\}`),
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
ses := &Session{
|
|
client: tt.fields.client,
|
|
params: tt.fields.params,
|
|
}
|
|
got, err := ses.Post(tt.args.url)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("Session.Post() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
|
|
if tt.want.MatchString(got.DContent) == false {
|
|
t.Errorf("Session.Post() = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
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.
|
|
{
|
|
name: "test Setparams",
|
|
fields: fields{client: &http.Client{}, params: &Params{}},
|
|
args: args{params: []interface{}{map[string]string{"a": "1", "b": "2"}}},
|
|
want: regexp.MustCompile(`"form": \{[^"]+"a": "1"[^"]+"b": "2"[^\}]+\}`),
|
|
},
|
|
{
|
|
name: "test json",
|
|
fields: fields{client: &http.Client{}, params: &Params{}},
|
|
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: &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()
|
|
ses.SetParams(tt.args.params...)
|
|
got, err := ses.Post("http://httpbin.org/post")
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("Session.Post() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
|
|
if tt.want.MatchString(got.DContent) == false {
|
|
t.Errorf("Session.Post() = %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()
|
|
ses.SetParams(tt.args.params, TypeFormData)
|
|
got, err := ses.Post("http://httpbin.org/post")
|
|
|
|
if err != nil {
|
|
t.Errorf("Session.Post() error = %v", err)
|
|
return
|
|
}
|
|
|
|
if tt.want.MatchString(got.DContent) == false {
|
|
t.Errorf("Session.Post() = %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()
|
|
ses.SetParams(tt.args.params, TypeFormData)
|
|
got, err := ses.Put("http://httpbin.org/put")
|
|
|
|
if err != nil {
|
|
t.Errorf("Session.Post() error = %v", err)
|
|
return
|
|
}
|
|
|
|
if tt.want.MatchString(got.DContent) == false {
|
|
t.Errorf("Session.Post() = %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()
|
|
ses.SetParams(tt.args.params, TypeFormData)
|
|
got, err := ses.Patch("http://httpbin.org/patch")
|
|
|
|
if err != nil {
|
|
t.Errorf("Session.Post() error = %v", err)
|
|
return
|
|
}
|
|
|
|
if tt.want.MatchString(got.DContent) == false {
|
|
t.Errorf("Session.Post() = %v, want %v", got, tt.want)
|
|
}
|
|
|
|
})
|
|
}
|
|
}
|