|
| 1 | +package httpheader |
| 2 | + |
| 3 | +import ( |
| 4 | +"net/http" |
| 5 | +"reflect" |
| 6 | +"testing" |
| 7 | +"time" |
| 8 | +) |
| 9 | + |
| 10 | +// Event defines a Google Calendar hook event type |
| 11 | +type Event string |
| 12 | + |
| 13 | +var pl *GoogleCalendarPayload |
| 14 | + |
| 15 | +// GoogleCalendar hook types |
| 16 | +const ( |
| 17 | +SyncEvent Event = "sync" |
| 18 | +ExistsEvent Event = "exists" |
| 19 | +NotExistsEvent Event = "not_exists" |
| 20 | +) |
| 21 | + |
| 22 | +// GoogleCalendarPayload a google calendar notice |
| 23 | +// https://developers.google.com/calendar/v3/push |
| 24 | +type GoogleCalendarPayload struct { |
| 25 | +ChannelID string `header:"X-Goog-Channel-ID"` |
| 26 | +ChannelToken string `header:"X-Goog-Channel-Token,omitempty"` |
| 27 | +ChannelExpiration time.Time `header:"X-Goog-Channel-Expiration,omitempty"` |
| 28 | +ResourceID string `header:"X-Goog-Resource-ID"` |
| 29 | +ResourceURI string `header:"X-Goog-Resource-URI"` |
| 30 | +ResourceState string `header:"X-Goog-Resource-State"` |
| 31 | +MessageNumber int `header:"X-Goog-Message-Number"` |
| 32 | +} |
| 33 | + |
| 34 | +func init() { |
| 35 | +pl = &GoogleCalendarPayload{ |
| 36 | +ChannelID: "channel-ID-value", |
| 37 | +ChannelToken: "channel-token-value", |
| 38 | +ResourceID: "identifier-for-the-watched-resource", |
| 39 | +ResourceURI: "version-specific-URI-of-the-watched-resource", |
| 40 | +MessageNumber: 1, |
| 41 | +} |
| 42 | +pl.ChannelExpiration, _ = time.Parse(time.RFC1123, "Tue, 19 Nov 2013 01:13:52 GMT") |
| 43 | + |
| 44 | +} |
| 45 | + |
| 46 | +func getHeader(e Event) http.Header { |
| 47 | +h := http.Header{} |
| 48 | +h.Add("X-Goog-Channel-ID", "channel-ID-value") |
| 49 | +h.Add("X-Goog-Channel-Token", "channel-token-value") |
| 50 | +h.Add("X-Goog-Channel-Expiration", "Tue, 19 Nov 2013 01:13:52 GMT") |
| 51 | +h.Add("X-Goog-Resource-ID", "identifier-for-the-watched-resource") |
| 52 | +h.Add("X-Goog-Resource-URI", "version-specific-URI-of-the-watched-resource") |
| 53 | +h.Add("X-Goog-Message-Number", "1") |
| 54 | +h.Add("X-Goog-Resource-State", string(e)) |
| 55 | +return h |
| 56 | +} |
| 57 | + |
| 58 | +func TestDecodeHeader(t *testing.T) { |
| 59 | +type args struct { |
| 60 | +e Event |
| 61 | +} |
| 62 | +tests := []struct { |
| 63 | +name string |
| 64 | +args args |
| 65 | +wantErr bool |
| 66 | +}{ |
| 67 | +{"Google Calendar sync", args{SyncEvent}, false}, |
| 68 | +{"Google Calendar exists", args{ExistsEvent}, false}, |
| 69 | +{"Google Calendar no exists", args{NotExistsEvent}, false}, |
| 70 | +} |
| 71 | + |
| 72 | +for i, tt := range tests { |
| 73 | +t.Run(tt.name, func(t *testing.T) { |
| 74 | +plrun := *pl |
| 75 | +plrun.ResourceState = string(tt.args.e) |
| 76 | +gcp := GoogleCalendarPayload{} |
| 77 | +err := DecodeHeader(getHeader(tt.args.e), &gcp) |
| 78 | +if (err != nil) != tt.wantErr { |
| 79 | +t.Errorf("%d. DecodeHeader() error = %+v, wantErr %+v", i, err, tt.wantErr) |
| 80 | +} |
| 81 | +if !reflect.DeepEqual(gcp, plrun) { |
| 82 | +t.Errorf("%d. DecodeHeader() does not work as expected, \ngot %+v \nwant %+v", i, gcp, plrun) |
| 83 | +} |
| 84 | +}) |
| 85 | +} |
| 86 | +} |
0 commit comments