1package webhook
  2
  3import "testing"
  4
  5func TestParseContentType(t *testing.T) {
  6	tests := []struct {
  7		name string
  8		s    string
  9		want ContentType
 10		err  error
 11	}{
 12		{
 13			name: "JSON",
 14			s:    "application/json",
 15			want: ContentTypeJSON,
 16		},
 17		{
 18			name: "Form",
 19			s:    "application/x-www-form-urlencoded",
 20			want: ContentTypeForm,
 21		},
 22		{
 23			name: "Invalid",
 24			s:    "application/invalid",
 25			err:  ErrInvalidContentType,
 26			want: -1,
 27		},
 28	}
 29
 30	for _, tt := range tests {
 31		t.Run(tt.name, func(t *testing.T) {
 32			got, err := ParseContentType(tt.s)
 33			if err != tt.err {
 34				t.Errorf("ParseContentType() error = %v, wantErr %v", err, tt.err)
 35				return
 36			}
 37			if got != tt.want {
 38				t.Errorf("ParseContentType() got = %v, want %v", got, tt.want)
 39			}
 40		})
 41	}
 42}
 43
 44func TestUnmarshalText(t *testing.T) {
 45	tests := []struct {
 46		name    string
 47		text    []byte
 48		want    ContentType
 49		wantErr bool
 50	}{
 51		{
 52			name: "JSON",
 53			text: []byte("application/json"),
 54			want: ContentTypeJSON,
 55		},
 56		{
 57			name: "Form",
 58			text: []byte("application/x-www-form-urlencoded"),
 59			want: ContentTypeForm,
 60		},
 61		{
 62			name:    "Invalid",
 63			text:    []byte("application/invalid"),
 64			wantErr: true,
 65		},
 66	}
 67
 68	for _, tt := range tests {
 69		t.Run(tt.name, func(t *testing.T) {
 70			c := new(ContentType)
 71			if err := c.UnmarshalText(tt.text); (err != nil) != tt.wantErr {
 72				t.Errorf("ContentType.UnmarshalText() error = %v, wantErr %v", err, tt.wantErr)
 73			}
 74			if *c != tt.want {
 75				t.Errorf("ContentType.UnmarshalText() got = %v, want %v", *c, tt.want)
 76			}
 77		})
 78	}
 79}
 80
 81func TestMarshalText(t *testing.T) {
 82	tests := []struct {
 83		name    string
 84		c       ContentType
 85		want    []byte
 86		wantErr bool
 87	}{
 88		{
 89			name: "JSON",
 90			c:    ContentTypeJSON,
 91			want: []byte("application/json"),
 92		},
 93		{
 94			name: "Form",
 95			c:    ContentTypeForm,
 96			want: []byte("application/x-www-form-urlencoded"),
 97		},
 98		{
 99			name:    "Invalid",
100			c:       ContentType(-1),
101			wantErr: true,
102		},
103	}
104
105	for _, tt := range tests {
106		t.Run(tt.name, func(t *testing.T) {
107			b, err := tt.c.MarshalText()
108			if (err != nil) != tt.wantErr {
109				t.Errorf("ContentType.MarshalText() error = %v, wantErr %v", err, tt.wantErr)
110				return
111			}
112			if string(b) != string(tt.want) {
113				t.Errorf("ContentType.MarshalText() got = %v, want %v", string(b), string(tt.want))
114			}
115		})
116	}
117}