Added documentation and tests for request.Meta
This commit is contained in:
parent
a2a91b7b2e
commit
d3bdaf6240
20
README.md
20
README.md
@ -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*
|
||||
|
14
client/request_test.go
Normal file
14
client/request_test.go
Normal 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")
|
||||
}
|
@ -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) {
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user