Esempio n. 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
}
Esempio n. 2
0
			ghttp.RespondWith(303, "", httpHeaders("Location", server.URL()+"/ok")),
		))
		server.AppendHandlers(ghttp.CombineHandlers(
			ghttp.VerifyRequest("GET", "/ok"),
			ghttp.RespondWith(200, "OK"),
		))
	})

	JustBeforeEach(func() {
		stderr.Reset()
		var err error
		var body = strings.NewReader(`{"foo":"bar"}`)
		req, err = http.NewRequest("POST", server.URL()+"/redirect", body)
		Ω(err).ShouldNot(HaveOccurred())
		if useHidden {
			resp, err = client.DoHidden(req)
		} else {
			resp, err = client.Do(req)
		}
		Ω(err).ShouldNot(HaveOccurred())
	})

	AfterEach(func() {
		server.Close()
	})

	Context("created with New", func() {

		BeforeEach(func() {
			client = httpclient.New()
		})