github.com/4ad/go@v0.0.0-20161219182952-69a12818b605/src/net/http/httptrace/trace_test.go (about) 1 // Copyright 2016 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file.h 4 5 package httptrace 6 7 import ( 8 "bytes" 9 "testing" 10 ) 11 12 func TestCompose(t *testing.T) { 13 var buf bytes.Buffer 14 var testNum int 15 16 connectStart := func(b byte) func(network, addr string) { 17 return func(network, addr string) { 18 if addr != "addr" { 19 t.Errorf(`%d. args for %q case = %q, %q; want addr of "addr"`, testNum, b, network, addr) 20 } 21 buf.WriteByte(b) 22 } 23 } 24 25 tests := [...]struct { 26 trace, old *ClientTrace 27 want string 28 }{ 29 0: { 30 want: "T", 31 trace: &ClientTrace{ 32 ConnectStart: connectStart('T'), 33 }, 34 }, 35 1: { 36 want: "TO", 37 trace: &ClientTrace{ 38 ConnectStart: connectStart('T'), 39 }, 40 old: &ClientTrace{ConnectStart: connectStart('O')}, 41 }, 42 2: { 43 want: "O", 44 trace: &ClientTrace{}, 45 old: &ClientTrace{ConnectStart: connectStart('O')}, 46 }, 47 } 48 for i, tt := range tests { 49 testNum = i 50 buf.Reset() 51 52 tr := *tt.trace 53 tr.compose(tt.old) 54 if tr.ConnectStart != nil { 55 tr.ConnectStart("net", "addr") 56 } 57 if got := buf.String(); got != tt.want { 58 t.Errorf("%d. got = %q; want %q", i, got, tt.want) 59 } 60 } 61 62 }