Beispiel #1
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)
}
Beispiel #2
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
		}
	}
}
Beispiel #3
0
func testUserAgent(t *testing.T, c *httpclient.Client, exp string) {
	ep := urlutil.MustJoin(httpBin, "/user-agent")
	resp, err := c.Get(ep)
	if err != nil {
		t.Fatal(err)
	}
	defer resp.Close()
	var m map[string]interface{}
	if err := resp.DecodeJSON(&m); err != nil {
		t.Fatal(err)
	}
	ua := m["user-agent"].(string)
	if idx := strings.Index(ua, " AppEngine-Google"); idx >= 0 {
		ua = ua[:idx]
	}
	if ua != exp {
		t.Errorf("expecting User-Agent %q, got %q instead", exp, ua)
	}
}
Beispiel #4
0
func (a *App) URL(path string) string {
	return urlutil.MustJoin(Base, path)
}
Beispiel #5
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)
}