3 * Copyright 2014 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
28 func TestTimeoutEncode(t *testing.T) {
29 for _, test := range []struct {
33 {"12345678ns", "12345678n"},
34 {"123456789ns", "123457u"},
35 {"12345678us", "12345678u"},
36 {"123456789us", "123457m"},
37 {"12345678ms", "12345678m"},
38 {"123456789ms", "123457S"},
39 {"12345678s", "12345678S"},
40 {"123456789s", "2057614M"},
41 {"12345678m", "12345678M"},
42 {"123456789m", "2057614H"},
44 d, err := time.ParseDuration(test.in)
46 t.Fatalf("failed to parse duration string %s: %v", test.in, err)
48 out := encodeTimeout(d)
50 t.Fatalf("timeoutEncode(%s) = %s, want %s", test.in, out, test.out)
55 func TestTimeoutDecode(t *testing.T) {
56 for _, test := range []struct {
63 {"1234S", time.Second * 1234, nil},
64 {"1234x", 0, fmt.Errorf("transport: timeout unit is not recognized: %q", "1234x")},
65 {"1", 0, fmt.Errorf("transport: timeout string is too short: %q", "1")},
66 {"", 0, fmt.Errorf("transport: timeout string is too short: %q", "")},
68 d, err := decodeTimeout(test.s)
69 if d != test.d || fmt.Sprint(err) != fmt.Sprint(test.err) {
70 t.Fatalf("timeoutDecode(%q) = %d, %v, want %d, %v", test.s, int64(d), err, int64(test.d), test.err)
75 func TestValidContentType(t *testing.T) {
80 {"application/grpc", true},
81 {"application/grpc+", true},
82 {"application/grpc+blah", true},
83 {"application/grpc;", true},
84 {"application/grpc;blah", true},
85 {"application/grpcd", false},
86 {"application/grpd", false},
87 {"application/grp", false},
89 for _, tt := range tests {
90 got := validContentType(tt.h)
92 t.Errorf("validContentType(%q) = %v; want %v", tt.h, got, tt.want)
97 func TestEncodeGrpcMessage(t *testing.T) {
98 for _, tt := range []struct {
104 {"my favorite character is \u0000", "my favorite character is %00"},
105 {"my favorite character is %", "my favorite character is %25"},
107 actual := encodeGrpcMessage(tt.input)
108 if tt.expected != actual {
109 t.Errorf("encodeGrpcMessage(%v) = %v, want %v", tt.input, actual, tt.expected)
114 func TestDecodeGrpcMessage(t *testing.T) {
115 for _, tt := range []struct {
124 {"%E7%B3%BB%E7%BB%9F", "系统"},
126 actual := decodeGrpcMessage(tt.input)
127 if tt.expected != actual {
128 t.Errorf("dncodeGrpcMessage(%v) = %v, want %v", tt.input, actual, tt.expected)
133 const binaryValue = string(128)
135 func TestEncodeMetadataHeader(t *testing.T) {
136 for _, test := range []struct {
143 {"key", "abc", "abc"},
144 {"KEY", "abc", "abc"},
145 {"key-bin", "abc", "YWJj"},
146 {"key-bin", binaryValue, "woA"},
148 v := encodeMetadataHeader(test.kin, test.vin)
149 if !reflect.DeepEqual(v, test.vout) {
150 t.Fatalf("encodeMetadataHeader(%q, %q) = %q, want %q", test.kin, test.vin, v, test.vout)
155 func TestDecodeMetadataHeader(t *testing.T) {
156 for _, test := range []struct {
164 {"a", "abc", "abc", nil},
165 {"key-bin", "Zm9vAGJhcg==", "foo\x00bar", nil},
166 {"key-bin", "Zm9vAGJhcg", "foo\x00bar", nil},
167 {"key-bin", "woA=", binaryValue, nil},
168 {"a", "abc,efg", "abc,efg", nil},
170 v, err := decodeMetadataHeader(test.kin, test.vin)
171 if !reflect.DeepEqual(v, test.vout) || !reflect.DeepEqual(err, test.err) {
172 t.Fatalf("decodeMetadataHeader(%q, %q) = %q, %v, want %q, %v", test.kin, test.vin, v, err, test.vout, test.err)