github.com/aloncn/graphics-go@v0.0.1/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 "sync" 13 "time" 14 ) 15 16 var ( 17 DefaultUserAgent = defaultUserAgent 18 NewLoggingConn = newLoggingConn 19 ExportAppendTime = appendTime 20 ExportRefererForURL = refererForURL 21 ExportServerNewConn = (*Server).newConn 22 ExportCloseWriteAndWait = (*conn).closeWriteAndWait 23 ExportErrRequestCanceled = errRequestCanceled 24 ExportErrRequestCanceledConn = errRequestCanceledConn 25 ExportServeFile = serveFile 26 ExportHttp2ConfigureTransport = http2ConfigureTransport 27 ExportHttp2ConfigureServer = http2ConfigureServer 28 ) 29 30 func init() { 31 // We only want to pay for this cost during testing. 32 // When not under test, these values are always nil 33 // and never assigned to. 34 testHookMu = new(sync.Mutex) 35 } 36 37 var ( 38 SetEnterRoundTripHook = hookSetter(&testHookEnterRoundTrip) 39 SetTestHookWaitResLoop = hookSetter(&testHookWaitResLoop) 40 SetRoundTripRetried = hookSetter(&testHookRoundTripRetried) 41 ) 42 43 func SetReadLoopBeforeNextReadHook(f func()) { 44 testHookMu.Lock() 45 defer testHookMu.Unlock() 46 unnilTestHook(&f) 47 testHookReadLoopBeforeNextRead = f 48 } 49 50 // SetPendingDialHooks sets the hooks that run before and after handling 51 // pending dials. 52 func SetPendingDialHooks(before, after func()) { 53 unnilTestHook(&before) 54 unnilTestHook(&after) 55 testHookPrePendingDial, testHookPostPendingDial = before, after 56 } 57 58 func SetTestHookServerServe(fn func(*Server, net.Listener)) { testHookServerServe = fn } 59 60 func NewTestTimeoutHandler(handler Handler, ch <-chan time.Time) Handler { 61 return &timeoutHandler{ 62 handler: handler, 63 testTimeout: ch, 64 // (no body) 65 } 66 } 67 68 func ResetCachedEnvironment() { 69 httpProxyEnv.reset() 70 httpsProxyEnv.reset() 71 noProxyEnv.reset() 72 } 73 74 func (t *Transport) NumPendingRequestsForTesting() int { 75 t.reqMu.Lock() 76 defer t.reqMu.Unlock() 77 return len(t.reqCanceler) 78 } 79 80 func (t *Transport) IdleConnKeysForTesting() (keys []string) { 81 keys = make([]string, 0) 82 t.idleMu.Lock() 83 defer t.idleMu.Unlock() 84 if t.idleConn == nil { 85 return 86 } 87 for key := range t.idleConn { 88 keys = append(keys, key.String()) 89 } 90 return 91 } 92 93 func (t *Transport) IdleConnCountForTesting(cacheKey string) int { 94 t.idleMu.Lock() 95 defer t.idleMu.Unlock() 96 if t.idleConn == nil { 97 return 0 98 } 99 for k, conns := range t.idleConn { 100 if k.String() == cacheKey { 101 return len(conns) 102 } 103 } 104 return 0 105 } 106 107 func (t *Transport) IdleConnChMapSizeForTesting() int { 108 t.idleMu.Lock() 109 defer t.idleMu.Unlock() 110 return len(t.idleConnCh) 111 } 112 113 func (t *Transport) IsIdleForTesting() bool { 114 t.idleMu.Lock() 115 defer t.idleMu.Unlock() 116 return t.wantIdle 117 } 118 119 func (t *Transport) RequestIdleConnChForTesting() { 120 t.getIdleConnCh(connectMethod{nil, "http", "example.com"}) 121 } 122 123 func (t *Transport) PutIdleTestConn() bool { 124 c, _ := net.Pipe() 125 return t.tryPutIdleConn(&persistConn{ 126 t: t, 127 conn: c, // dummy 128 closech: make(chan struct{}), // so it can be closed 129 cacheKey: connectMethodKey{"", "http", "example.com"}, 130 }) == nil 131 } 132 133 // All test hooks must be non-nil so they can be called directly, 134 // but the tests use nil to mean hook disabled. 135 func unnilTestHook(f *func()) { 136 if *f == nil { 137 *f = nop 138 } 139 } 140 141 func hookSetter(dst *func()) func(func()) { 142 return func(fn func()) { 143 unnilTestHook(&fn) 144 *dst = fn 145 } 146 }