MCPcopy Index your code
hub / github.com/coder/coder / RoundTrip

Method RoundTrip

testutil/websocket.go:36–63  ·  view source on GitHub ↗
(request *http.Request)

Source from the content-addressed store, hash-verified

34}
35
36func (i InMemWebsocketRoundTripper) RoundTrip(request *http.Request) (*http.Response, error) {
37 i.Logger.Debug(context.Background(), "round trip start")
38 defer i.Logger.Debug(context.Background(), "round trip end")
39 newCtx := i.CtxMutator(request.Context())
40 request = request.WithContext(newCtx)
41 serverP, clientP := net.Pipe()
42 var _ io.ReadWriteCloser = clientP // compile time check that response body is OK for websocket
43 response := &http.Response{
44 Header: make(http.Header),
45 Body: clientP,
46 }
47 rw := newInMemWebsocketResponseWriter(response, serverP)
48 go func() {
49 i.Handler.ServeHTTP(rw, request)
50 if !rw.hijacked {
51 i.Logger.Debug(context.Background(), "closing connection after handler did not hijack")
52 // If the handler didn't hijack the connection, we should close it when the handler finishes.
53 // This prevents a 3s delay in websocket.Dial() reading the non-upgraded response.
54 _ = serverP.Close()
55 }
56 }()
57 select {
58 case <-newCtx.Done():
59 return nil, newCtx.Err()
60 case <-rw.gotHeaders:
61 return response, nil
62 }
63}
64
65func newInMemWebsocketResponseWriter(resp *http.Response, conn net.Conn) *inMemWebsocketResponseWriter {
66 r := bufio.NewReader(conn)

Callers

nothing calls this directly

Calls 7

WithContextMethod · 0.80
ErrMethod · 0.80
ContextMethod · 0.65
CloseMethod · 0.65
ServeHTTPMethod · 0.45
DoneMethod · 0.45

Tested by

no test coverage detected