github.com/hbdrawn/golang@v0.0.0-20141214014649-6b835209aba2/src/net/http/export_test.go (about) 1 // Copyright 2011 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. 4 5 // Bridge package to expose http internals to tests in the http_test 6 // package. 7 8 package http 9 10 import ( 11 "net" 12 "net/url" 13 "time" 14 ) 15 16 func NewLoggingConn(baseName string, c net.Conn) net.Conn { 17 return newLoggingConn(baseName, c) 18 } 19 20 var ExportAppendTime = appendTime 21 22 func (t *Transport) NumPendingRequestsForTesting() int { 23 t.reqMu.Lock() 24 defer t.reqMu.Unlock() 25 return len(t.reqCanceler) 26 } 27 28 func (t *Transport) IdleConnKeysForTesting() (keys []string) { 29 keys = make([]string, 0) 30 t.idleMu.Lock() 31 defer t.idleMu.Unlock() 32 if t.idleConn == nil { 33 return 34 } 35 for key := range t.idleConn { 36 keys = append(keys, key.String()) 37 } 38 return 39 } 40 41 func (t *Transport) IdleConnCountForTesting(cacheKey string) int { 42 t.idleMu.Lock() 43 defer t.idleMu.Unlock() 44 if t.idleConn == nil { 45 return 0 46 } 47 for k, conns := range t.idleConn { 48 if k.String() == cacheKey { 49 return len(conns) 50 } 51 } 52 return 0 53 } 54 55 func (t *Transport) IdleConnChMapSizeForTesting() int { 56 t.idleMu.Lock() 57 defer t.idleMu.Unlock() 58 return len(t.idleConnCh) 59 } 60 61 func (t *Transport) IsIdleForTesting() bool { 62 t.idleMu.Lock() 63 defer t.idleMu.Unlock() 64 return t.wantIdle 65 } 66 67 func (t *Transport) RequestIdleConnChForTesting() { 68 t.getIdleConnCh(connectMethod{nil, "http", "example.com"}) 69 } 70 71 func (t *Transport) PutIdleTestConn() bool { 72 c, _ := net.Pipe() 73 return t.putIdleConn(&persistConn{ 74 t: t, 75 conn: c, // dummy 76 closech: make(chan struct{}), // so it can be closed 77 cacheKey: connectMethodKey{"", "http", "example.com"}, 78 }) 79 } 80 81 func NewTestTimeoutHandler(handler Handler, ch <-chan time.Time) Handler { 82 f := func() <-chan time.Time { 83 return ch 84 } 85 return &timeoutHandler{handler, f, ""} 86 } 87 88 func ResetCachedEnvironment() { 89 httpProxyEnv.reset() 90 httpsProxyEnv.reset() 91 noProxyEnv.reset() 92 } 93 94 var DefaultUserAgent = defaultUserAgent 95 96 func ExportRefererForURL(lastReq, newReq *url.URL) string { 97 return refererForURL(lastReq, newReq) 98 } 99 100 // SetPendingDialHooks sets the hooks that run before and after handling 101 // pending dials. 102 func SetPendingDialHooks(before, after func()) { 103 prePendingDial, postPendingDial = before, after 104 } 105 106 var ExportServerNewConn = (*Server).newConn 107 108 var ExportCloseWriteAndWait = (*conn).closeWriteAndWait