Skip to content

Commit

Permalink
expanded examples
Browse files Browse the repository at this point in the history
  • Loading branch information
johnabass committed Jul 16, 2024
1 parent ca131e2 commit e5ccff9
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions client/redirect_examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func ExampleCopyHeadersOnRedirect() {
request := httptest.NewRequest("GET", "/", nil)
previous := httptest.NewRequest("GET", "/", nil)
previous.Header.Set("Copy-Me", "value")
previous.Header.Set("Copy-Me", "copied value")

client := http.Client{
CheckRedirect: CopyHeadersOnRedirect("copy-me"), // canonicalization will happen
Expand All @@ -26,7 +26,7 @@ func ExampleCopyHeadersOnRedirect() {
fmt.Println(request.Header.Get("Copy-Me"))

// Output:
// value
// copied value
}

func ExampleMaxRedirects() {
Expand All @@ -47,3 +47,31 @@ func ExampleMaxRedirects() {
// fewer than 5 redirects
// max redirects exceeded
}

func ExampleNewCheckRedirects() {
request := httptest.NewRequest("GET", "/", nil)
previous := httptest.NewRequest("GET", "/", nil)
previous.Header.Set("Copy-Me", "copied value")

client := http.Client{
CheckRedirect: NewCheckRedirects(
MaxRedirects(10),
CopyHeadersOnRedirect("Copy-Me"),
func(*http.Request, []*http.Request) error {
fmt.Println("custom check redirect")
return nil
},
),
}

if err := client.CheckRedirect(request, []*http.Request{previous}); err != nil {
// shouldn't be output
fmt.Println(err)
}

fmt.Println(request.Header.Get("Copy-Me"))

// Output:
// custom check redirect
// copied value
}

0 comments on commit e5ccff9

Please sign in to comment.