Comments (3)
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
--- Doing a GET ---
body: "Hello, client\n"
&http.Response{Status:"200 OK", StatusCode:200, Proto:"HTTP/1.1", ProtoMajor:1, ProtoMinor:1, Header:http.Header{"Content-Length":[]string{"14"}, "Content-Type":[]string{"text/plain; charset=utf-8"}, "Date":[]string{"Sat, 01 Feb 2020 00:01:16 GMT"}}, Body:(*http.bodyEOFSignal)(0xc0001bc100), ContentLength:14, TransferEncoding:[]string(nil), Close:false, Uncompressed:false, Trailer:http.Header(nil), Request:(*http.Request)(0xc0001c8000), TLS:(*tls.ConnectionState)(nil)}
--- Doing a POST ---
body: ""
&http.Response{Status:"307 Temporary Redirect", StatusCode:307, Proto:"HTTP/1.1", ProtoMajor:1, ProtoMinor:1, Header:http.Header{"Content-Length":[]string{"0"}, "Date":[]string{"Sat, 01 Feb 2020 00:01:16 GMT"}, "Location":[]string{"/more"}}, Body:http.noBody{}, ContentLength:0, TransferEncoding:[]string(nil), Close:false, Uncompressed:false, Trailer:http.Header(nil), Request:(*http.Request)(0xc000176420), TLS:(*tls.ConnectionState)(nil)}
from go-sdk.
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
--- Doing a GET ---
&url.Error{Op:"Get", URL:"/v1/auth/token/lookup", Err:(*errors.errorString)(0xc0001d4220)}
&errors.errorString{s:"stopped after 10 redirects"}
from go-sdk.
Closing this since #176 is merged. I opened #177 to track just the 307 for POST
bits.
from go-sdk.
Related Issues (20)
- v2 bug: web.ViewCache() incorrectly claims to be cached after Parse failures HOT 1
- v2 logger swallows panic caused by `nil` HTTP request
- v3: `r2.OptTLSRootCAs()` has "typed nil" issue when checking for a nil transport HOT 1
- `r2` sends invalid HTTP requests if `Path` does not have a leading `/`. HOT 5
- v3: `web.Ctx.CookieDomain()` includes port
- Span type and span operation name for `oauthtrace` are generic HTTP
- [assert] Function doesn't compare equal to itself HOT 2
- `r2.Request` should follow 307 on non-GET HOT 3
- Off-by-one error in line numbers in panic output from within a goroutine
- `make dev-deps` failing to install goreleaser HOT 4
- `log.Trigger()` buries panics HOT 2
- Port internal SDK changes to public `go-sdk` HOT 1
- crypto, only KeySize 32 is supported HOT 1
- `testutil` suite run silently aborts on panics in `OptBefore` hooks HOT 1
- Outdated modules that contains volunerabilities HOT 1
- SNYK-GOLANG-GITHUBCOMJACKCPGPROTO3V2-1316251
- dependency error go sdk HOT 1
- When ever Particular key has empty value in XFCC Header, It is parsing wrongly
- selector/runes.go: isAlpha not robust
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-sdk.