Skip to content

Commit

Permalink
Added documentation and tests for request.Meta
Browse files Browse the repository at this point in the history
  • Loading branch information
musabgultekin committed May 30, 2021
1 parent a2a91b7 commit d3bdaf6
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ geziyor.NewGeziyor(&geziyor.Options{
},
//BrowserEndpoint: "ws://localhost:3000",
}).Start()
```
```

### Extracting Data

Expand Down Expand Up @@ -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*
Expand Down
14 changes: 14 additions & 0 deletions client/request_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
13 changes: 13 additions & 0 deletions geziyor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down

0 comments on commit d3bdaf6

Please sign in to comment.