Added documentation and tests for request.Meta

This commit is contained in:
Musab Gültekin 2021-05-30 10:43:54 +03:00
parent a2a91b7b2e
commit d3bdaf6240
3 changed files with 48 additions and 1 deletions

View File

@ -107,7 +107,7 @@ geziyor.NewGeziyor(&geziyor.Options{
}, },
//BrowserEndpoint: "ws://localhost:3000", //BrowserEndpoint: "ws://localhost:3000",
}).Start() }).Start()
``` ```
### Extracting Data ### Extracting Data
@ -147,6 +147,26 @@ geziyor.NewGeziyor(&geziyor.Options{
}).Start() }).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 ## Benchmark
**8748 request per seconds** on *Macbook Pro 15" 2016* **8748 request per seconds** on *Macbook Pro 15" 2016*

14
client/request_test.go Normal file
View File

@ -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")
}

View File

@ -229,6 +229,19 @@ func TestRobots(t *testing.T) {
}).Start() }).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 // Make sure to increase open file descriptor limits before running
func BenchmarkRequests(b *testing.B) { func BenchmarkRequests(b *testing.B) {