MCPcopy
hub / github.com/grpc/grpc-go / TestBasicAuthInNewClientWithProxy

Method TestBasicAuthInNewClientWithProxy

internal/transport/proxy_ext_test.go:387–441  ·  view source on GitHub ↗

Tests the scenario where grpc.NewClient is used with the default DNS resolver for targetURI and a proxy. The test verifies that the client connects to the proxy server, sends the unresolved target URI in the HTTP CONNECT request, and successfully connects to the backend. Additionally, it checks that

(t *testing.T)

Source from the content-addressed store, hash-verified

385// the CONNECT request. The test also ensures that target resolution does not
386// happen on the client.
387func (s) TestBasicAuthInNewClientWithProxy(t *testing.T) {
388 unresolvedTargetURI := "example.test"
389 const (
390 user = "notAUser"
391 password = "notAPassword"
392 )
393 proxyCalled := false
394 reqCheck := func(req *http.Request) {
395 proxyCalled = true
396 if got, want := req.URL.Host, "example.test:443"; got != want {
397 t.Errorf(" Unexpected request host: %s, want = %s ", got, want)
398 }
399 wantProxyAuthStr := "Basic " + base64.StdEncoding.EncodeToString([]byte(user+":"+password))
400 if got := req.Header.Get("Proxy-Authorization"); got != wantProxyAuthStr {
401 gotDecoded, err := base64.StdEncoding.DecodeString(got)
402 if err != nil {
403 t.Errorf("failed to decode Proxy-Authorization header: %v", err)
404 }
405 wantDecoded, _ := base64.StdEncoding.DecodeString(wantProxyAuthStr)
406 t.Errorf("unexpected auth %q (%q), want %q (%q)", got, gotDecoded, wantProxyAuthStr, wantDecoded)
407 }
408 }
409 pServer := proxyserver.New(t, reqCheck, false)
410
411 t.Setenv("HTTPS_PROXY", user+":"+password+"@"+pServer.Addr)
412
413 // Use the httpproxy package functions instead of `http.ProxyFromEnvironment`
414 // because the latter reads proxy-related environment variables only once at
415 // initialization. This behavior causes issues when running test multiple
416 // times, as changes to environment variables during tests would be ignored.
417 // By using `httpproxy.FromEnvironment()`, we ensure proxy settings are read dynamically.
418 origHTTPSProxyFromEnvironment := delegatingresolver.HTTPSProxyFromEnvironment
419 delegatingresolver.HTTPSProxyFromEnvironment = func(req *http.Request) (*url.URL, error) {
420 return httpproxy.FromEnvironment().ProxyFunc()(req.URL)
421 }
422 defer func() {
423 delegatingresolver.HTTPSProxyFromEnvironment = origHTTPSProxyFromEnvironment
424 }()
425
426 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
427 defer cancel()
428 conn, err := grpc.NewClient(unresolvedTargetURI, grpc.WithTransportCredentials(insecure.NewCredentials()))
429 if err != nil {
430 t.Fatalf("grpc.NewClient(%s) failed: %v", unresolvedTargetURI, err)
431 }
432 defer conn.Close()
433
434 // Send an empty RPC to the backend through the proxy.
435 client := testgrpc.NewTestServiceClient(conn)
436 client.EmptyCall(ctx, &testpb.Empty{})
437
438 if !proxyCalled {
439 t.Fatalf("Proxy not connected")
440 }
441}

Callers

nothing calls this directly

Calls 9

EmptyCallMethod · 0.95
NewFunction · 0.92
NewClientFunction · 0.92
WithTransportCredentialsFunction · 0.92
NewCredentialsFunction · 0.92
ErrorfMethod · 0.65
GetMethod · 0.65
FatalfMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected