MCPcopy Create free account
hub / github.com/Invisv-Privacy/masque / TestCreateTCPStream

Function TestCreateTCPStream

http2/client_test.go:97–194  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

95}
96
97func TestCreateTCPStream(t *testing.T) {
98 level := slog.LevelDebug
99 logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
100 Level: level,
101 }))
102 slog.SetDefault(logger)
103
104 // Start target HTTP/S server
105 expectedResponse := "test http response data"
106 ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
107 assert.Equal(t, "", r.Header.Get("Proxy-Authorization"), "Should not pass the auth token to the end target server")
108 assert.Equal(t, http.MethodGet, r.Method, "Request to the end target server should be a GET")
109 _, err := fmt.Fprintf(w, expectedResponse)
110 require.NoError(t, err, "fmt.Fprintf")
111 }))
112 ts.EnableHTTP2 = true
113 // We want to listen on 0.0.0.0 because the proxy container will be on a different non-localhost network.
114 // In order to do that we have this kind of awkward hack borrowed from:
115 // https://stackoverflow.com/a/42218765/1787596
116 l, err := net.Listen("tcp", "0.0.0.0:0")
117 if err != nil {
118 log.Fatal(err)
119 }
120
121 // Swap out the default test server listener with our custom one listening on 0.0.0.0
122 require.NoError(t, ts.Listener.Close(), "ts.Listener.Close()")
123 ts.Listener = l
124 ts.StartTLS()
125 defer ts.Close()
126
127 log.Printf("Test server listening on: %v", ts.URL)
128
129 urlSplit := strings.Split(ts.URL, ":")
130 port := urlSplit[len(urlSplit)-1]
131
132 // Now configure and start the MASQUE client
133 certDataFile := fmt.Sprintf("%s/testdata/h2o/server.crt", testutils.RootDir())
134 certData, err := os.ReadFile(certDataFile)
135 require.NoError(t, err, "Reading certData")
136
137 config := ClientConfig{
138 ProxyAddr: "localhost:8444",
139 // The h2o server we're using doesn't require an actual token so this can be anything
140 AuthToken: "fake-token",
141 Logger: logger,
142 CertData: certData,
143 }
144
145 c := NewClient(config)
146
147 err = c.ConnectToProxy()
148 require.NoError(t, err, "ConnectToProxy")
149
150 dockerHostURL := fmt.Sprintf("%v:%v", containerGateway, port)
151 conn, err := c.CreateTCPStream(dockerHostURL)
152 require.NoError(t, err, "CreateTCPStream")
153 defer func() {
154 require.NoError(t, conn.Close(), "conn.Close()")

Callers

nothing calls this directly

Calls 5

ConnectToProxyMethod · 0.95
CreateTCPStreamMethod · 0.95
RootDirFunction · 0.92
NewClientFunction · 0.70
CloseMethod · 0.45

Tested by

no test coverage detected