New initializes and starts a proxy server, registers a cleanup to stop it, and returns a ProxyServer.
(t *testing.T, reqCheck func(*http.Request), waitForServerHello bool)
| 104 | // New initializes and starts a proxy server, registers a cleanup to |
| 105 | // stop it, and returns a ProxyServer. |
| 106 | func New(t *testing.T, reqCheck func(*http.Request), waitForServerHello bool) *ProxyServer { |
| 107 | t.Helper() |
| 108 | pLis, err := testutils.LocalTCPListener() |
| 109 | if err != nil { |
| 110 | t.Fatalf("failed to listen: %v", err) |
| 111 | } |
| 112 | |
| 113 | p := &ProxyServer{ |
| 114 | lis: pLis, |
| 115 | onRequest: reqCheck, |
| 116 | Addr: pLis.Addr().String(), |
| 117 | } |
| 118 | |
| 119 | // Start the proxy server. |
| 120 | go func() { |
| 121 | for { |
| 122 | in, err := p.lis.Accept() |
| 123 | if err != nil { |
| 124 | return |
| 125 | } |
| 126 | // p.handleRequest is not invoked in a goroutine because the test |
| 127 | // proxy currently supports handling only one connection at a time. |
| 128 | p.handleRequest(t, in, waitForServerHello) |
| 129 | } |
| 130 | }() |
| 131 | t.Logf("Started proxy at: %q", pLis.Addr().String()) |
| 132 | t.Cleanup(p.stop) |
| 133 | return p |
| 134 | } |