MCPcopy
hub / github.com/tailscale/tailscale / testControlHTTP

Function testControlHTTP

control/controlhttp/http_test.go:148–326  ·  view source on GitHub ↗
(t *testing.T, param httpTestParam)

Source from the content-addressed store, hash-verified

146}
147
148func testControlHTTP(t *testing.T, param httpTestParam) {
149 proxy := param.proxy
150 client, server := key.NewMachine(), key.NewMachine()
151
152 bus := eventbustest.NewBus(t)
153
154 const testProtocolVersion = 1
155 const earlyWriteMsg = "Hello, world!"
156 sch := make(chan serverResult, 1)
157 handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
158 var earlyWriteFn func(protocolVersion int, w io.Writer) error
159 if param.doEarlyWrite {
160 earlyWriteFn = func(protocolVersion int, w io.Writer) error {
161 if protocolVersion != testProtocolVersion {
162 t.Errorf("unexpected protocol version %d; want %d", protocolVersion, testProtocolVersion)
163 return fmt.Errorf("unexpected protocol version %d; want %d", protocolVersion, testProtocolVersion)
164 }
165 _, err := io.WriteString(w, earlyWriteMsg)
166 return err
167 }
168 }
169 conn, err := controlhttpserver.AcceptHTTP(context.Background(), w, r, server, earlyWriteFn)
170 if err != nil {
171 log.Print(err)
172 }
173 res := serverResult{
174 err: err,
175 }
176 if conn != nil {
177 res.clientAddr = conn.RemoteAddr().String()
178 res.version = conn.ProtocolVersion()
179 res.peer = conn.Peer()
180 res.conn = conn
181 }
182 sch <- res
183 })
184
185 httpLn, err := net.Listen("tcp", "127.0.0.1:0")
186 if err != nil {
187 t.Fatalf("HTTP listen: %v", err)
188 }
189 httpsLn, err := net.Listen("tcp", "127.0.0.1:0")
190 if err != nil {
191 t.Fatalf("HTTPS listen: %v", err)
192 }
193
194 var httpHandler http.Handler = handler
195 const fallbackDelay = 50 * time.Millisecond
196 clock := tstest.NewClock(tstest.ClockOpts{Step: 2 * fallbackDelay})
197 // Advance once to init the clock.
198 clock.Now()
199 if param.makeHTTPHangAfterUpgrade {
200 httpHandler = brokenMITMHandler(clock)
201 }
202 httpServer := &http.Server{Handler: httpHandler}
203 go httpServer.Serve(httpLn)
204 defer httpServer.Close()
205

Callers 1

TestControlHTTPFunction · 0.85

Calls 15

NowMethod · 0.95
ServeMethod · 0.95
CloseMethod · 0.95
SetBusMethod · 0.95
dialMethod · 0.95
NewMachineFunction · 0.92
NewBusFunction · 0.92
AcceptHTTPFunction · 0.92
NewClockFunction · 0.92
NewStaticFunction · 0.92
NewDialerFunction · 0.92
NewTrackerFunction · 0.92

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…