New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide custom CheckRedirect
for r2.Request.Client
.
#175
Labels
enhancement
New feature or request
Comments
As a way to test redirects in general: package main
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"github.com/blend/go-sdk/r2"
)
func main() {
path := "/v1/auth/token/lookup"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == path {
http.Redirect(w, r, "/more", http.StatusTemporaryRedirect)
return
}
fmt.Fprintln(w, "Hello, client")
}))
defer ts.Close()
u, err := url.Parse(ts.URL)
if err != nil {
panic(err)
}
// Do a GET.
fmt.Println("--- Doing a GET ---")
opts := []r2.Option{
r2.OptScheme("http"),
r2.OptHeaderValue("X-Vault-Token", "bar"),
r2.OptHost(u.Host),
r2.OptGet(),
r2.OptPathf(path),
}
body, res, err := r2.New("", opts...).Bytes()
if err != nil {
panic(err)
}
fmt.Printf("body: %q\n\n%#v\n", string(body), res)
// Do a POST.
fmt.Println("--- Doing a POST ---")
opts = []r2.Option{
r2.OptScheme("http"),
r2.OptHeaderValue("X-Vault-Token", "bar"),
r2.OptHost(u.Host),
r2.OptPost(),
r2.OptPathf(path),
r2.OptJSONBody(map[string]string{"token": "foo"}),
}
body, res, err = r2.New("", opts...).Bytes()
if err != nil {
panic(err)
}
fmt.Printf("body: %q\n\n%#v\n", string(body), res)
} which produces
|
Also more relevant (directly) to this issue would be the case of too many redirects: package main
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"github.com/blend/go-sdk/r2"
)
func main() {
path := "/v1/auth/token/lookup"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, path, http.StatusTemporaryRedirect)
}))
defer ts.Close()
u, err := url.Parse(ts.URL)
if err != nil {
panic(err)
}
// Do a GET.
fmt.Println("--- Doing a GET ---")
opts := []r2.Option{
r2.OptScheme("http"),
r2.OptHeaderValue("X-Vault-Token", "bar"),
r2.OptHost(u.Host),
r2.OptGet(),
r2.OptPathf(path),
}
_, _, err = r2.New("", opts...).Bytes()
if err == nil {
panic("SHOULD ERR")
}
unwrapped, ok := err.(*url.Error)
if !ok {
panic("SHOULD UNWRAP")
}
fmt.Printf("%#v\n", unwrapped)
fmt.Printf("%#v\n", unwrapped.Err)
} which produces
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
By default, if a
(net/http).Client
hits 10 redirects, it will give up with:but this default can be overridden on a
Client
.The text was updated successfully, but these errors were encountered: