Add custom actions for rendered requests & Fix not closing bug

This commit is contained in:
Musab Gültekin
2022-04-29 03:05:31 +03:00
parent 34d17a2d3d
commit 738852f932
7 changed files with 824 additions and 87 deletions

View File

@@ -2,8 +2,11 @@ package geziyor_test
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/chromedp/cdproto/dom"
"github.com/chromedp/chromedp"
"net/http"
"net/http/httptest"
"net/url"
@@ -138,6 +141,37 @@ func TestGetRendered(t *testing.T) {
}).Start()
}
func TestGetRenderedCustomActions(t *testing.T) {
geziyor.NewGeziyor(&geziyor.Options{
StartRequestsFunc: func(g *geziyor.Geziyor) {
req, _ := client.NewRequest("GET", "https://httpbin.org/anything", nil)
req.Rendered = true
req.Actions = []chromedp.Action{
chromedp.Navigate("https://httpbin.org/anything"),
chromedp.WaitReady(":root"),
chromedp.ActionFunc(func(ctx context.Context) error {
node, err := dom.GetDocument().Do(ctx)
if err != nil {
return err
}
body, err := dom.GetOuterHTML().WithNodeID(node.NodeID).Do(ctx)
fmt.Println("HOLAAA", body)
return err
}),
}
g.Do(req, g.Opt.ParseFunc)
},
ParseFunc: func(g *geziyor.Geziyor, r *client.Response) {
fmt.Println(string(r.Body))
fmt.Println(r.Request.URL.String(), r.Header)
},
// This will make only visit and nothing more.
//PreActions: []chromedp.Action{
// chromedp.Navigate("https://httpbin.org/anything"),
//},
}).Start()
}
func TestGetRenderedCookie(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(r.Header.Get("Cookie")))