1 package main
2
3 import (
4 "errors"
5 "flag"
6 "reflect"
7 "testing"
8 )
9
10 func TestParseReqParam(t *testing.T) {
11
12 testcases := []struct {
13 name string
14 expected map[string]string
15 request string
16 expectedError error
17 allowDeleteBodyV bool
18 allowMergeV bool
19 allowRepeatedFieldsInBodyV bool
20 includePackageInTagsV bool
21 fileV string
22 importPathV string
23 mergeFileNameV string
24 useFQNForSwaggerNameV bool
25 }{
26 {
27
28
29
30 name: "Test 0",
31 expected: map[string]string{},
32 request: "",
33 allowDeleteBodyV: false, allowMergeV: false, allowRepeatedFieldsInBodyV: false, includePackageInTagsV: false,
34 fileV: "-", importPathV: "", mergeFileNameV: "apidocs",
35 },
36 {
37 name: "Test 1",
38 expected: map[string]string{"google/api/annotations.proto": "github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis/google/api"},
39 request: "allow_delete_body,allow_merge,allow_repeated_fields_in_body,include_package_in_tags,file=./foo.pb,import_prefix=/bar/baz,Mgoogle/api/annotations.proto=github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis/google/api",
40 allowDeleteBodyV: true, allowMergeV: true, allowRepeatedFieldsInBodyV: true, includePackageInTagsV: true,
41 fileV: "./foo.pb", importPathV: "/bar/baz", mergeFileNameV: "apidocs",
42 },
43 {
44 name: "Test 2",
45 expected: map[string]string{"google/api/annotations.proto": "github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis/google/api"},
46 request: "allow_delete_body=true,allow_merge=true,allow_repeated_fields_in_body=true,include_package_in_tags=true,merge_file_name=test_name,file=./foo.pb,import_prefix=/bar/baz,Mgoogle/api/annotations.proto=github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis/google/api",
47 allowDeleteBodyV: true, allowMergeV: true, allowRepeatedFieldsInBodyV: true, includePackageInTagsV: true,
48 fileV: "./foo.pb", importPathV: "/bar/baz", mergeFileNameV: "test_name",
49 },
50 {
51 name: "Test 3",
52 expected: map[string]string{"a/b/c.proto": "github.com/x/y/z", "f/g/h.proto": "github.com/1/2/3/"},
53 request: "allow_delete_body=false,allow_merge=false,Ma/b/c.proto=github.com/x/y/z,Mf/g/h.proto=github.com/1/2/3/",
54 allowDeleteBodyV: false, allowMergeV: false, allowRepeatedFieldsInBodyV: false, includePackageInTagsV: false,
55 fileV: "stdin", importPathV: "", mergeFileNameV: "apidocs",
56 },
57 {
58 name: "Test 4",
59 expected: map[string]string{},
60 request: "",
61 allowDeleteBodyV: false, allowMergeV: false, allowRepeatedFieldsInBodyV: false, includePackageInTagsV: false,
62 fileV: "stdin", importPathV: "", mergeFileNameV: "apidocs",
63 },
64 {
65 name: "Test 5",
66 expected: map[string]string{},
67 request: "unknown_param=17",
68 expectedError: errors.New("Cannot set flag unknown_param=17: no such flag -unknown_param"),
69 allowDeleteBodyV: false, allowMergeV: false, allowRepeatedFieldsInBodyV: false, includePackageInTagsV: false,
70 fileV: "stdin", importPathV: "", mergeFileNameV: "apidocs",
71 },
72 {
73 name: "Test 6",
74 expected: map[string]string{},
75 request: "Mfoo",
76 expectedError: errors.New("Cannot set flag Mfoo: no such flag -Mfoo"),
77 allowDeleteBodyV: false, allowMergeV: false, allowRepeatedFieldsInBodyV: false, includePackageInTagsV: false,
78 fileV: "stdin", importPathV: "", mergeFileNameV: "apidocs",
79 },
80 {
81 name: "Test 7",
82 expected: map[string]string{},
83 request: "allow_delete_body,file,import_prefix,allow_merge,allow_repeated_fields_in_body,include_package_in_tags,merge_file_name",
84 allowDeleteBodyV: true, allowMergeV: true, allowRepeatedFieldsInBodyV: true, includePackageInTagsV: true,
85 fileV: "", importPathV: "", mergeFileNameV: "",
86 },
87 {
88 name: "Test 8",
89 expected: map[string]string{},
90 request: "allow_delete_body,file,import_prefix,allow_merge,allow_repeated_fields_in_body=3,merge_file_name",
91 expectedError: errors.New(`Cannot set flag allow_repeated_fields_in_body=3: parse error`),
92 allowDeleteBodyV: true, allowMergeV: true, allowRepeatedFieldsInBodyV: false, includePackageInTagsV: false,
93 fileV: "", importPathV: "", mergeFileNameV: "apidocs",
94 },
95 {
96 name: "Test 9",
97 expected: map[string]string{},
98 request: "include_package_in_tags=3",
99 expectedError: errors.New(`Cannot set flag include_package_in_tags=3: parse error`),
100 allowDeleteBodyV: false, allowMergeV: false, allowRepeatedFieldsInBodyV: false, includePackageInTagsV: false,
101 fileV: "stdin", importPathV: "", mergeFileNameV: "apidocs",
102 },
103 {
104 name: "Test 10",
105 expected: map[string]string{},
106 request: "fqn_for_swagger_name=3",
107 expectedError: errors.New(`Cannot set flag fqn_for_swagger_name=3: parse error`),
108 allowDeleteBodyV: false, allowMergeV: false, allowRepeatedFieldsInBodyV: false, includePackageInTagsV: false, useFQNForSwaggerNameV: false,
109 fileV: "stdin", importPathV: "", mergeFileNameV: "apidocs",
110 },
111 {
112 name: "Test 11",
113 expected: map[string]string{},
114 request: "fqn_for_swagger_name=true",
115 allowDeleteBodyV: false, allowMergeV: false, allowRepeatedFieldsInBodyV: false, includePackageInTagsV: false, useFQNForSwaggerNameV: true,
116 fileV: "stdin", importPathV: "", mergeFileNameV: "apidocs",
117 },
118 }
119
120 for i, tc := range testcases {
121 t.Run(tc.name, func(tt *testing.T) {
122 f := flag.CommandLine
123 pkgMap := make(map[string]string)
124 err := parseReqParam(tc.request, f, pkgMap)
125 if tc.expectedError == nil {
126 if err != nil {
127 tt.Errorf("unexpected parse error '%v'", err)
128 }
129 if !reflect.DeepEqual(pkgMap, tc.expected) {
130 tt.Errorf("pkgMap parse error, expected '%v', got '%v'", tc.expected, pkgMap)
131 }
132 } else {
133 if err == nil {
134 tt.Error("expected parse error not returned")
135 }
136 if !reflect.DeepEqual(pkgMap, tc.expected) {
137 tt.Errorf("pkgMap parse error, expected '%v', got '%v'", tc.expected, pkgMap)
138 }
139 if err.Error() != tc.expectedError.Error() {
140 tt.Errorf("expected error malformed, expected %q, got %q", tc.expectedError.Error(), err.Error())
141 }
142 }
143 checkFlags(tc.allowDeleteBodyV, tc.allowMergeV, tc.allowRepeatedFieldsInBodyV, tc.includePackageInTagsV, tc.useFQNForSwaggerNameV, tc.fileV, tc.importPathV, tc.mergeFileNameV, tt, i)
144
145 clearFlags()
146 })
147 }
148
149 }
150
151 func checkFlags(allowDeleteV, allowMergeV, allowRepeatedFieldsInBodyV, includePackageInTagsV bool, useFQNForSwaggerNameV bool, fileV, importPathV, mergeFileNameV string, t *testing.T, tid int) {
152 if *importPrefix != importPathV {
153 t.Errorf("Test %v: import_prefix misparsed, expected '%v', got '%v'", tid, importPathV, *importPrefix)
154 }
155 if *file != fileV {
156 t.Errorf("Test %v: file misparsed, expected '%v', got '%v'", tid, fileV, *file)
157 }
158 if *allowDeleteBody != allowDeleteV {
159 t.Errorf("Test %v: allow_delete_body misparsed, expected '%v', got '%v'", tid, allowDeleteV, *allowDeleteBody)
160 }
161 if *allowMerge != allowMergeV {
162 t.Errorf("Test %v: allow_merge misparsed, expected '%v', got '%v'", tid, allowMergeV, *allowMerge)
163 }
164 if *mergeFileName != mergeFileNameV {
165 t.Errorf("Test %v: merge_file_name misparsed, expected '%v', got '%v'", tid, mergeFileNameV, *mergeFileName)
166 }
167 if *allowRepeatedFieldsInBody != allowRepeatedFieldsInBodyV {
168 t.Errorf("Test %v: allow_repeated_fields_in_body misparsed, expected '%v', got '%v'", tid, allowRepeatedFieldsInBodyV, *allowRepeatedFieldsInBody)
169 }
170 if *includePackageInTags != includePackageInTagsV {
171 t.Errorf("Test %v: include_package_in_tags misparsed, expected '%v', got '%v'", tid, includePackageInTagsV, *includePackageInTags)
172 }
173 if *useFQNForSwaggerName != useFQNForSwaggerNameV {
174 t.Errorf("Test %v: fqn_for_swagger_name misparsed, expected '%v', got '%v'", tid, useFQNForSwaggerNameV, *useFQNForSwaggerName)
175 }
176 }
177
178 func clearFlags() {
179 *importPrefix = ""
180 *file = "stdin"
181 *allowDeleteBody = false
182 *allowMerge = false
183 *allowRepeatedFieldsInBody = false
184 *includePackageInTags = false
185 *mergeFileName = "apidocs"
186 }
187
View as plain text