1package webhook23import "testing"45func TestParseContentType(t *testing.T) {6 tests := []struct {7 name string8 s string9 want ContentType10 err error11 }{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 }2930 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 return36 }37 if got != tt.want {38 t.Errorf("ParseContentType() got = %v, want %v", got, tt.want)39 }40 })41 }42}4344func TestUnmarshalText(t *testing.T) {45 tests := []struct {46 name string47 text []byte48 want ContentType49 wantErr bool50 }{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 }6768 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}8081func TestMarshalText(t *testing.T) {82 tests := []struct {83 name string84 c ContentType85 want []byte86 wantErr bool87 }{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 }104105 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 return111 }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}