220
227
res, err := c.Get(ts.URL)
229
t.Fatalf("Get error: %v", err)
221
232
finalUrl := res.Request.URL.String()
222
233
if e, g := "<nil>", fmt.Sprintf("%v", err); e != g {
223
234
t.Errorf("with custom client, expected error %q, got %q", e, g)
232
243
checkErr = errors.New("no redirects allowed")
233
244
res, err = c.Get(ts.URL)
234
finalUrl = res.Request.URL.String()
235
if e, g := "Get /?n=1: no redirects allowed", fmt.Sprintf("%v", err); e != g {
236
t.Errorf("with redirects forbidden, expected error %q, got %q", e, g)
245
if urlError, ok := err.(*url.Error); !ok || urlError.Err != checkErr {
246
t.Errorf("with redirects forbidden, expected a *url.Error with our 'no redirects allowed' error inside; got %#v (%q)", err, err)
249
t.Fatalf("Expected a non-nil Response on CheckRedirect failure (http://golang.org/issue/3795)")
252
if res.Header.Get("Location") == "" {
253
t.Errorf("no Location header in Response")
257
func TestPostRedirects(t *testing.T) {
263
var ts *httptest.Server
264
ts = httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
266
fmt.Fprintf(&log.Buffer, "%s %s ", r.Method, r.RequestURI)
268
if v := r.URL.Query().Get("code"); v != "" {
269
code, _ := strconv.Atoi(v)
271
w.Header().Set("Location", ts.URL)
279
want int // response code
287
for _, tt := range tests {
288
res, err := Post(ts.URL+tt.suffix, "text/plain", strings.NewReader("Some content"))
292
if res.StatusCode != tt.want {
293
t.Errorf("POST %s: status code = %d; want %d", tt.suffix, res.StatusCode, tt.want)
299
want := "POST / POST /?code=301 POST /?code=302 GET / POST /?code=303 GET / POST /?code=404 "
301
t.Errorf("Log differs.\n Got: %q\nWant: %q", got, want)
279
344
req, _ := NewRequest("GET", us, nil)
280
345
client.Do(req) // Note: doesn't hit network
281
346
matchReturnedCookies(t, expectedCookies, tr.req.Cookies())
348
req, _ = NewRequest("POST", us, nil)
349
client.Do(req) // Note: doesn't hit network
350
matchReturnedCookies(t, expectedCookies, tr.req.Cookies())
284
353
// Just enough correctness for our redirect tests. Uses the URL.Host as the
320
393
func TestRedirectCookiesJar(t *testing.T) {
321
395
var ts *httptest.Server
322
396
ts = httptest.NewServer(echoCookiesRedirectHandler)
325
c.Jar = &TestJar{perURL: make(map[string][]*Cookie)}
326
401
u, _ := url.Parse(ts.URL)
327
402
c.Jar.SetCookies(u, []*Cookie{expectedCookies[0]})
328
resp, _ := c.Get(ts.URL)
403
resp, err := c.Get(ts.URL)
405
t.Fatalf("Get: %v", err)
329
408
matchReturnedCookies(t, expectedCookies, resp.Cookies())
430
func TestJarCalls(t *testing.T) {
432
ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
433
pathSuffix := r.RequestURI[1:]
434
if r.RequestURI == "/nosetcookie" {
435
return // dont set cookies for this path
437
SetCookie(w, &Cookie{Name: "name" + pathSuffix, Value: "val" + pathSuffix})
438
if r.RequestURI == "/" {
439
Redirect(w, r, "http://secondhost.fake/secondpath", 302)
443
jar := new(RecordingJar)
446
Transport: &Transport{
447
Dial: func(_ string, _ string) (net.Conn, error) {
448
return net.Dial("tcp", ts.Listener.Addr().String())
452
_, err := c.Get("http://firsthost.fake/")
456
_, err = c.Get("http://firsthost.fake/nosetcookie")
460
got := jar.log.String()
461
want := `Cookies("http://firsthost.fake/")
462
SetCookie("http://firsthost.fake/", [name=val])
463
Cookies("http://secondhost.fake/secondpath")
464
SetCookie("http://secondhost.fake/secondpath", [namesecondpath=valsecondpath])
465
Cookies("http://firsthost.fake/nosetcookie")
468
t.Errorf("Got Jar calls:\n%s\nWant:\n%s", got, want)
472
// RecordingJar keeps a log of calls made to it, without
473
// tracking any cookies.
474
type RecordingJar struct {
479
func (j *RecordingJar) SetCookies(u *url.URL, cookies []*Cookie) {
480
j.logf("SetCookie(%q, %v)\n", u, cookies)
483
func (j *RecordingJar) Cookies(u *url.URL) []*Cookie {
484
j.logf("Cookies(%q)\n", u)
488
func (j *RecordingJar) logf(format string, args ...interface{}) {
491
fmt.Fprintf(&j.log, format, args...)
351
494
func TestStreamingGet(t *testing.T) {
352
496
say := make(chan string)
353
497
ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
354
498
w.(Flusher).Flush()
446
592
InsecureSkipVerify: insecure,
595
defer tr.CloseIdleConnections()
449
596
c := &Client{Transport: tr}
450
_, err := c.Get(ts.URL)
597
res, err := c.Get(ts.URL)
451
598
if (err == nil) != insecure {
452
599
t.Errorf("insecure=%v: got unexpected err=%v", insecure, err)
457
607
func TestClientErrorWithRequestURI(t *testing.T) {
458
609
req, _ := NewRequest("GET", "http://localhost:1234/", nil)
459
610
req.RequestURI = "/this/field/is/illegal/and/should/error/"
460
611
_, err := DefaultClient.Do(req)
465
616
t.Errorf("wanted error mentioning RequestURI; got error: %v", err)
620
func newTLSTransport(t *testing.T, ts *httptest.Server) *Transport {
621
certs := x509.NewCertPool()
622
for _, c := range ts.TLS.Certificates {
623
roots, err := x509.ParseCertificates(c.Certificate[len(c.Certificate)-1])
625
t.Fatalf("error parsing server's root cert: %v", err)
627
for _, root := range roots {
632
TLSClientConfig: &tls.Config{RootCAs: certs},
636
func TestClientWithCorrectTLSServerName(t *testing.T) {
638
ts := httptest.NewTLSServer(HandlerFunc(func(w ResponseWriter, r *Request) {
639
if r.TLS.ServerName != "127.0.0.1" {
640
t.Errorf("expected client to set ServerName 127.0.0.1, got: %q", r.TLS.ServerName)
645
c := &Client{Transport: newTLSTransport(t, ts)}
646
if _, err := c.Get(ts.URL); err != nil {
647
t.Fatalf("expected successful TLS connection, got error: %v", err)
651
func TestClientWithIncorrectTLSServerName(t *testing.T) {
653
ts := httptest.NewTLSServer(HandlerFunc(func(w ResponseWriter, r *Request) {}))
656
trans := newTLSTransport(t, ts)
657
trans.TLSClientConfig.ServerName = "badserver"
658
c := &Client{Transport: trans}
659
_, err := c.Get(ts.URL)
661
t.Fatalf("expected an error")
663
if !strings.Contains(err.Error(), "127.0.0.1") || !strings.Contains(err.Error(), "badserver") {
664
t.Errorf("wanted error mentioning 127.0.0.1 and badserver; got error: %v", err)
668
// Verify Response.ContentLength is populated. http://golang.org/issue/4126
669
func TestClientHeadContentLength(t *testing.T) {
671
ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
672
if v := r.FormValue("cl"); v != "" {
673
w.Header().Set("Content-Length", v)
685
for _, tt := range tests {
686
req, _ := NewRequest("HEAD", ts.URL+tt.suffix, nil)
687
res, err := DefaultClient.Do(req)
691
if res.ContentLength != tt.want {
692
t.Errorf("Content-Length = %d; want %d", res.ContentLength, tt.want)
694
bs, err := ioutil.ReadAll(res.Body)
699
t.Errorf("Unexpected content: %q", bs)