]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: quiet distracting test spam
authorBrad Fitzpatrick <bradfitz@golang.org>
Thu, 26 Dec 2013 21:03:30 +0000 (13:03 -0800)
committerBrad Fitzpatrick <bradfitz@golang.org>
Thu, 26 Dec 2013 21:03:30 +0000 (13:03 -0800)
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

src/pkg/net/http/client_test.go
src/pkg/net/http/cookie_test.go
src/pkg/net/http/transport_test.go

index 997d04151c224eb1579c4a061352f152f07df48b..e5ad39c77418a568adff50ac258a236d31147605 100644 (file)
@@ -373,24 +373,6 @@ func (j *TestJar) Cookies(u *url.URL) []*Cookie {
        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) {
        defer afterTest(t)
        var ts *httptest.Server
@@ -410,8 +392,8 @@ func TestRedirectCookiesJar(t *testing.T) {
 }
 
 func matchReturnedCookies(t *testing.T, expected, given []*Cookie) {
-       t.Logf("Received cookies: %v", given)
        if len(given) != len(expected) {
+               t.Logf("Received cookies: %v", given)
                t.Errorf("Expected %d cookies, got %d", len(expected), len(given))
        }
        for _, ec := range expected {
index 11b01cc5713de70f06e3c0e676c630e5833cb819..1aa9d49d96ec40d36128a0e163db7e6d751b7421 100644 (file)
@@ -5,9 +5,13 @@
 package http
 
 import (
+       "bytes"
        "encoding/json"
        "fmt"
+       "log"
+       "os"
        "reflect"
+       "strings"
        "testing"
        "time"
 )
@@ -51,12 +55,20 @@ var writeSetCookiesTests = []struct {
 }
 
 func TestWriteSetCookies(t *testing.T) {
+       defer log.SetOutput(os.Stderr)
+       var logbuf bytes.Buffer
+       log.SetOutput(&logbuf)
+
        for i, tt := range writeSetCookiesTests {
                if g, e := tt.Cookie.String(), tt.Raw; g != e {
                        t.Errorf("Test %d, expecting:\n%s\nGot:\n%s\n", i, e, g)
                        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
@@ -244,6 +256,10 @@ func TestReadCookies(t *testing.T) {
 }
 
 func TestCookieSanitizeValue(t *testing.T) {
+       defer log.SetOutput(os.Stderr)
+       var logbuf bytes.Buffer
+       log.SetOutput(&logbuf)
+
        tests := []struct {
                in, want string
        }{
@@ -257,9 +273,17 @@ func TestCookieSanitizeValue(t *testing.T) {
                        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) {
+       defer log.SetOutput(os.Stderr)
+       var logbuf bytes.Buffer
+       log.SetOutput(&logbuf)
+
        tests := []struct {
                in, want string
        }{
@@ -272,4 +296,8 @@ func TestCookieSanitizePath(t *testing.T) {
                        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)
+       }
 }
index e4df30a98de1c1da9af70d67800a1480edaf90fa..2ce2b6b5180c86c6c73d047e8cda3f7665b2cc19 100644 (file)
@@ -798,8 +798,8 @@ func TestTransportPersistConnLeak(t *testing.T) {
 
        // We expect 0 or 1 extra goroutine, empirically.  Allow up to 5.
        // Previously we were leaking one per numReq.
-       t.Logf("goroutine growth: %d -> %d -> %d (delta: %d)", n0, nhigh, nfinal, growth)
        if int(growth) > 5 {
+               t.Logf("goroutine growth: %d -> %d -> %d (delta: %d)", n0, nhigh, nfinal, growth)
                t.Error("too many new goroutines")
        }
 }