11package encoding_test
22
33import (
4- "context"
54 "io"
65 "testing"
76
@@ -11,45 +10,19 @@ import (
1110)
1211
1312func TestRegisterEncoder (t * testing.T ) {
14- t .Run ("Marshaler" , func (t * testing.T ) {
15- testRegisterEncoder (t , func (v any ) ([]byte , error ) {
16- b := v .([]byte )
17- if len (b ) == 0 {
18- return nil , io .EOF
19- }
20- return b , nil
21- }, "unmarshaler" , "marshaler-alias" )
22- })
23-
24- t .Run ("ContextMarshaler" , func (t * testing.T ) {
25- testRegisterEncoder (t , func (ctx context.Context , v any ) ([]byte , error ) {
26- b := v .([]byte )
27- if len (b ) == 0 {
28- return nil , io .EOF
29- }
30- return b , nil
31- }, "context-marshaler" , "context-marshaler-alias" )
32- })
33-
34- t .Run ("ResponseEncoder" , func (t * testing.T ) {
35- testRegisterEncoder (t , func (ctx * fasthttp.RequestCtx , v any ) error {
36- b := v .([]byte )
37- if len (b ) == 0 {
38- return io .EOF
39- }
40- ctx .Response .SetBodyRaw (b )
41- return nil
42- }, "response-encoder" , "response-encoder-alias" )
43- })
44- }
45-
46- func testRegisterEncoder [T encoding.EncoderConstraint ](t * testing.T , dec T , contentType , alias string ) {
47- t .Helper ()
13+ enc := func (ctx * fasthttp.RequestCtx , v any ) error {
14+ b := v .([]byte )
15+ if len (b ) == 0 {
16+ return io .EOF
17+ }
18+ ctx .Response .SetBodyRaw (b )
19+ return nil
20+ }
4821
49- encoding .RegisterEncoder (dec , contentType , alias )
22+ encoding .RegisterEncoder (enc , "response-encoder" , "response-encoder- alias" )
5023
51- for _ , v := range []string {contentType , alias } {
52- encode := encoding .GetEncoder (v )
24+ for _ , v := range []string {"response-encoder" , "response-encoder- alias" } {
25+ encode := encoding .GetEncoder ([] byte ( v ) )
5326 if encode == nil {
5427 t .Error ("encoder not found" )
5528 continue
@@ -76,12 +49,12 @@ func TestGetEncoderMultipleContentTypes(t *testing.T) {
7649
7750 encoding .RegisterEncoder (encFn , "application/xml" )
7851
79- enc := encoding .GetEncoder ("text/html,application/xhtml+xml,application/xml" )
52+ enc := encoding .GetEncoder ([] byte ( "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" ) )
8053 if enc == nil {
8154 t .Fatal ("encoder not found" )
8255 }
8356
84- enc = encoding .GetEncoder ("application/xhtml+xml" )
57+ enc = encoding .GetEncoder ([] byte ( "application/xhtml+xml" ) )
8558 if enc != nil {
8659 t .Fatal ("encoder should not be found" )
8760 }
0 commit comments