Repository: grpc/grpc-go
File: default_dial_option_server_option_test.go (Go)

Code before:

	// Ensure the NewClient passes with the extra dial options
	if cc, err := NewClient("fake"); err != nil {
		t.Fatalf("NewClient with insecure credential failed: %v", err)
	} else {
		cc.Close()
	}

	internal.ClearGlobalDialOptions()
	if len(globalDialOptions) != 0 {
		t.Fatalf("Unexpected len of globalDialOptions: %d != 0", len(globalDialOptions))
	}
}

// TestDisableGlobalOptions tests dialing with the disableGlobalDialOptions dial
// option. Dialing with this set should not pick up global options.
func (s) TestDisableGlobalOptions(t *testing.T) {
	// Set transport credentials as a global option.
	internal.AddGlobalDialOptions.(func(opt ...DialOption))(WithTransportCredentials(insecure.NewCredentials()))
	defer internal.ClearGlobalDialOptions()
	// Dial with the disable global options dial option. This dial should fail
	// due to the global dial options with credentials not being picked up due
	// to global options being disabled.
	noTSecStr := "no transport security set"
	if _, err := NewClient("fake", internal.DisableGlobalDialOptions.(func() DialOption)()); !strings.Contains(fmt.Sprint(err), noTSecStr) {
		t.Fatalf("NewClient received unexpected error: %q, want error containing %q", err, noTSecStr)
	}
}

type testPerTargetDialOption struct{}

func (do *testPerTargetDialOption) DialOptionForTarget(parsedTarget url.URL) DialOption {
	if parsedTarget.Scheme == "passthrough" {
		return WithTransportCredentials(insecure.NewCredentials()) // credentials provided, should pass NewClient.
	}
	return EmptyDialOption{} // no credentials, should fail NewClient
}

// TestGlobalPerTargetDialOption configures a global per target dial option that
// produces transport credentials for channels using "passthrough" scheme.
// Channels that use the passthrough scheme should be successfully created due
// to picking up transport credentials, whereas other channels should fail at
// creation due to not having transport credentials.
func (s) TestGlobalPerTargetDialOption(t *testing.T) {
	internal.AddGlobalPerTargetDialOptions.(func(opt any))(&testPerTargetDialOption{})
	defer internal.ClearGlobalPerTargetDialOptions()
	noTSecStr := "no transport security set"
	if _, err := NewClient("dns:///fake"); !strings.Contains(fmt.Sprint(err), noTSecStr) {
		t.Fatalf("NewClient received unexpected error: %q, want error containing %q", err, noTSecStr)
	}
	cc, err := NewClient("passthrough:///nice")

Review comment (suggestion):
Please format errors with `%v`, `%q` should be used for strings.

Code after:

	// Ensure the NewClient passes with the extra dial options
	if cc, err := NewClient("fake"); err != nil {
		t.Fatalf("NewClient with insecure credential failed: %v", err)
	} else {
		cc.Close()
	}

	internal.ClearGlobalDialOptions()
	if len(globalDialOptions) != 0 {
		t.Fatalf("Unexpected len of globalDialOptions: %d != 0", len(globalDialOptions))
	}
}

// TestDisableGlobalOptions tests dialing with the disableGlobalDialOptions dial
// option. Dialing with this set should not pick up global options.
func (s) TestDisableGlobalOptions(t *testing.T) {
	// Set transport credentials as a global option.
	internal.AddGlobalDialOptions.(func(opt ...DialOption))(WithTransportCredentials(insecure.NewCredentials()))
	defer internal.ClearGlobalDialOptions()
	// Dial with the disable global options dial option. This dial should fail
	// due to the global dial options with credentials not being picked up due
	// to global options being disabled.
	noTSecStr := "no transport security set"
	if _, err := NewClient("fake", internal.DisableGlobalDialOptions.(func() DialOption)()); !strings.Contains(fmt.Sprint(err), noTSecStr) {
		t.Fatalf("NewClient received unexpected error: %v, want error containing %q", err, noTSecStr)
	}
}

type testPerTargetDialOption struct{}

func (do *testPerTargetDialOption) DialOptionForTarget(parsedTarget url.URL) DialOption {
	if parsedTarget.Scheme == "passthrough" {
		return WithTransportCredentials(insecure.NewCredentials()) // credentials provided, should pass NewClient.
	}
	return EmptyDialOption{} // no credentials, should fail NewClient
}

// TestGlobalPerTargetDialOption configures a global per target dial option that
// produces transport credentials for channels using "passthrough" scheme.
// Channels that use the passthrough scheme should be successfully created due
// to picking up transport credentials, whereas other channels should fail at
// creation due to not having transport credentials.
func (s) TestGlobalPerTargetDialOption(t *testing.T) {
	internal.AddGlobalPerTargetDialOptions.(func(opt any))(&testPerTargetDialOption{})
	defer internal.ClearGlobalPerTargetDialOptions()
	noTSecStr := "no transport security set"
	if _, err := NewClient("dns:///fake"); !strings.Contains(fmt.Sprint(err), noTSecStr) {
		t.Fatalf("NewClient received unexpected error: %v, want error containing %q", err, noTSecStr)
	}
	cc, err := NewClient("passthrough:///nice")