oauth_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. *
  3. * Copyright 2021 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package oauth
  19. import (
  20. "strings"
  21. "testing"
  22. )
  23. func checkErrorMsg(err error, msg string) bool {
  24. if err == nil && msg == "" {
  25. return true
  26. } else if err != nil {
  27. return strings.Contains(err.Error(), msg)
  28. }
  29. return false
  30. }
  31. func TestRemoveServiceNameFromJWTURI(t *testing.T) {
  32. tests := []struct {
  33. name string
  34. uri string
  35. wantedURI string
  36. wantedErrMsg string
  37. }{
  38. {
  39. name: "invalid URI",
  40. uri: "ht tp://foo.com",
  41. wantedErrMsg: "first path segment in URL cannot contain colon",
  42. },
  43. {
  44. name: "valid URI",
  45. uri: "https://foo.com/go/",
  46. wantedURI: "https://foo.com/",
  47. },
  48. }
  49. for _, tt := range tests {
  50. t.Run(tt.name, func(t *testing.T) {
  51. if got, err := removeServiceNameFromJWTURI(tt.uri); got != tt.wantedURI || !checkErrorMsg(err, tt.wantedErrMsg) {
  52. t.Errorf("RemoveServiceNameFromJWTURI() = %s, %v, want %s, %v", got, err, tt.wantedURI, tt.wantedErrMsg)
  53. }
  54. })
  55. }
  56. }