requests/workflow_test.go

62 lines
1.3 KiB
Go

package requests
import (
"regexp"
"testing"
"474420502.top/eson/gjson"
)
func TestWorkflow(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.readContent) {
t.Error(resp.readContent)
}
wf := ses.Get("http://httpbin.org/cookies/set")
resp, err = wf.AddKVCookie("b", "2").Execute()
if err != nil {
t.Error("cookies set error", err)
}
result := gjson.Get(resp.readContent, "cookies.a")
if result.Exists() {
t.Error(resp.readContent)
}
result = gjson.Get(resp.readContent, "cookies.b")
if result.Int() != 2 {
t.Error(resp.readContent)
}
resp, err = wf.AddKVCookie("a", "3").Execute()
results := gjson.GetMany(resp.readContent, "cookies.a", "cookies.b")
if results[0].Int() != 3 {
t.Error(resp.readContent)
}
if results[1].Int() != 2 {
t.Error(resp.readContent)
}
resp, err = wf.AddHeader("XX", "123").SetRawURL("http://httpbin.org/headers").Execute()
if err != nil {
t.Error("cookies set error", err)
}
// headers 只能是String 表示
result = gjson.Get(resp.readContent, "headers.Xx")
if result.String() != "123" {
t.Error(resp.readContent)
}
})
}