add some test

This commit is contained in:
huangsimin 2020-04-08 15:18:29 +08:00
parent e9cfeec84e
commit d7c4848d63
8 changed files with 222 additions and 16 deletions

View File

@ -1,10 +1,14 @@
package hunter
import "github.com/474420502/requests"
// TaskContext 上下文
type TaskContext struct {
share map[string]interface{}
hunter *Hunter
curNode ITaskNode
share map[string]interface{}
hunter *Hunter
curNode ITaskNode
session *requests.Session
workflow *requests.Workflow
}
// NewContext 任务上下文
@ -17,15 +21,15 @@ func (cxt *TaskContext) AddTask(itask ITask) {
if children := cxt.curNode.Children(); children == nil {
cxt.curNode.SetChildren(cxt.hunter.createQueue())
}
bt := &BaseTask{}
bt.SetTask(itask)
bt := &BaseTask{task: itask}
cxt.curNode.Children().Push(bt)
}
// AddParentTask 添加到当前任务队列
func (cxt *TaskContext) AddParentTask(itask ITask) {
bt := &BaseTask{}
bt.SetTask(itask)
bt := &BaseTask{task: itask}
cxt.curNode.Parent().Children().Push(bt)
}
@ -41,3 +45,31 @@ func (cxt *TaskContext) GetShare(key string) interface{} {
func (cxt *TaskContext) SetShare(key string, value interface{}) {
cxt.share[key] = value
}
// Session Get return session *requests.Session
func (cxt *TaskContext) Session() *requests.Session {
if cxt.session == nil {
cxt.session = requests.NewSession()
}
return cxt.session
}
// SetSession Set session *requests.Session
func (cxt *TaskContext) SetSession(session *requests.Session) {
cxt.session = session
}
// Workflow Get return Workflow *requests.Workflow. not exists, return nil
func (cxt *TaskContext) Workflow() *requests.Workflow {
return cxt.workflow
}
// SetWorkflow Set Workflow *requests.Workflow
func (cxt *TaskContext) SetWorkflow(workflow *requests.Workflow) {
cxt.workflow = workflow
}
// Hunt Hunt() = cxt.Workflow().Execute()
func (cxt *TaskContext) Hunt() (*requests.Response, error) {
return cxt.workflow.Execute()
}

1
go.mod
View File

@ -4,4 +4,5 @@ go 1.14
require (
github.com/474420502/focus v0.8.1
github.com/474420502/requests v1.4.0
)

19
go.sum
View File

@ -1,10 +1,24 @@
github.com/474420502/focus v0.8.1 h1:PZwCgzcnxwx7ZZCWc/XKLVaZPH9e4YX9cP4ckyT2HDA=
github.com/474420502/focus v0.8.1/go.mod h1:jrDXvK1CnUJ3PCR3ZJVYinbS2Yz5kM8OoAbCLe6AF7Y=
github.com/474420502/requests v1.0.0 h1:yY5OP1TJRsu2yI0qMW+4OteFqmNmudhwgLuimvXLPPU=
github.com/474420502/requests v1.0.0/go.mod h1:SLXrQ5dL9c7dkIeKNUCBAjOIt3J9KFCS2RQjWJecNwo=
github.com/474420502/requests v1.4.0 h1:dnufhTVTo/N1SdWVqBKm4xKDRb0N4P7D7Zg1FJFO29I=
github.com/474420502/requests v1.4.0/go.mod h1:SLXrQ5dL9c7dkIeKNUCBAjOIt3J9KFCS2RQjWJecNwo=
github.com/Pallinder/go-randomdata v1.1.0/go.mod h1:yHmJgulpD2Nfrm0cR9tI/+oAgRqCQQixsA8HyRZfV9Y=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/elazarl/goproxy v0.0.0-20190711103511-473e67f1d7d2/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc=
github.com/elazarl/goproxy/ext v0.0.0-20190711103511-473e67f1d7d2/go.mod h1:gNh8nYJoAm43RfaxurUnxr+N1PwuFV3ZMl/efxlIlY8=
github.com/mdempsky/gocode v0.0.0-20200405233807-4acdcbdea79d h1:P8ngpzttYTqLj67Xt66V+2o5C53fGhXvkbqwFXMFvVI=
github.com/mdempsky/gocode v0.0.0-20200405233807-4acdcbdea79d/go.mod h1:hltEC42XzfMNgg0S1v6JTywwra2Mu6F6cLR03debVQ8=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-charset v0.0.0-20180617210344-2471d30d28b4/go.mod h1:qgYeAmZ5ZIpBWTGllZSQnw97Dj+woV0toclVaRGI8pc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/tidwall/gjson v1.3.2/go.mod h1:P256ACg0Mn+j1RXIDXoss50DeIABTYK1PULOJHhxOls=
github.com/tidwall/match v1.0.1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0+pL9E=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
@ -12,10 +26,13 @@ golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e h1:3G+cUijn7XD+S4eJFddp53Pv7+slrESplyjG25HgL+k=
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200407143752-a3568bac92ae h1:nMRC2i7oRGagyN5g3chxBF7gb2do/tIJ3ihoEnvYvsU=
@ -23,3 +40,5 @@ golang.org/x/tools v0.0.0-20200407143752-a3568bac92ae/go.mod h1:EkVYQZoAsY45+roY
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

View File

@ -56,13 +56,25 @@ func (hunter *Hunter) Execute() {
}
func (hunter *Hunter) recursionTasks(itask ITaskNode) {
for children := itask.Children(); children != nil && children.Size() > 0; {
if itask, ok := children.Pop(); ok {
tasknode := itask.(ITaskNode)
task := tasknode.Task()
if before, ok := task.(IBefore); ok {
before.Before(hunter.cxt)
}
tasknode.Task().Execute(hunter.cxt)
if after, ok := task.(IAfter); ok {
after.After(hunter.cxt)
}
hunter.recursionTasks(tasknode)
}
}
}
// Stop 停止任务

View File

@ -1,19 +1,112 @@
package hunter
import (
"encoding/json"
"testing"
)
type Web struct {
type WebGet struct {
PreGetUrl
}
func (web Web) Execute(cxt *TaskContext) {
cxt.SetShare("123", 123)
func (web *WebGet) Execute(cxt *TaskContext) {
resp, err := cxt.Hunt()
if err != nil {
panic(err)
}
cxt.SetShare("test", resp.Content())
}
func TestCase1(t *testing.T) {
func TestCasePreUrl(t *testing.T) {
hunter := NewHunter()
hunter.AddTask(&Web{})
hunter.AddTask(&WebGet{PreGetUrl: "http://httpbin.org/get"})
hunter.Execute()
t.Error(hunter.cxt.GetShare("123"))
data := make(map[string]interface{})
content := hunter.cxt.GetShare("test").(string)
err := json.Unmarshal([]byte(content), &data)
if err != nil {
t.Error(err)
}
if iurl, ok := data["url"]; ok {
if iurl.(string) != "http://httpbin.org/get" {
t.Error(iurl)
}
}
}
type WebPost struct {
PrePostUrl
}
func (web *WebPost) Execute(cxt *TaskContext) {
wf := cxt.Workflow()
wf.SetBodyAuto("param=hello form")
resp, err := wf.Execute()
if err != nil {
panic(err)
}
cxt.SetShare("test", resp.Content())
}
func TestCasePostForm(t *testing.T) {
hunter := NewHunter()
hunter.AddTask(&WebPost{PrePostUrl: "http://httpbin.org/post"})
hunter.Execute()
data := make(map[string]interface{})
content := hunter.cxt.GetShare("test").(string)
err := json.Unmarshal([]byte(content), &data)
if err != nil {
t.Error(err)
}
if iform, ok := data["form"]; ok {
if iform.(string) != "hello form" {
t.Error(iform)
}
}
}
type WebSub struct {
PrePostUrl
}
func (web *WebSub) Execute(cxt *TaskContext) {
wf := cxt.Workflow()
wf.SetBodyAuto(`{"a": "1"}`)
resp, err := wf.Execute()
if err != nil {
panic(err)
}
cxt.SetShare("test", resp.Content())
}
func TestCaseWebSub(t *testing.T) {
hunter := NewHunter()
hunter.AddTask(&WebSub{PrePostUrl: "http://httpbin.org/post"})
hunter.Execute()
data := make(map[string]interface{})
content := hunter.cxt.GetShare("test").(string)
err := json.Unmarshal([]byte(content), &data)
if err != nil {
t.Error(err)
}
if ijson, ok := data["json"]; ok {
if j, ok := ijson.(map[string]interface{}); ok {
if ia, ok := j["a"]; ok {
if ia.(string) != "1" {
t.Error(ia)
}
} else {
t.Error(ia)
}
} else {
t.Error(j)
}
}
}

50
pre_url_com.go Normal file
View File

@ -0,0 +1,50 @@
package hunter
// PreGetUrl Task的 Get url 预处理组件
type PreGetUrl string
func (h PreGetUrl) Before(ctx *TaskContext) {
ctx.SetWorkflow(ctx.Session().Get((string)(h)))
}
// PrePostUrl Task的 Post url 预处理组件
type PrePostUrl string
func (h PrePostUrl) Before(ctx *TaskContext) {
ctx.SetWorkflow(ctx.Session().Post((string)(h)))
}
// PrePutUrl Task的 Put url 预处理组件
type PrePutUrl string
func (h PrePutUrl) Before(ctx *TaskContext) {
ctx.SetWorkflow(ctx.Session().Put((string)(h)))
}
// PreHeadUrl Task的 Head url 预处理组件
type PreHeadUrl string
func (h PreHeadUrl) Before(ctx *TaskContext) {
ctx.SetWorkflow(ctx.Session().Head((string)(h)))
}
// PrePatchUrl Task的 Patch url 预处理组件
type PrePatchUrl string
func (h PrePatchUrl) Before(ctx *TaskContext) {
ctx.SetWorkflow(ctx.Session().Patch((string)(h)))
}
// PreDeleteUrl Task的 Delete url 预处理组件
type PreDeleteUrl string
func (h PreDeleteUrl) Before(ctx *TaskContext) {
ctx.SetWorkflow(ctx.Session().Delete((string)(h)))
}
// PreOptionsUrl Task的 Options url 预处理组件
type PreOptionsUrl string
func (h PreOptionsUrl) Before(ctx *TaskContext) {
ctx.SetWorkflow(ctx.Session().Options((string)(h)))
}

View File

@ -1 +0,0 @@
package hunter

View File

@ -6,7 +6,7 @@ import (
// IBefore 执行任务前处理
type IBefore interface {
Before()
Before(cxt *TaskContext)
}
// // IExecute 执行任务
@ -16,7 +16,7 @@ type IBefore interface {
// IAfter 执行任务后
type IAfter interface {
After()
After(cxt *TaskContext)
}
// ITask 任务接口