Tests the scenario where grpc.Dial is performed using a proxy with the default resolver in the target URI. The test verifies that the connection is established to the proxy server, sends the unresolved target URI in the HTTP CONNECT request and is successfully connected to the backend server.
(t *testing.T)
| 89 | // established to the proxy server, sends the unresolved target URI in the HTTP |
| 90 | // CONNECT request and is successfully connected to the backend server. |
| 91 | func (s) TestGRPCDialWithProxy(t *testing.T) { |
| 92 | backend := startBackendServer(t) |
| 93 | unresolvedTargetURI := fmt.Sprintf("localhost:%d", testutils.ParsePort(t, backend.Address)) |
| 94 | proxyCalled := false |
| 95 | reqCheck := func(req *http.Request) { |
| 96 | proxyCalled = true |
| 97 | host, _, err := net.SplitHostPort(req.URL.Host) |
| 98 | if err != nil { |
| 99 | t.Error(err) |
| 100 | } |
| 101 | if got, want := host, "localhost"; got != want { |
| 102 | t.Errorf(" Unexpected request host: %s, want = %s ", got, want) |
| 103 | } |
| 104 | } |
| 105 | pServer := proxyserver.New(t, reqCheck, false) |
| 106 | // Use "localhost:<port>" to verify the proxy address is handled |
| 107 | // correctly by the delegating resolver and connects to the proxy server |
| 108 | // correctly even when unresolved. |
| 109 | pAddr := fmt.Sprintf("localhost:%d", testutils.ParsePort(t, pServer.Addr)) |
| 110 | |
| 111 | overrideTestHTTPSProxy(t, pAddr) |
| 112 | |
| 113 | ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) |
| 114 | defer cancel() |
| 115 | conn, err := grpc.Dial(unresolvedTargetURI, grpc.WithTransportCredentials(insecure.NewCredentials())) |
| 116 | if err != nil { |
| 117 | t.Fatalf("grpc.Dial(%s) failed: %v", unresolvedTargetURI, err) |
| 118 | } |
| 119 | defer conn.Close() |
| 120 | |
| 121 | // Send an empty RPC to the backend through the proxy. |
| 122 | client := testgrpc.NewTestServiceClient(conn) |
| 123 | if _, err := client.EmptyCall(ctx, &testpb.Empty{}); err != nil { |
| 124 | t.Fatalf("EmptyCall failed: %v", err) |
| 125 | } |
| 126 | |
| 127 | if !proxyCalled { |
| 128 | t.Fatalf("Proxy not connected") |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // Tests the scenario where `grpc.Dial` is performed with a proxy and the "dns" |
| 133 | // scheme for the target. The test verifies that the proxy URI is correctly |
nothing calls this directly
no test coverage detected