添加curl 的预处理组件
This commit is contained in:
parent
85170b5b7e
commit
ea6cfc1efa
|
@ -73,6 +73,11 @@ func (cxt *TaskContext) Path() string {
|
|||
return cxt.current.Path()
|
||||
}
|
||||
|
||||
// GetHunter 获取share的数据, 存储用的
|
||||
func (cxt *TaskContext) GetHunter() *Hunter {
|
||||
return cxt.hunter
|
||||
}
|
||||
|
||||
// Hunt Hunt() = cxt.Workflow().Execute()
|
||||
func (cxt *TaskContext) Hunt() (*requests.Response, error) {
|
||||
return cxt.workflow.Execute()
|
||||
|
|
1
go.mod
1
go.mod
|
@ -4,5 +4,6 @@ go 1.14
|
|||
|
||||
require (
|
||||
github.com/474420502/focus v0.8.1
|
||||
github.com/474420502/gurl v0.0.1
|
||||
github.com/474420502/requests v1.4.0
|
||||
)
|
||||
|
|
2
go.sum
2
go.sum
|
@ -1,5 +1,7 @@
|
|||
github.com/474420502/focus v0.8.1 h1:PZwCgzcnxwx7ZZCWc/XKLVaZPH9e4YX9cP4ckyT2HDA=
|
||||
github.com/474420502/focus v0.8.1/go.mod h1:jrDXvK1CnUJ3PCR3ZJVYinbS2Yz5kM8OoAbCLe6AF7Y=
|
||||
github.com/474420502/gurl v0.0.1 h1:GYrbbDNVEcA7dyT3QjdEHILXydTkUs5ghqEZu6xdh9o=
|
||||
github.com/474420502/gurl v0.0.1/go.mod h1:Sx+9gFgFVM33bO7RQ8nj/I6JufDKPSCo0sikgPYmEp0=
|
||||
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=
|
||||
|
|
|
@ -22,24 +22,6 @@ func (web *WebGet) Execute(cxt *TaskContext) {
|
|||
cxt.SetShare("test", resp.Content())
|
||||
}
|
||||
|
||||
func TestCasePreUrl(t *testing.T) {
|
||||
hunter := NewHunter()
|
||||
hunter.AddTask(&WebGet{PreGetUrl: "http://httpbin.org/get"})
|
||||
hunter.Execute()
|
||||
|
||||
data := make(map[string]interface{})
|
||||
content := hunter.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
|
||||
}
|
||||
|
@ -102,34 +84,18 @@ type WebSub1 struct {
|
|||
}
|
||||
|
||||
func (web *WebSub1) Execute(cxt *TaskContext) {
|
||||
log.Panic(cxt.Path() + "." + cxt.TaskID())
|
||||
cxt.SetShare("test", cxt.Path()+"."+cxt.TaskID())
|
||||
}
|
||||
|
||||
func TestCaseWebSub(t *testing.T) {
|
||||
|
||||
hunter := NewHunter()
|
||||
hunter.AddTask(&WebSub{"http://httpbin.org/post"})
|
||||
hunter.Execute()
|
||||
|
||||
data := make(map[string]interface{})
|
||||
content := hunter.GetShare("test").(string)
|
||||
err := json.Unmarshal([]byte(content), &data)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
if content != ".0.1" {
|
||||
t.Error(content)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
14
pre_curl_com.go
Normal file
14
pre_curl_com.go
Normal file
|
@ -0,0 +1,14 @@
|
|||
package hunter
|
||||
|
||||
import (
|
||||
gcurl "github.com/474420502/gurl"
|
||||
)
|
||||
|
||||
// PreCurlUrl Task的 curl bash 预处理组件
|
||||
type PreCurlUrl string
|
||||
|
||||
func (h PreCurlUrl) Before(ctx *TaskContext) {
|
||||
gurl := gcurl.ParseRawCURL(string(h))
|
||||
ctx.GetHunter().SetSession(gurl.CreateSession())
|
||||
ctx.SetWorkflow(gurl.CreateWorkflow(ctx.Session()))
|
||||
}
|
32
pre_curl_com_test.go
Normal file
32
pre_curl_com_test.go
Normal file
|
@ -0,0 +1,32 @@
|
|||
package hunter
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type WebGurl struct {
|
||||
PreCurlUrl
|
||||
}
|
||||
|
||||
func (web *WebGurl) Execute(cxt *TaskContext) {
|
||||
resp, err := cxt.Hunt()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
cxt.SetShare("test", resp.Content())
|
||||
}
|
||||
|
||||
func TestCurlCom(t *testing.T) {
|
||||
curlBash := "curl 'http://httpbin.org/' -H 'Connection: keep-alive' -H 'Cache-Control: max-age=0' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9' -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: zh-CN,zh;q=0.9' --compressed --insecure"
|
||||
hunter := NewHunter()
|
||||
hunter.AddTask(&WebGurl{PreCurlUrl: PreCurlUrl(curlBash)})
|
||||
hunter.Execute()
|
||||
|
||||
content := hunter.GetShare("test").(string)
|
||||
|
||||
isMatchContent := regexp.MustCompile("<title>httpbin.org</title>").MatchString(content)
|
||||
if !isMatchContent {
|
||||
t.Error(content)
|
||||
}
|
||||
}
|
36
pre_url_com_test.go
Normal file
36
pre_url_com_test.go
Normal file
|
@ -0,0 +1,36 @@
|
|||
package hunter
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type WebPreUrl struct {
|
||||
PreGetUrl
|
||||
}
|
||||
|
||||
func (web *WebPreUrl) Execute(cxt *TaskContext) {
|
||||
resp, err := cxt.Hunt()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
cxt.SetShare("test", resp.Content())
|
||||
}
|
||||
|
||||
func TestCasePreUrl(t *testing.T) {
|
||||
hunter := NewHunter()
|
||||
hunter.AddTask(&WebPreUrl{PreGetUrl: "http://httpbin.org/get"})
|
||||
hunter.Execute()
|
||||
|
||||
data := make(map[string]interface{})
|
||||
content := hunter.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)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user