Skip to content

Commit

Permalink
README: fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
dnaeon committed Aug 18, 2024
1 parent a066115 commit cd0d4d6
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ function.

For example, the following matcher will match on method, URL and body:

```go
``` go

func customMatcher(r *http.Request, i cassette.Request) bool {
if r.Body == nil || r.Body == http.NoBody {
return cassette.DefaultMatcher(r, i)
Expand Down Expand Up @@ -74,7 +75,6 @@ client := rec.GetDefaultClient()
resp, err := client.Get("https://www.google.com/")

...
}
```

## Hooks
Expand Down Expand Up @@ -102,24 +102,26 @@ playback. The supported hook kinds are `AfterCaptureHook`, `BeforeSaveHook`,
Here is an example that removes the `Authorization` header from all requests
right after capturing a new interaction.

```go
``` go
// A hook which removes Authorization headers from all requests
hook := func(i *cassette.Interaction) error {
delete(i.Request.Headers, "Authorization")
return nil
return nil
}

// Recorder options
opts := []recorder.Option{
recorder.WithCassette("fixtures/filters"),
recorder.WithHook(hook, recorder.AfterCaptureHook),
recorder.WithCassette("fixtures/filters"),
recorder.WithHook(hook, recorder.AfterCaptureHook),
}

r, err := recorder.New(opts...)
if err != nil {
log.Fatal(err)
}
defer r.Stop() // Make sure recorder is stopped once done with it

...
```

Hooks added using `recorder.AfterCaptureHook` are applied right after an
Expand All @@ -136,7 +138,7 @@ In such cases you would want to modify the recorded interactions right before
they are saved on disk. For that purpose you should be using a `BeforeSaveHook`,
e.g.

```go
``` go
// Your test code will continue to see the real access token and
// it is redacted before the recorded interactions are saved on disk
hook := func(i *cassette.Interaction) error {
Expand All @@ -149,15 +151,17 @@ hook := func(i *cassette.Interaction) error {

// Recorder options
opts := []recorder.Option{
recorder.WithCassette("fixtures/filters"),
recorder.WithHook(hook, recorder.BeforeSaveHook),
recorder.WithCassette("fixtures/filters"),
recorder.WithHook(hook, recorder.BeforeSaveHook),
}

r, err := recorder.New(opts...)
if err != nil {
log.Fatal(err)
}
defer r.Stop() // Make sure recorder is stopped once done with it

...
```

## Passing Through Requests
Expand All @@ -171,15 +175,15 @@ recorder.

Here's an example to pass through requests to a specific endpoint:

```go
``` go
passthrough := func(req *http.Request) bool {
return req.URL.Path == "/login"
}

// Recorder options
opts := []recorder.Option{
recorder.WithCassette("fixtures/filters"),
recorder.WithPassthrough(passthrough),
recorder.WithCassette("fixtures/filters"),
recorder.WithPassthrough(passthrough),
}

r, err := recorder.New(opts...)
Expand Down

0 comments on commit cd0d4d6

Please sign in to comment.