Skip to content

Commit

Permalink
simple examples for composable check redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
johnabass committed Jul 16, 2024
1 parent 0f0bd4b commit 2e4108b
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions client/redirect_examples_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// SPDX-FileCopyrightText: 2024 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0

package client

import (
"fmt"
"net/http"
"net/http/httptest"
)

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

client := http.Client{
CheckRedirect: CopyHeadersOnRedirect("copy-me"), // canonicalization will happen
}

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:
// value
}

func ExampleMaxRedirects() {
request := httptest.NewRequest("GET", "/", nil)
client := http.Client{
CheckRedirect: MaxRedirects(5),
}

if client.CheckRedirect(request, make([]*http.Request, 4)) == nil {
fmt.Println("fewer than 5 redirects")
}

if client.CheckRedirect(request, make([]*http.Request, 6)) != nil {
fmt.Println("max redirects exceeded")
}

// Output:
// fewer than 5 redirects
// max redirects exceeded
}

0 comments on commit 2e4108b

Please sign in to comment.