|
| 1 | +// Copyright 2025 CloudWeGo Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package thriftgo |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/cloudwego/kitex/internal/test" |
| 21 | + "github.com/cloudwego/thriftgo/parser" |
| 22 | +) |
| 23 | + |
| 24 | +func TestGenHTTPTags(t *testing.T) { |
| 25 | + emptyOpt := genHTTPTagOption{} |
| 26 | + // required, no annotation, generate form and query by default |
| 27 | + httpTag := genHTTPTags(&parser.Field{ |
| 28 | + Name: "f1", |
| 29 | + Requiredness: parser.FieldType_Required, |
| 30 | + }, emptyOpt) |
| 31 | + test.Assert(t, httpTag == ` form:"f1,required" json:"f1,required" query:"f1,required"`, httpTag) |
| 32 | + |
| 33 | + // default, no annotation |
| 34 | + httpTag = genHTTPTags(&parser.Field{ |
| 35 | + Name: "f11", |
| 36 | + Requiredness: parser.FieldType_Default, |
| 37 | + }, emptyOpt) |
| 38 | + test.Assert(t, httpTag == ` form:"f11" json:"f11" query:"f11"`, httpTag) |
| 39 | + |
| 40 | + // optional + go.tag (ignored by genHTTPTags) |
| 41 | + httpTag = genHTTPTags(&parser.Field{ |
| 42 | + Name: "f12", |
| 43 | + Requiredness: parser.FieldType_Optional, |
| 44 | + Annotations: []*parser.Annotation{ |
| 45 | + {Key: "go.tag", Values: []string{`some_tag:"some_tag_value"`}}, |
| 46 | + }, |
| 47 | + }, emptyOpt) |
| 48 | + test.Assert(t, httpTag == ` form:"f12" json:"f12,omitempty" query:"f12"`, httpTag) |
| 49 | + |
| 50 | + // required, api.query |
| 51 | + httpTag = genHTTPTags(&parser.Field{ |
| 52 | + Name: "f22", |
| 53 | + Requiredness: parser.FieldType_Required, |
| 54 | + Annotations: []*parser.Annotation{ |
| 55 | + {Key: "api.query", Values: []string{"abc"}}, |
| 56 | + }, |
| 57 | + }, emptyOpt) |
| 58 | + test.Assert(t, httpTag == ` json:"f22,required" query:"abc,required"`, httpTag) |
| 59 | + |
| 60 | + // required, api.form |
| 61 | + httpTag = genHTTPTags(&parser.Field{ |
| 62 | + Name: "f23", |
| 63 | + Requiredness: parser.FieldType_Required, |
| 64 | + Annotations: []*parser.Annotation{ |
| 65 | + {Key: "api.form", Values: []string{"abc"}}, |
| 66 | + }, |
| 67 | + }, emptyOpt) |
| 68 | + test.Assert(t, httpTag == ` form:"abc,required" json:"f23,required"`, httpTag) |
| 69 | + |
| 70 | + // required, api.path |
| 71 | + httpTag = genHTTPTags(&parser.Field{ |
| 72 | + Name: "f24", |
| 73 | + Requiredness: parser.FieldType_Required, |
| 74 | + Annotations: []*parser.Annotation{ |
| 75 | + {Key: "api.path", Values: []string{"abc"}}, |
| 76 | + }, |
| 77 | + }, emptyOpt) |
| 78 | + test.Assert(t, httpTag == ` json:"f24,required" path:"abc,required"`, httpTag) |
| 79 | + |
| 80 | + // required, api.header |
| 81 | + httpTag = genHTTPTags(&parser.Field{ |
| 82 | + Name: "f25", |
| 83 | + Requiredness: parser.FieldType_Required, |
| 84 | + Annotations: []*parser.Annotation{ |
| 85 | + {Key: "api.header", Values: []string{"abc"}}, |
| 86 | + }, |
| 87 | + }, emptyOpt) |
| 88 | + test.Assert(t, httpTag == ` header:"abc,required" json:"f25,required"`, httpTag) |
| 89 | + |
| 90 | + // required, api.cookie |
| 91 | + httpTag = genHTTPTags(&parser.Field{ |
| 92 | + Name: "f26", |
| 93 | + Requiredness: parser.FieldType_Required, |
| 94 | + Annotations: []*parser.Annotation{ |
| 95 | + {Key: "api.cookie", Values: []string{"abc"}}, |
| 96 | + }, |
| 97 | + }, emptyOpt) |
| 98 | + test.Assert(t, httpTag == ` cookie:"abc,required" json:"f26,required"`, httpTag) |
| 99 | + |
| 100 | + // required, api.raw_body |
| 101 | + httpTag = genHTTPTags(&parser.Field{ |
| 102 | + Name: "f27", |
| 103 | + Requiredness: parser.FieldType_Required, |
| 104 | + Annotations: []*parser.Annotation{ |
| 105 | + {Key: "api.raw_body", Values: []string{"abc"}}, |
| 106 | + }, |
| 107 | + }, emptyOpt) |
| 108 | + test.Assert(t, httpTag == ` json:"f27,required" raw_body:"abc,required"`, httpTag) |
| 109 | + |
| 110 | + // required, multiple annotations, generate all |
| 111 | + httpTag = genHTTPTags(&parser.Field{ |
| 112 | + Name: "f31", |
| 113 | + Requiredness: parser.FieldType_Required, |
| 114 | + Annotations: []*parser.Annotation{ |
| 115 | + {Key: "api.query", Values: []string{"abc"}}, |
| 116 | + {Key: "api.form", Values: []string{"def"}}, |
| 117 | + {Key: "api.header", Values: []string{"abc"}}, |
| 118 | + }, |
| 119 | + }, emptyOpt) |
| 120 | + // tags are arranged in alphabet order. |
| 121 | + test.Assert(t, httpTag == ` form:"def,required" header:"abc,required" json:"f31,required" query:"abc,required"`, httpTag) |
| 122 | + |
| 123 | + // required, duplicate api.query values, use the last one. |
| 124 | + httpTag = genHTTPTags(&parser.Field{ |
| 125 | + Name: "f32", |
| 126 | + Requiredness: parser.FieldType_Required, |
| 127 | + Annotations: []*parser.Annotation{ |
| 128 | + {Key: "api.query", Values: []string{"abc", "def"}}, |
| 129 | + }, |
| 130 | + }, emptyOpt) |
| 131 | + test.Assert(t, httpTag == ` json:"f32,required" query:"def,required"`, httpTag) |
| 132 | + |
| 133 | + // required, api.body, api body generate json and form |
| 134 | + httpTag = genHTTPTags(&parser.Field{ |
| 135 | + Name: "f41", |
| 136 | + Requiredness: parser.FieldType_Required, |
| 137 | + Annotations: []*parser.Annotation{ |
| 138 | + {Key: "api.body", Values: []string{"abc"}}, |
| 139 | + }, |
| 140 | + }, emptyOpt) |
| 141 | + test.Assert(t, httpTag == ` form:"abc,required" json:"abc,required"`, httpTag) |
| 142 | + |
| 143 | + // default, api.body |
| 144 | + httpTag = genHTTPTags(&parser.Field{ |
| 145 | + Name: "f42", |
| 146 | + Requiredness: parser.FieldType_Default, |
| 147 | + Annotations: []*parser.Annotation{ |
| 148 | + {Key: "api.body", Values: []string{"abc"}}, |
| 149 | + }, |
| 150 | + }, emptyOpt) |
| 151 | + test.Assert(t, httpTag == ` form:"abc" json:"abc"`, httpTag) |
| 152 | + |
| 153 | + // default, api.body |
| 154 | + httpTag = genHTTPTags(&parser.Field{ |
| 155 | + Name: "f43", |
| 156 | + Requiredness: parser.FieldType_Optional, |
| 157 | + Annotations: []*parser.Annotation{ |
| 158 | + {Key: "api.body", Values: []string{"abc"}}, |
| 159 | + }, |
| 160 | + }, emptyOpt) |
| 161 | + test.Assert(t, httpTag == ` form:"abc" json:"abc,omitempty"`, httpTag) |
| 162 | + |
| 163 | + // default, api.body, api.cookie |
| 164 | + httpTag = genHTTPTags(&parser.Field{ |
| 165 | + Name: "f43", |
| 166 | + Requiredness: parser.FieldType_Optional, |
| 167 | + Annotations: []*parser.Annotation{ |
| 168 | + {Key: "api.body", Values: []string{"abc"}}, |
| 169 | + {Key: "api.cookie", Values: []string{"efg"}}, |
| 170 | + }, |
| 171 | + }, emptyOpt) |
| 172 | + test.Assert(t, httpTag == ` cookie:"efg" form:"abc" json:"abc,omitempty"`, httpTag) |
| 173 | +} |
| 174 | + |
| 175 | +func TestGenHTTPTagsWithOption(t *testing.T) { |
| 176 | + // snake field name to camel, only for json |
| 177 | + httpTag := genHTTPTags(&parser.Field{ |
| 178 | + Name: "field_a", |
| 179 | + Requiredness: parser.FieldType_Required, |
| 180 | + }, genHTTPTagOption{lowerCamelCaseJSONTag: true}) |
| 181 | + test.Assert(t, httpTag == ` form:"field_a,required" json:"fieldA,required" query:"field_a,required"`, httpTag) |
| 182 | + |
| 183 | + // camel field name to snake, only for json |
| 184 | + httpTag = genHTTPTags(&parser.Field{ |
| 185 | + Name: "FieldB", |
| 186 | + Requiredness: parser.FieldType_Required, |
| 187 | + }, genHTTPTagOption{snakeTyleJSONTag: true}) |
| 188 | + test.Assert(t, httpTag == ` form:"FieldB,required" json:"field_b,required" query:"FieldB,required"`, httpTag) |
| 189 | + |
| 190 | + // snake opt + camel opt => snake fmt and then camel fmt |
| 191 | + httpTag = genHTTPTags(&parser.Field{ |
| 192 | + Name: "FieldB", |
| 193 | + Requiredness: parser.FieldType_Required, |
| 194 | + }, genHTTPTagOption{snakeTyleJSONTag: true, lowerCamelCaseJSONTag: true}) |
| 195 | + test.Assert(t, httpTag == ` form:"FieldB,required" json:"fieldB,required" query:"FieldB,required"`, httpTag) |
| 196 | + |
| 197 | + // it also works for api.body json tag |
| 198 | + httpTag = genHTTPTags(&parser.Field{ |
| 199 | + Name: "field_c", |
| 200 | + Requiredness: parser.FieldType_Optional, |
| 201 | + Annotations: []*parser.Annotation{ |
| 202 | + {Key: "api.body", Values: []string{"hey_hello"}}, |
| 203 | + }, |
| 204 | + }, genHTTPTagOption{lowerCamelCaseJSONTag: true}) |
| 205 | + test.Assert(t, httpTag == ` form:"hey_hello" json:"heyHello,omitempty"`, httpTag) |
| 206 | + |
| 207 | + // snakeStyleHTTPTag option only works for form、query..... not for json and “json generated by api.body” |
| 208 | + httpTag = genHTTPTags(&parser.Field{ |
| 209 | + Name: "field_c", |
| 210 | + Requiredness: parser.FieldType_Optional, |
| 211 | + Annotations: []*parser.Annotation{ |
| 212 | + {Key: "api.body", Values: []string{"heyHello"}}, |
| 213 | + {Key: "api.query", Values: []string{"howAreYou"}}, |
| 214 | + {Key: "api.cookie", Values: []string{"fineThanks"}}, |
| 215 | + }, |
| 216 | + }, genHTTPTagOption{snakeStyleHTTPTag: true}) |
| 217 | + test.Assert(t, httpTag == ` cookie:"fine_thanks" form:"hey_hello" json:"heyHello,omitempty" query:"how_are_you"`, httpTag) |
| 218 | + |
| 219 | + // unsetOmitempty remove 'omitempty' in json |
| 220 | + httpTag = genHTTPTags(&parser.Field{ |
| 221 | + Name: "field_c", |
| 222 | + Requiredness: parser.FieldType_Optional, |
| 223 | + Annotations: []*parser.Annotation{ |
| 224 | + {Key: "api.body", Values: []string{"heyHello"}}, |
| 225 | + {Key: "api.query", Values: []string{"howAreYou"}}, |
| 226 | + {Key: "api.cookie", Values: []string{"fineThanks"}}, |
| 227 | + }, |
| 228 | + }, genHTTPTagOption{unsetOmitempty: true}) |
| 229 | + test.Assert(t, httpTag == ` cookie:"fineThanks" form:"heyHello" json:"heyHello" query:"howAreYou"`, httpTag) |
| 230 | +} |
0 commit comments