trpc.group/trpc-go/trpc-go@v1.0.3/http/serialization_form_data_test.go (about) 1 // 2 // 3 // Tencent is pleased to support the open source community by making tRPC available. 4 // 5 // Copyright (C) 2023 THL A29 Limited, a Tencent company. 6 // All rights reserved. 7 // 8 // If you have downloaded a copy of the tRPC source code from Tencent, 9 // please note that tRPC source code is licensed under the Apache 2.0 License, 10 // A copy of the Apache 2.0 License is included in this file. 11 // 12 // 13 14 package http 15 16 import ( 17 "reflect" 18 "testing" 19 20 "trpc.group/trpc-go/trpc-go/codec" 21 ) 22 23 func Test_getFormDataContentType(t *testing.T) { 24 tests := []struct { 25 name string 26 marshalType int 27 want string 28 }{ 29 { 30 name: "err", 31 marshalType: -1, 32 want: "", 33 }, 34 { 35 name: "normal", 36 marshalType: codec.SerializationTypeJSON, 37 want: "application/json", 38 }, 39 } 40 for _, tt := range tests { 41 t.Run(tt.name, func(t *testing.T) { 42 FormDataMarshalType = tt.marshalType 43 if got := getFormDataContentType(); got != tt.want { 44 t.Errorf("getFormDataContentType() = %v, want %v", got, tt.want) 45 } 46 }) 47 } 48 } 49 50 func TestNewFormDataSerialization(t *testing.T) { 51 type args struct { 52 tag string 53 } 54 tests := []struct { 55 name string 56 args args 57 want codec.Serializer 58 }{ 59 { 60 name: "", 61 args: args{ 62 tag: "json", 63 }, 64 want: &FormDataSerialization{ 65 tagName: "json", 66 }, 67 }, 68 } 69 for _, tt := range tests { 70 t.Run(tt.name, func(t *testing.T) { 71 if got := NewFormDataSerialization(tt.args.tag); !reflect.DeepEqual(got, tt.want) { 72 t.Errorf("NewFormDataSerialization() = %v, want %v", got, tt.want) 73 } 74 }) 75 } 76 } 77 78 func TestFormDataSerialization_Unmarshal(t *testing.T) { 79 type fields struct { 80 tagName string 81 } 82 type args struct { 83 in []byte 84 body interface{} 85 } 86 87 type request struct { 88 Competition string `protobuf:"bytes,1,opt,name=competition,proto3" json:"competition,omitempty"` 89 Season int32 `protobuf:"varint,2,opt,name=season,proto3" json:"season,omitempty"` 90 Teams []string `protobuf:"bytes,3,rep,name=teams,proto3" json:"teams,omitempty"` 91 } 92 93 tests := []struct { 94 name string 95 fields fields 96 args args 97 wantErr bool 98 wantBody interface{} 99 }{ 100 { 101 name: "normal", 102 fields: fields{ 103 tagName: "json", 104 }, 105 args: args{ 106 in: []byte("competition=NBA&season=2021&teams=%E6%B9%96%E4%BA%BA&teams=%E5%8B%87%E5%A3%AB"), 107 body: &request{}, 108 }, 109 wantErr: false, 110 wantBody: &request{ 111 Competition: "NBA", 112 Season: 2021, 113 Teams: []string{"湖人", "勇士"}, 114 }, 115 }, 116 { 117 name: "err", 118 fields: fields{ 119 tagName: "json", 120 }, 121 args: args{ 122 in: []byte("competition=NBA&season=2021&teams=%E6%B9%96%E4%BA%BA&teams=%E5%8B%87%E5%A3%AB&competition=OPTA"), 123 body: &request{}, 124 }, 125 wantErr: true, 126 wantBody: &request{ 127 Competition: "", 128 Season: 2021, 129 Teams: []string{"湖人", "勇士"}, 130 }, 131 }, 132 } 133 for _, tt := range tests { 134 t.Run(tt.name, func(t *testing.T) { 135 j := &FormDataSerialization{ 136 tagName: tt.fields.tagName, 137 } 138 if err := j.Unmarshal(tt.args.in, tt.args.body); (err != nil) != tt.wantErr { 139 t.Errorf("Unmarshal() error = %v, wantErr %v", err, tt.wantErr) 140 } 141 if !reflect.DeepEqual(tt.args.body, tt.wantBody) { 142 t.Errorf("Unmarshal() body = %#v, wantBody %#v", tt.args.body, tt.wantBody) 143 } 144 }) 145 } 146 } 147 148 func TestFormDataSerialization_Marshal(t *testing.T) { 149 type fields struct { 150 tagName string 151 } 152 type args struct { 153 serializationType int 154 body interface{} 155 } 156 tests := []struct { 157 name string 158 fields fields 159 args args 160 want string 161 wantErr bool 162 }{ 163 { 164 name: "err", 165 fields: fields{ 166 tagName: "json", 167 }, 168 args: args{ 169 serializationType: -1, 170 body: &struct { 171 DataOrigin string `json:"dataOrigin,omitempty"` 172 OriginalID string `json:"originalID,omitempty"` 173 NewPlayerID int64 `json:"newPlayerID,omitempty"` 174 PlayerIDs []int64 `json:"playerIDs,omitempty"` 175 }{ 176 DataOrigin: "opta", 177 OriginalID: "40669", 178 NewPlayerID: 148681, 179 PlayerIDs: []int64{13, 14}, 180 }, 181 }, 182 want: "", 183 wantErr: true, 184 }, 185 { 186 name: "normal", 187 fields: fields{ 188 tagName: "json", 189 }, 190 args: args{ 191 serializationType: codec.SerializationTypeJSON, 192 body: &struct { 193 DataOrigin string `json:"dataOrigin,omitempty"` 194 OriginalID string `json:"originalID,omitempty"` 195 NewPlayerID int64 `json:"newPlayerID,omitempty"` 196 PlayerIDs []int64 `json:"playerIDs,omitempty"` 197 }{ 198 DataOrigin: "opta", 199 OriginalID: "40669", 200 NewPlayerID: 148681, 201 PlayerIDs: []int64{13, 14}, 202 }, 203 }, 204 want: `{"dataOrigin":"opta","originalID":"40669","newPlayerID":148681,"playerIDs":[13,14]}`, 205 wantErr: false, 206 }, 207 } 208 for _, tt := range tests { 209 t.Run(tt.name, func(t *testing.T) { 210 j := &FormDataSerialization{ 211 tagName: tt.fields.tagName, 212 } 213 FormDataMarshalType = tt.args.serializationType 214 got, err := j.Marshal(tt.args.body) 215 if (err != nil) != tt.wantErr { 216 t.Errorf("Marshal() error = %v, wantErr %v", err, tt.wantErr) 217 return 218 } 219 if !reflect.DeepEqual(string(got), tt.want) { 220 t.Errorf("Marshal() got = %v, want %v", string(got), tt.want) 221 } 222 }) 223 } 224 }