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.4 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 option
  15. import (
  16. "testing"
  17. "github.com/google/go-cmp/cmp"
  18. "github.com/google/go-cmp/cmp/cmpopts"
  19. "golang.org/x/oauth2/google"
  20. "google.golang.org/api/internal"
  21. "google.golang.org/grpc"
  22. )
  23. // Check that the slice passed into WithScopes is copied.
  24. func TestCopyScopes(t *testing.T) {
  25. o := &internal.DialSettings{}
  26. scopes := []string{"a", "b"}
  27. WithScopes(scopes...).Apply(o)
  28. // Modify after using.
  29. scopes[1] = "c"
  30. if o.Scopes[0] != "a" || o.Scopes[1] != "b" {
  31. t.Errorf("want ['a', 'b'], got %+v", o.Scopes)
  32. }
  33. }
  34. func TestApply(t *testing.T) {
  35. conn := &grpc.ClientConn{}
  36. opts := []ClientOption{
  37. WithEndpoint("https://example.com:443"),
  38. WithScopes("a"), // the next WithScopes should overwrite this one
  39. WithScopes("https://example.com/auth/helloworld", "https://example.com/auth/otherthing"),
  40. WithGRPCConn(conn),
  41. WithUserAgent("ua"),
  42. WithCredentialsFile("service-account.json"),
  43. WithCredentialsJSON([]byte(`{some: "json"}`)),
  44. WithCredentials(&google.DefaultCredentials{ProjectID: "p"}),
  45. WithAPIKey("api-key"),
  46. WithAudiences("https://example.com/"),
  47. WithQuotaProject("user-project"),
  48. WithRequestReason("Request Reason"),
  49. }
  50. var got internal.DialSettings
  51. for _, opt := range opts {
  52. opt.Apply(&got)
  53. }
  54. want := internal.DialSettings{
  55. Scopes: []string{"https://example.com/auth/helloworld", "https://example.com/auth/otherthing"},
  56. UserAgent: "ua",
  57. Endpoint: "https://example.com:443",
  58. GRPCConn: conn,
  59. Credentials: &google.DefaultCredentials{ProjectID: "p"},
  60. CredentialsFile: "service-account.json",
  61. CredentialsJSON: []byte(`{some: "json"}`),
  62. APIKey: "api-key",
  63. Audiences: []string{"https://example.com/"},
  64. QuotaProject: "user-project",
  65. RequestReason: "Request Reason",
  66. }
  67. if !cmp.Equal(got, want, cmpopts.IgnoreUnexported(grpc.ClientConn{})) {
  68. t.Errorf("\ngot %#v\nwant %#v", got, want)
  69. }
  70. }