hunter/hunter_test.go

102 lines
1.9 KiB
Go
Raw Normal View History

2020-04-07 10:55:41 +00:00
package hunter
2020-04-07 17:18:33 +00:00
import (
2020-04-08 07:18:29 +00:00
"encoding/json"
2020-04-09 02:06:16 +00:00
"log"
2020-04-07 17:18:33 +00:00
"testing"
)
2020-04-07 10:55:41 +00:00
2020-04-09 02:06:16 +00:00
func init() {
log.Println("recommend: docker run -p 80:80 kennethreitz/httpbin")
2020-04-09 02:06:16 +00:00
}
2020-04-08 07:18:29 +00:00
type WebGet struct {
PreGetUrl
2020-04-07 17:18:33 +00:00
}
2020-04-07 10:55:41 +00:00
2020-04-08 07:18:29 +00:00
func (web *WebGet) Execute(cxt *TaskContext) {
resp, err := cxt.Hunt()
if err != nil {
panic(err)
}
cxt.SetShare("test", resp.Content())
2020-04-07 17:18:33 +00:00
}
2020-04-08 07:18:29 +00:00
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", string(resp.Content()))
2020-04-08 07:18:29 +00:00
}
func TestCasePostForm(t *testing.T) {
hunter := NewHunter()
hunter.AddTask(&WebPost{PrePostUrl: "http://httpbin.org/post"})
hunter.Execute()
data := make(map[string]interface{})
content := hunter.GetShare("test").(string)
2020-04-08 07:18:29 +00:00
err := json.Unmarshal([]byte(content), &data)
if err != nil {
t.Error(err)
}
if iform, ok := data["form"]; ok {
2020-04-09 10:41:30 +00:00
form := iform.(map[string]interface{})
if form["param"].(string) != "hello form" {
2020-04-08 07:18:29 +00:00
t.Error(iform)
}
}
}
type WebSub struct {
PrePostUrl
}
func (web *WebSub) Execute(cxt *TaskContext) {
wf := cxt.Workflow()
2020-04-09 10:41:30 +00:00
wf.SetBodyAuto(`{"a": "1","url":["http://httpbin.org/post","http://httpbin.org/get"]}`)
2020-04-08 07:18:29 +00:00
resp, err := wf.Execute()
if err != nil {
panic(err)
}
cxt.SetShare("test", resp.Content())
2020-04-09 10:41:30 +00:00
data := make(map[string]interface{})
2020-04-10 17:44:31 +00:00
json.Unmarshal(resp.Content(), &data)
2020-04-09 10:41:30 +00:00
if urlList, ok := data["json"].(map[string]interface{})["url"].([]interface{}); ok {
for _, is := range urlList {
s := is.(string)
cxt.AddTask(&WebSub1{PrePostUrl(s)})
}
}
}
type WebSub1 struct {
PrePostUrl
}
func (web *WebSub1) Execute(cxt *TaskContext) {
2020-04-17 07:05:41 +00:00
cxt.SetShare("test", cxt.Path()+"/"+cxt.TaskID())
2020-04-08 07:18:29 +00:00
}
func TestCaseWebSub(t *testing.T) {
2020-04-10 08:39:48 +00:00
2020-04-08 07:18:29 +00:00
hunter := NewHunter()
2020-04-09 10:41:30 +00:00
hunter.AddTask(&WebSub{"http://httpbin.org/post"})
2020-04-08 07:18:29 +00:00
hunter.Execute()
content := hunter.GetShare("test").(string)
2020-04-17 07:05:41 +00:00
if content != "/0/1" {
2020-04-10 08:39:48 +00:00
t.Error(content)
2020-04-08 07:18:29 +00:00
}
2020-04-07 10:55:41 +00:00
}