Commit 2ccc9a96 authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

net/http: quiet distracting test spam

Capture log output (and test it while at it),
and quiet unnecessary t.Logf.

R=golang-codereviews, iant
CC=golang-codereviews
https://golang.org/cl/45850043
parent 1fa02060
...@@ -373,24 +373,6 @@ func (j *TestJar) Cookies(u *url.URL) []*Cookie { ...@@ -373,24 +373,6 @@ func (j *TestJar) Cookies(u *url.URL) []*Cookie {
return j.perURL[u.Host] return j.perURL[u.Host]
} }
func TestRedirectCookiesOnRequest(t *testing.T) {
defer afterTest(t)
var ts *httptest.Server
ts = httptest.NewServer(echoCookiesRedirectHandler)
defer ts.Close()
c := &Client{}
req, _ := NewRequest("GET", ts.URL, nil)
req.AddCookie(expectedCookies[0])
// TODO: Uncomment when an implementation of a RFC6265 cookie jar lands.
_ = c
// resp, _ := c.Do(req)
// matchReturnedCookies(t, expectedCookies, resp.Cookies())
req, _ = NewRequest("GET", ts.URL, nil)
// resp, _ = c.Do(req)
// matchReturnedCookies(t, expectedCookies[1:], resp.Cookies())
}
func TestRedirectCookiesJar(t *testing.T) { func TestRedirectCookiesJar(t *testing.T) {
defer afterTest(t) defer afterTest(t)
var ts *httptest.Server var ts *httptest.Server
...@@ -410,8 +392,8 @@ func TestRedirectCookiesJar(t *testing.T) { ...@@ -410,8 +392,8 @@ func TestRedirectCookiesJar(t *testing.T) {
} }
func matchReturnedCookies(t *testing.T, expected, given []*Cookie) { func matchReturnedCookies(t *testing.T, expected, given []*Cookie) {
t.Logf("Received cookies: %v", given)
if len(given) != len(expected) { if len(given) != len(expected) {
t.Logf("Received cookies: %v", given)
t.Errorf("Expected %d cookies, got %d", len(expected), len(given)) t.Errorf("Expected %d cookies, got %d", len(expected), len(given))
} }
for _, ec := range expected { for _, ec := range expected {
......
...@@ -5,9 +5,13 @@ ...@@ -5,9 +5,13 @@
package http package http
import ( import (
"bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"os"
"reflect" "reflect"
"strings"
"testing" "testing"
"time" "time"
) )
...@@ -51,12 +55,20 @@ var writeSetCookiesTests = []struct { ...@@ -51,12 +55,20 @@ var writeSetCookiesTests = []struct {
} }
func TestWriteSetCookies(t *testing.T) { func TestWriteSetCookies(t *testing.T) {
defer log.SetOutput(os.Stderr)
var logbuf bytes.Buffer
log.SetOutput(&logbuf)
for i, tt := range writeSetCookiesTests { for i, tt := range writeSetCookiesTests {
if g, e := tt.Cookie.String(), tt.Raw; g != e { if g, e := tt.Cookie.String(), tt.Raw; g != e {
t.Errorf("Test %d, expecting:\n%s\nGot:\n%s\n", i, e, g) t.Errorf("Test %d, expecting:\n%s\nGot:\n%s\n", i, e, g)
continue continue
} }
} }
if got, sub := logbuf.String(), "dropping domain attribute"; !strings.Contains(got, sub) {
t.Errorf("Expected substring %q in log output. Got:\n%s", sub, got)
}
} }
type headerOnlyResponseWriter Header type headerOnlyResponseWriter Header
...@@ -244,6 +256,10 @@ func TestReadCookies(t *testing.T) { ...@@ -244,6 +256,10 @@ func TestReadCookies(t *testing.T) {
} }
func TestCookieSanitizeValue(t *testing.T) { func TestCookieSanitizeValue(t *testing.T) {
defer log.SetOutput(os.Stderr)
var logbuf bytes.Buffer
log.SetOutput(&logbuf)
tests := []struct { tests := []struct {
in, want string in, want string
}{ }{
...@@ -257,9 +273,17 @@ func TestCookieSanitizeValue(t *testing.T) { ...@@ -257,9 +273,17 @@ func TestCookieSanitizeValue(t *testing.T) {
t.Errorf("sanitizeCookieValue(%q) = %q; want %q", tt.in, got, tt.want) t.Errorf("sanitizeCookieValue(%q) = %q; want %q", tt.in, got, tt.want)
} }
} }
if got, sub := logbuf.String(), "dropping invalid bytes"; !strings.Contains(got, sub) {
t.Errorf("Expected substring %q in log output. Got:\n%s", sub, got)
}
} }
func TestCookieSanitizePath(t *testing.T) { func TestCookieSanitizePath(t *testing.T) {
defer log.SetOutput(os.Stderr)
var logbuf bytes.Buffer
log.SetOutput(&logbuf)
tests := []struct { tests := []struct {
in, want string in, want string
}{ }{
...@@ -272,4 +296,8 @@ func TestCookieSanitizePath(t *testing.T) { ...@@ -272,4 +296,8 @@ func TestCookieSanitizePath(t *testing.T) {
t.Errorf("sanitizeCookiePath(%q) = %q; want %q", tt.in, got, tt.want) t.Errorf("sanitizeCookiePath(%q) = %q; want %q", tt.in, got, tt.want)
} }
} }
if got, sub := logbuf.String(), "dropping invalid bytes"; !strings.Contains(got, sub) {
t.Errorf("Expected substring %q in log output. Got:\n%s", sub, got)
}
} }
...@@ -798,8 +798,8 @@ func TestTransportPersistConnLeak(t *testing.T) { ...@@ -798,8 +798,8 @@ func TestTransportPersistConnLeak(t *testing.T) {
// We expect 0 or 1 extra goroutine, empirically. Allow up to 5. // We expect 0 or 1 extra goroutine, empirically. Allow up to 5.
// Previously we were leaking one per numReq. // Previously we were leaking one per numReq.
t.Logf("goroutine growth: %d -> %d -> %d (delta: %d)", n0, nhigh, nfinal, growth)
if int(growth) > 5 { if int(growth) > 5 {
t.Logf("goroutine growth: %d -> %d -> %d (delta: %d)", n0, nhigh, nfinal, growth)
t.Error("too many new goroutines") t.Error("too many new goroutines")
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment