Example #1
0
// testAuth makes a GET /api/sessions CM 1.5 request using the given authenticator and returns
// an error if it failed, nil otherwise.
// The instance flag specifies whether an instance or an account facing API request should be made.
func testAuth(auth Authenticator, client httpclient.HTTPClient, host string, instance bool) error {
	if host == "" {
		return fmt.Errorf("missing host information")
	}
	var req *http.Request
	var err error
	if instance {
		req, err = http.NewRequest("GET", buildURL(host, "api/user_data"), nil)
	} else {
		req, err = http.NewRequest("GET", buildURL(host, "api/sessions"), nil)
	}
	if err != nil {
		return err
	}
	req.Header.Set("X-Api-Version", "1.5")
	if err = auth.Sign(req); err != nil {
		return err
	}
	resp, err := client.DoHidden(req)
	if err != nil {
		return err
	}
	if resp.StatusCode != 200 {
		var body string
		if b, err := ioutil.ReadAll(resp.Body); err != nil {
			body = ": " + string(b)
		}
		return fmt.Errorf("%s%s", resp.Status, body)
	}
	return nil
}
Example #2
0
	"strings"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/onsi/gomega/ghttp"
	"github.com/rightscale/rsc/httpclient"
)

// Helper function that creates a http.Header from a name value pair
func httpHeaders(name, value string) http.Header {
	m := map[string][]string{name: []string{value}}
	return http.Header(m)
}

var _ = Describe("HTTP client", func() {
	var client httpclient.HTTPClient
	var server *ghttp.Server
	var stderr bytes.Buffer
	var req *http.Request
	var resp *http.Response
	var useHidden bool

	BeforeEach(func() {
		httpclient.NoCertCheck = true
		httpclient.OsStderr = &stderr
		server = ghttp.NewTLSServer()
		server.AppendHandlers(ghttp.CombineHandlers(
			ghttp.VerifyRequest("POST", "/redirect"),
			ghttp.RespondWith(303, "", httpHeaders("Location", server.URL()+"/ok")),
		))
		server.AppendHandlers(ghttp.CombineHandlers(