1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
|
package httpheader
import (
"fmt"
"net/http"
"net/textproto"
"reflect"
"regexp"
"sort"
"testing"
"time"
)
// Event defines a Google Calendar hook event type
type Event string
// GoogleCalendar hook types
const (
SyncEvent Event = "sync"
ExistsEvent Event = "exists"
NotExistsEvent Event = "not_exists"
)
// GoogleCalendarPayload a google calendar notice
// https://developers.google.com/calendar/v3/push
type GoogleCalendarPayload struct {
ChannelID string `header:"X-Goog-Channel-ID"`
ChannelToken string `header:"X-Goog-Channel-Token,omitempty"`
ChannelExpiration time.Time `header:"X-Goog-Channel-Expiration,omitempty"`
ResourceID string `header:"X-Goog-Resource-ID"`
ResourceURI string `header:"X-Goog-Resource-URI"`
ResourceState string `header:"X-Goog-Resource-State"`
MessageNumber int `header:"X-Goog-Message-Number"`
}
func getHeader(e Event) http.Header {
h := http.Header{}
h.Add("X-Goog-Channel-ID", "channel-ID-value")
h.Add("X-Goog-Channel-Token", "channel-token-value")
h.Add("X-Goog-Channel-Expiration", "Tue, 19 Nov 2013 01:13:52 GMT")
h.Add("X-Goog-Resource-ID", "identifier-for-the-watched-resource")
h.Add("X-Goog-Resource-URI", "version-specific-URI-of-the-watched-resource")
h.Add("X-Goog-Message-Number", "1")
h.Add("X-Goog-Resource-State", string(e))
return h
}
func TestDecodeHeader(t *testing.T) {
type args struct {
e Event
}
tests := []struct {
name string
args args
wantErr bool
}{
{"Google Calendar sync", args{SyncEvent}, false},
{"Google Calendar exists", args{ExistsEvent}, false},
{"Google Calendar no exists", args{NotExistsEvent}, false},
}
for i, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
plrun := GoogleCalendarPayload{
ChannelID: "channel-ID-value",
ChannelToken: "channel-token-value",
ResourceID: "identifier-for-the-watched-resource",
ResourceURI: "version-specific-URI-of-the-watched-resource",
MessageNumber: 1,
}
plrun.ChannelExpiration, _ = time.Parse(http.TimeFormat, "Tue, 19 Nov 2013 01:13:52 GMT")
plrun.ResourceState = string(tt.args.e)
gcp := GoogleCalendarPayload{}
err := Decode(getHeader(tt.args.e), &gcp)
if (err != nil) != tt.wantErr {
t.Errorf("%d. Decode() error = %+v, wantErr %+v", i, err, tt.wantErr)
}
if !reflect.DeepEqual(gcp, plrun) {
t.Errorf("%d. Decode() does not work as expected, \ngot %+v \nwant %+v", i, gcp, plrun)
}
})
}
}
type DecodedArgs []string
func (m *DecodedArgs) DecodeHeader(header http.Header, key string) error {
baseKey := textproto.CanonicalMIMEHeaderKey(key)
keyMatch := regexp.MustCompile(fmt.Sprintf(`^%s\.\d+$`, baseKey))
var args DecodedArgs
for k := range header {
if keyMatch.MatchString(textproto.CanonicalMIMEHeaderKey(k)) {
args = append(args, header.Get(k))
}
}
// TODO: sort args by id
sort.Strings(args)
if len(args) > 0 {
*m = args
}
return nil
}
func TestDecodeHeader_Unmarshaler(t *testing.T) {
type ArgStruct struct {
Args DecodedArgs `header:"Arg"`
}
input := http.Header{
"Arg.0": []string{"a"},
"Arg.1": []string{"b"},
"Arg.2": []string{"c"},
}
want := ArgStruct{
Args: []string{"a", "b", "c"},
}
var got ArgStruct
err := Decode(input, &got)
if err != nil {
t.Errorf("want no error, got error: %#v", err)
}
if !reflect.DeepEqual(want, got) {
t.Errorf("Decode returned %#v, want %#v", got, want)
}
}
func TestDecodeHeader_UnmarshalerWithNilPointer(t *testing.T) {
s := struct {
Args *EncodedArgs `header:"Arg"`
}{}
err := Decode(http.Header{}, s)
if err == nil {
t.Error("want error but got nil")
}
}
type simpleStruct struct {
Foo string
Bar int
}
type fullTypeStruct struct {
unExport string
UnExportTwo string `header:"-"`
Bool bool `header:"Bool"`
BoolInt bool `header:"Bool-Int,int"`
String string
StringEmpty string `header:"String-Empty"`
StringEmptyIgnore string `header:"String-Empty-Ignore,omitempty"`
Uint uint
Uint64 uint64
Uint8 uint8
Uint16 uint16
Uint32 uint32
Int int
Int64 int64
Int8 int8
Int16 int16
Int32 int32
Float32 float32
Float64 float64
Slice []string
SliceTwo []int `header:"Slice-Two"`
Array [3]string
ArrayTwo [2]int `header:"Array-Two"`
Interface interface{}
Time time.Time
TimeUnix time.Time `header:"Time-Unix,unix"`
// Point *string
Args DecodedArgs `header:"Arg"`
Foo simpleStruct
}
func TestDecodeHeader_more_data_type(t *testing.T) {
timeV := time.Date(2000, 1, 1, 12, 34, 56, 0, time.UTC)
timeS := "Sat, 01 Jan 2000 12:34:56 GMT"
timeU := "946730096"
h := http.Header{
"UnExportTwo": []string{"foo"},
"UnExport-Two": []string{"foo"},
"Bool": []string{"true"},
"Bool-Int": []string{"1"},
"String": []string{"foobar"},
"String-Empty": []string{""},
"Uint": []string{"2"},
"Uint64": []string{"3"},
"Uint8": []string{"4"},
"Uint16": []string{"5"},
"Uint32": []string{"6"},
"Int": []string{"7"},
"Int64": []string{"8"},
"Int8": []string{"9"},
"Int16": []string{"10"},
"Int32": []string{"11"},
"Float32": []string{"12.2"},
"Float64": []string{"13.2"},
"Slice": []string{"a", "b", "c"},
"Slice-Two": []string{"1", "2", "3"},
"Array": []string{"a", "b", "c"},
"Array-Two": []string{"1", "2", "3"},
"Interface": []string{"foo", "bar"},
"Time": []string{timeS},
"Time-Unix": []string{timeU},
"Point": []string{"foo"},
"Arg.0": []string{"a"},
"Arg.1": []string{"b"},
"Arg.2": []string{"c"},
"Foo": []string{"bar"},
}
want := fullTypeStruct{
unExport: "",
UnExportTwo: "",
Bool: true,
BoolInt: true,
String: "foobar",
StringEmpty: "",
StringEmptyIgnore: "",
Uint: 2,
Uint64: 3,
Uint8: 4,
Uint16: 5,
Uint32: 6,
Int: 7,
Int64: 8,
Int8: 9,
Int16: 10,
Int32: 11,
Float32: 12.2,
Float64: 13.2,
Slice: []string{"a", "b", "c"},
SliceTwo: []int{1, 2, 3},
Array: [3]string{"a", "b", "c"},
ArrayTwo: [2]int{1, 2},
Interface: interface{}([]string{"foo", "bar"}),
Time: timeV,
TimeUnix: timeV,
// Point: stringPoint("foo"),
Args: []string{"a", "b", "c"},
Foo: simpleStruct{Foo: "bar"},
}
var got fullTypeStruct
err := Decode(h, &got)
if err != nil {
t.Errorf("Decode returned error: %#v", err)
}
if !reflect.DeepEqual(want, got) {
t.Errorf("want %#v, but got %#v", want, got)
}
}
func TestDecodeHeader_point(t *testing.T) {
type A struct {
Point *string
}
h := http.Header{}
h.Set("Point", "foobar")
want := A{
Point: stringPoint("foobar"),
}
var got A
err := Decode(h, &got)
if err != nil {
t.Errorf("Decode returned error: %#v", err)
}
if !reflect.DeepEqual(want, got) {
t.Errorf("want %#v, but got %#v", want, got)
}
}
func stringPoint(s string) *string {
return &s
}
func Test_fillValues_errors(t *testing.T) {
type args struct {
sv reflect.Value
opts tagOptions
valArr []string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Uint",
args: args{
sv: reflect.New(reflect.TypeOf(uint(3))),
opts: tagOptions{},
valArr: []string{"a"},
},
wantErr: true,
},
{
name: "Uint64",
args: args{
sv: reflect.New(reflect.TypeOf(uint64(3))),
opts: tagOptions{},
valArr: []string{"a"},
},
wantErr: true,
},
{
name: "Uint8",
args: args{
sv: reflect.New(reflect.TypeOf(uint8(3))),
opts: tagOptions{},
valArr: []string{"a"},
},
wantErr: true,
},
{
name: "Uint16",
args: args{
sv: reflect.New(reflect.TypeOf(uint16(3))),
opts: tagOptions{},
valArr: []string{"a"},
},
wantErr: true,
},
{
name: "Uint32",
args: args{
sv: reflect.New(reflect.TypeOf(uint32(3))),
opts: tagOptions{},
valArr: []string{"a"},
},
wantErr: true,
},
{
name: "int",
args: args{
sv: reflect.New(reflect.TypeOf(int(3))),
opts: tagOptions{},
valArr: []string{"a"},
},
wantErr: true,
},
{
name: "int64",
args: args{
sv: reflect.New(reflect.TypeOf(int64(3))),
opts: tagOptions{},
valArr: []string{"a"},
},
wantErr: true,
},
{
name: "int8",
args: args{
sv: reflect.New(reflect.TypeOf(int8(3))),
opts: tagOptions{},
valArr: []string{"a"},
},
wantErr: true,
},
{
name: "int16",
args: args{
sv: reflect.New(reflect.TypeOf(int16(3))),
opts: tagOptions{},
valArr: []string{"a"},
},
wantErr: true,
},
{
name: "int32",
args: args{
sv: reflect.New(reflect.TypeOf(int32(3))),
opts: tagOptions{},
valArr: []string{"a"},
},
wantErr: true,
},
{
name: "float32",
args: args{
sv: reflect.New(reflect.TypeOf(float32(3))),
opts: tagOptions{},
valArr: []string{"a"},
},
wantErr: true,
},
{
name: "float64",
args: args{
sv: reflect.New(reflect.TypeOf(float64(3))),
opts: tagOptions{},
valArr: []string{"a"},
},
wantErr: true,
},
{
name: "slice",
args: args{
sv: reflect.New(reflect.TypeOf([]int{})),
opts: tagOptions{},
valArr: []string{"a"},
},
wantErr: true,
},
{
name: "array",
args: args{
sv: reflect.New(reflect.TypeOf([1]int{})),
opts: tagOptions{},
valArr: []string{"a"},
},
wantErr: true,
},
{
name: "time",
args: args{
sv: reflect.New(reflect.TypeOf(time.Time{})),
opts: tagOptions{},
valArr: []string{"a"},
},
wantErr: true,
},
{
name: "time unix",
args: args{
sv: reflect.New(reflect.TypeOf(time.Time{})),
opts: tagOptions{"unix"},
valArr: []string{"a"},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := fillValues(tt.args.sv, tt.args.opts, tt.args.valArr); (err != nil) != tt.wantErr {
t.Errorf("fillValues() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestDecode_check_header_key_not_present_no_point(t *testing.T) {
h := http.Header{}
h.Set("Length", "100")
var got fullTypeStruct
err := Decode(h, &got)
if err != nil {
t.Errorf("Decode returned error: %#v", err)
}
var want fullTypeStruct
if !reflect.DeepEqual(want, got) {
t.Errorf("want %#v, but got %#v", want, got)
}
}
func TestDecode_check_header_key_not_present_point(t *testing.T) {
type testStruct struct {
A *string
B *fullTypeStruct
C *int
D *[]string
E *[2]string
F interface{}
G *time.Time
}
h := http.Header{}
h.Set("Length", "100")
var got testStruct
err := Decode(h, &got)
if err != nil {
t.Errorf("Decode returned error: %#v", err)
}
var want testStruct
if !reflect.DeepEqual(want, got) {
t.Errorf("want %#v, but got %#v", want, got)
}
if got.A != nil || got.B != nil || got.C != nil || got.D != nil || got.E != nil || got.F != nil || got.G != nil {
t.Error("all fields should be nil")
}
}
func TestDecode_error(t *testing.T) {
h := http.Header{
"Int": []string{"abc"},
}
var got fullTypeStruct
err := Decode(h, &got)
if err == nil {
t.Errorf("expect error, got : %#v", got)
}
}
|