Пример #1
0
func ExampleIter() {
	// Passing nil only works on non-App Engine and while
	// running tests. Usually you should pass a *app.Context
	// to httpclient.New.
	c := httpclient.New(nil)
	req, err := http.NewRequest("GET", "http://httpbin.org/relative-redirect/3", nil)
	if err != nil {
		panic(err)
	}
	iter := c.Iter(req)
	// Don't forget to close the Iter after you're done with it
	defer iter.Close()
	var urls []string
	for iter.Next() {
		urls = append(urls, iter.Response().URL().String())
	}
	// iter.Assert() could also be used here
	if err := iter.Err(); err != nil {
		panic(err)
	}
	fmt.Println("Last", iter.Response().URL())
	fmt.Println("Intermediate", urls)
	// Output:
	// Last http://httpbin.org/get
	// Intermediate [http://httpbin.org/relative-redirect/3 http://httpbin.org/relative-redirect/2 http://httpbin.org/relative-redirect/1]
}
Пример #2
0
func TestProxy(t *testing.T) {
	c := httpclient.New(nil)
	if !c.SupportsProxy() {
		t.Skipf("current run environment does not support support proxies")
	}
	const addr = "127.0.0.1:12345"
	proxy := goproxy.NewProxyHttpServer()
	count := 0
	proxy.OnRequest().DoFunc(
		func(r *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
			count++
			return r, nil
		})
	go func() {
		http.ListenAndServe(addr, proxy)
	}()
	time.Sleep(time.Millisecond)
	c.SetProxy(func(_ *http.Request) (*url.URL, error) {
		return url.Parse("http://" + addr)
	})
	testUserAgent(t, c, httpclient.DefaultUserAgent)
	if count != 1 {
		t.Errorf("expecting 1 proxy request, got %d instead", count)
	}
	testUserAgent(t, c.Clone(nil), httpclient.DefaultUserAgent)
	if count != 2 {
		t.Errorf("expecting 2 proxy request, got %d instead", count)
	}
}
Пример #3
0
// New returns a new oAuth 2 Client. The authorization parameter
// must be the URL for redirecting a user to perform client
// authorization. Exchange is the URL for exchanging an oAuth 2
// code for a token.
func New(authorization string, exchange string) *Client {
	return &Client{
		AuthorizationURL: authorization,
		ExchangeURL:      exchange,
		HTTPClient:       httpclient.New(nil),
	}
}
Пример #4
0
func TestUserAgent(t *testing.T) {
	const ua = "Gondolier"
	c := httpclient.New(nil)
	testUserAgent(t, c, httpclient.DefaultUserAgent)
	c.SetUserAgent(ua)
	testUserAgent(t, c, ua)
	testUserAgent(t, c.Clone(nil), ua)
}
Пример #5
0
func (a *App) client() *httpclient.Client {
	if a.Client != nil {
		return a.Client
	}
	if a.httpClient == nil {
		a.httpClient = httpclient.New(nil)
	}
	return a.httpClient
}
Пример #6
0
func TestGetForm(t *testing.T) {
	data := map[string]string{"a": "b", "c": "d"}
	f := httpclient.New(nil).GetForm
	testForm(t, f, urlutil.MustJoin(httpBin, "/get"), data, data)
	expect := map[string]string{"e": "f"}
	for k, v := range data {
		expect[k] = v
	}
	testForm(t, f, urlutil.MustJoin(httpBin, "/get?e=f"), data, expect)
}
Пример #7
0
func (c *Consumer) client() *httpclient.Client {
	if c.Client != nil {
		c.preventRedirects(c.Client)
		return c.Client
	}
	if c.httpClient == nil {
		c.httpClient = httpclient.New(nil)
		c.preventRedirects(c.httpClient)
	}
	return c.httpClient
}
Пример #8
0
func TestRedirect(t *testing.T) {
	start := urlutil.MustJoin(httpBin, "/redirect/6")
	end := urlutil.MustJoin(httpBin, "/get")
	c := httpclient.New(nil)
	resp, err := c.Get(start)
	if err != nil {
		t.Fatal(err)
	}
	defer resp.Close()
	if u := resp.Request.URL.String(); u != end {
		t.Errorf("expecting final url %q, got %q instead", end, u)
	}
	cur := redirNumber(t, start)
	next := start
	for {
		req, err := http.NewRequest("GET", next, nil)
		if err != nil {
			t.Fatal(err)
		}
		resp, err := c.Trip(req)
		if err != nil {
			t.Fatal(err)
		}
		if cur > 0 {
			redir, err := resp.Redirect()
			if err != nil {
				t.Fatal(err)
			}
			cur--
			if cur > 0 {
				rn := redirNumber(t, redir)
				if rn != cur {
					t.Fatalf("expecting redirect %d, got %d instead", cur, rn)
				}
			}
			next = redir
		} else {
			if resp.IsRedirect() {
				t.Error("unexpected redirect")
			}
			if u := resp.Request.URL.String(); u != end {
				t.Errorf("expecting final url %q, got %q instead", end, u)
			}
			break
		}
	}
}
Пример #9
0
func defaultFetchImage(ctx *app.Context, url string) (string, string, error) {
	resp, err := httpclient.New(ctx).Get(url)
	if err != nil {
		return "", "", err
	}
	defer resp.Close()
	data, err := resp.ReadAll()
	if err != nil {
		return "", "", err
	}
	_, format, err := image.DecodeConfig(bytes.NewReader(data))
	if err != nil {
		return "", "", err
	}
	bs := ctx.Blobstore()
	id, err := bs.Store(data, nil)
	if err != nil {
		return "", "", err
	}
	return id, strings.ToLower(format), nil
}
Пример #10
0
func (c *Client) client() *httpclient.Client {
	if c.HTTPClient == nil {
		c.HTTPClient = httpclient.New(nil)
	}
	return c.HTTPClient
}
Пример #11
0
func TestPostForm(t *testing.T) {
	data := map[string]string{"a": "b", "c": "d"}
	testForm(t, httpclient.New(nil).PostForm, urlutil.MustJoin(httpBin, "/post"), data, data)
}