From d3bdaf6240124135397ae8ebaa08378ea1901a31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Musab=20G=C3=BCltekin?= Date: Sun, 30 May 2021 10:43:54 +0300 Subject: [PATCH] Added documentation and tests for request.Meta --- README.md | 22 +++++++++++++++++++++- client/request_test.go | 14 ++++++++++++++ geziyor_test.go | 13 +++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 client/request_test.go diff --git a/README.md b/README.md index 3180eb7..037b672 100644 --- a/README.md +++ b/README.md @@ -107,7 +107,7 @@ geziyor.NewGeziyor(&geziyor.Options{ }, //BrowserEndpoint: "ws://localhost:3000", }).Start() -``` +``` ### Extracting Data @@ -147,6 +147,26 @@ geziyor.NewGeziyor(&geziyor.Options{ }).Start() ``` + +### Custom Requests - Passing Metadata To Callbacks + +You can create custom requests with ```client.NewRequest``` + +Use that request on ```geziyor.Do(request, callback)``` + +```go +geziyor.NewGeziyor(&geziyor.Options{ + StartRequestsFunc: func(g *geziyor.Geziyor) { + req, _ := client.NewRequest("GET", "https://httpbin.org/anything", nil) + req.Meta["key"] = "value" + g.Do(req, g.Opt.ParseFunc) + }, + ParseFunc: func(g *geziyor.Geziyor, r *client.Response) { + fmt.Println("This is our data from request: ", r.Request.Meta["key"]) + }, +}).Start() +``` + ## Benchmark **8748 request per seconds** on *Macbook Pro 15" 2016* diff --git a/client/request_test.go b/client/request_test.go new file mode 100644 index 0000000..17808bd --- /dev/null +++ b/client/request_test.go @@ -0,0 +1,14 @@ +package client + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestMeta(t *testing.T) { + req, err := NewRequest("GET", "https://github.com/geziyor/geziyor", nil) + assert.NoError(t, err) + req.Meta["key"] = "value" + + assert.Equal(t, req.Meta["key"], "value") +} diff --git a/geziyor_test.go b/geziyor_test.go index 9057931..b4251e9 100644 --- a/geziyor_test.go +++ b/geziyor_test.go @@ -229,6 +229,19 @@ func TestRobots(t *testing.T) { }).Start() } +func TestPassMetadata(t *testing.T) { + geziyor.NewGeziyor(&geziyor.Options{ + StartRequestsFunc: func(g *geziyor.Geziyor) { + req, _ := client.NewRequest("GET", "https://httpbin.org/anything", nil) + req.Meta["key"] = "value" + g.Do(req, g.Opt.ParseFunc) + }, + ParseFunc: func(g *geziyor.Geziyor, r *client.Response) { + assert.Equal(t, r.Request.Meta["key"], "value") + }, + }).Start() +} + // Make sure to increase open file descriptor limits before running func BenchmarkRequests(b *testing.B) {