method_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. *
  3. * Copyright 2018 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 grpcutil
  19. import (
  20. "testing"
  21. )
  22. func TestParseMethod(t *testing.T) {
  23. testCases := []struct {
  24. methodName string
  25. wantService string
  26. wantMethod string
  27. wantError bool
  28. }{
  29. {methodName: "/s/m", wantService: "s", wantMethod: "m", wantError: false},
  30. {methodName: "/p.s/m", wantService: "p.s", wantMethod: "m", wantError: false},
  31. {methodName: "/p/s/m", wantService: "p/s", wantMethod: "m", wantError: false},
  32. {methodName: "/", wantError: true},
  33. {methodName: "/sm", wantError: true},
  34. {methodName: "", wantError: true},
  35. {methodName: "sm", wantError: true},
  36. }
  37. for _, tc := range testCases {
  38. s, m, err := ParseMethod(tc.methodName)
  39. if (err != nil) != tc.wantError || s != tc.wantService || m != tc.wantMethod {
  40. t.Errorf("ParseMethod(%s) = (%s, %s, %v), want (%s, %s, %v)", tc.methodName, s, m, err, tc.wantService, tc.wantMethod, tc.wantError)
  41. }
  42. }
  43. }
  44. func TestContentSubtype(t *testing.T) {
  45. tests := []struct {
  46. contentType string
  47. want string
  48. wantValid bool
  49. }{
  50. {"application/grpc", "", true},
  51. {"application/grpc+", "", true},
  52. {"application/grpc+blah", "blah", true},
  53. {"application/grpc;", "", true},
  54. {"application/grpc;blah", "blah", true},
  55. {"application/grpcd", "", false},
  56. {"application/grpd", "", false},
  57. {"application/grp", "", false},
  58. }
  59. for _, tt := range tests {
  60. got, gotValid := ContentSubtype(tt.contentType)
  61. if got != tt.want || gotValid != tt.wantValid {
  62. t.Errorf("contentSubtype(%q) = (%v, %v); want (%v, %v)", tt.contentType, got, gotValid, tt.want, tt.wantValid)
  63. }
  64. }
  65. }