You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

72 line
2.1 KiB

  1. // Copyright 2017 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Package internal supports the options and transport packages.
  15. package internal
  16. import (
  17. "net/http"
  18. "testing"
  19. "google.golang.org/grpc"
  20. "golang.org/x/oauth2"
  21. "golang.org/x/oauth2/google"
  22. )
  23. func TestSettingsValidate(t *testing.T) {
  24. // Valid.
  25. for _, ds := range []DialSettings{
  26. {},
  27. {APIKey: "x"},
  28. {Scopes: []string{"s"}},
  29. {CredentialsFile: "f"},
  30. {TokenSource: dummyTS{}},
  31. {CredentialsFile: "f", TokenSource: dummyTS{}}, // keep for backwards compatibility
  32. {HTTPClient: &http.Client{}},
  33. {GRPCConn: &grpc.ClientConn{}},
  34. // Although NoAuth and Scopes are technically incompatible, too many
  35. // cloud clients add WithScopes to user-provided options to make
  36. // the check feasible.
  37. {NoAuth: true, Scopes: []string{"s"}},
  38. } {
  39. err := ds.Validate()
  40. if err != nil {
  41. t.Errorf("%+v: got %v, want nil", ds, err)
  42. }
  43. }
  44. // Invalid.
  45. for _, ds := range []DialSettings{
  46. {NoAuth: true, APIKey: "x"},
  47. {NoAuth: true, CredentialsFile: "f"},
  48. {NoAuth: true, TokenSource: dummyTS{}},
  49. {NoAuth: true, Credentials: &google.DefaultCredentials{}},
  50. {Credentials: &google.DefaultCredentials{}, CredentialsFile: "f"},
  51. {Credentials: &google.DefaultCredentials{}, TokenSource: dummyTS{}},
  52. {HTTPClient: &http.Client{}, GRPCConn: &grpc.ClientConn{}},
  53. {HTTPClient: &http.Client{}, GRPCDialOpts: []grpc.DialOption{grpc.WithInsecure()}},
  54. } {
  55. err := ds.Validate()
  56. if err == nil {
  57. t.Errorf("%+v: got nil, want error", ds)
  58. }
  59. }
  60. }
  61. type dummyTS struct{}
  62. func (dummyTS) Token() (*oauth2.Token, error) { return nil, nil }