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