Giter Club home page Giter Club logo

Comments (3)

dhermes avatar dhermes commented on August 30, 2024

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.

dhermes avatar dhermes commented on August 30, 2024

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.

dhermes avatar dhermes commented on August 30, 2024

Closing this since #176 is merged. I opened #177 to track just the 307 for POST bits.

from go-sdk.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.