OSDN Git Service

new repo
[bytom/vapor.git] / vendor / google.golang.org / grpc / transport / http_util_test.go
1 /*
2  *
3  * Copyright 2014 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
19 package transport
20
21 import (
22         "fmt"
23         "reflect"
24         "testing"
25         "time"
26 )
27
28 func TestTimeoutEncode(t *testing.T) {
29         for _, test := range []struct {
30                 in  string
31                 out string
32         }{
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"},
43         } {
44                 d, err := time.ParseDuration(test.in)
45                 if err != nil {
46                         t.Fatalf("failed to parse duration string %s: %v", test.in, err)
47                 }
48                 out := encodeTimeout(d)
49                 if out != test.out {
50                         t.Fatalf("timeoutEncode(%s) = %s, want %s", test.in, out, test.out)
51                 }
52         }
53 }
54
55 func TestTimeoutDecode(t *testing.T) {
56         for _, test := range []struct {
57                 // input
58                 s string
59                 // output
60                 d   time.Duration
61                 err error
62         }{
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", "")},
67         } {
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)
71                 }
72         }
73 }
74
75 func TestValidContentType(t *testing.T) {
76         tests := []struct {
77                 h    string
78                 want bool
79         }{
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},
88         }
89         for _, tt := range tests {
90                 got := validContentType(tt.h)
91                 if got != tt.want {
92                         t.Errorf("validContentType(%q) = %v; want %v", tt.h, got, tt.want)
93                 }
94         }
95 }
96
97 func TestEncodeGrpcMessage(t *testing.T) {
98         for _, tt := range []struct {
99                 input    string
100                 expected string
101         }{
102                 {"", ""},
103                 {"Hello", "Hello"},
104                 {"my favorite character is \u0000", "my favorite character is %00"},
105                 {"my favorite character is %", "my favorite character is %25"},
106         } {
107                 actual := encodeGrpcMessage(tt.input)
108                 if tt.expected != actual {
109                         t.Errorf("encodeGrpcMessage(%v) = %v, want %v", tt.input, actual, tt.expected)
110                 }
111         }
112 }
113
114 func TestDecodeGrpcMessage(t *testing.T) {
115         for _, tt := range []struct {
116                 input    string
117                 expected string
118         }{
119                 {"", ""},
120                 {"Hello", "Hello"},
121                 {"H%61o", "Hao"},
122                 {"H%6", "H%6"},
123                 {"%G0", "%G0"},
124                 {"%E7%B3%BB%E7%BB%9F", "系统"},
125         } {
126                 actual := decodeGrpcMessage(tt.input)
127                 if tt.expected != actual {
128                         t.Errorf("dncodeGrpcMessage(%v) = %v, want %v", tt.input, actual, tt.expected)
129                 }
130         }
131 }
132
133 const binaryValue = string(128)
134
135 func TestEncodeMetadataHeader(t *testing.T) {
136         for _, test := range []struct {
137                 // input
138                 kin string
139                 vin string
140                 // output
141                 vout string
142         }{
143                 {"key", "abc", "abc"},
144                 {"KEY", "abc", "abc"},
145                 {"key-bin", "abc", "YWJj"},
146                 {"key-bin", binaryValue, "woA"},
147         } {
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)
151                 }
152         }
153 }
154
155 func TestDecodeMetadataHeader(t *testing.T) {
156         for _, test := range []struct {
157                 // input
158                 kin string
159                 vin string
160                 // output
161                 vout string
162                 err  error
163         }{
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},
169         } {
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)
173                 }
174         }
175 }