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.
 
 
 

79 lines
2.5 KiB

  1. // Copyright 2017 Google LLC
  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. {CredentialsJSON: []byte("json")},
  33. {HTTPClient: &http.Client{}},
  34. {GRPCConn: &grpc.ClientConn{}},
  35. // Although NoAuth and Scopes are technically incompatible, too many
  36. // cloud clients add WithScopes to user-provided options to make
  37. // the check feasible.
  38. {NoAuth: true, Scopes: []string{"s"}},
  39. } {
  40. err := ds.Validate()
  41. if err != nil {
  42. t.Errorf("%+v: got %v, want nil", ds, err)
  43. }
  44. }
  45. // Invalid.
  46. for _, ds := range []DialSettings{
  47. {NoAuth: true, APIKey: "x"},
  48. {NoAuth: true, CredentialsFile: "f"},
  49. {NoAuth: true, TokenSource: dummyTS{}},
  50. {NoAuth: true, Credentials: &google.DefaultCredentials{}},
  51. {Credentials: &google.DefaultCredentials{}, CredentialsFile: "f"},
  52. {Credentials: &google.DefaultCredentials{}, TokenSource: dummyTS{}},
  53. {Credentials: &google.DefaultCredentials{}, CredentialsJSON: []byte("json")},
  54. {CredentialsFile: "f", CredentialsJSON: []byte("json")},
  55. {CredentialsJSON: []byte("json"), TokenSource: dummyTS{}},
  56. {HTTPClient: &http.Client{}, GRPCConn: &grpc.ClientConn{}},
  57. {HTTPClient: &http.Client{}, GRPCDialOpts: []grpc.DialOption{grpc.WithInsecure()}},
  58. {Audiences: []string{"foo"}, Scopes: []string{"foo"}},
  59. {HTTPClient: &http.Client{}, QuotaProject: "foo"},
  60. {HTTPClient: &http.Client{}, RequestReason: "foo"},
  61. } {
  62. err := ds.Validate()
  63. if err == nil {
  64. t.Errorf("%+v: got nil, want error", ds)
  65. }
  66. }
  67. }
  68. type dummyTS struct{}
  69. func (dummyTS) Token() (*oauth2.Token, error) { return nil, nil }