trpc.group/trpc-go/trpc-go@v1.0.3/http/serialization_get_test.go (about) 1 // 2 // 3 // Tencent is pleased to support the open source community by making tRPC available. 4 // 5 // Copyright (C) 2023 THL A29 Limited, a Tencent company. 6 // All rights reserved. 7 // 8 // If you have downloaded a copy of the tRPC source code from Tencent, 9 // please note that tRPC source code is licensed under the Apache 2.0 License, 10 // A copy of the Apache 2.0 License is included in this file. 11 // 12 // 13 14 package http_test 15 16 import ( 17 "testing" 18 19 "github.com/stretchr/testify/require" 20 21 "trpc.group/trpc-go/trpc-go/codec" 22 "trpc.group/trpc-go/trpc-go/http" 23 ) 24 25 func TestGetSerializerRegister(t *testing.T) { 26 defer func() { 27 e := recover() 28 require.Nil(t, e) 29 }() 30 31 s := codec.GetSerializer(codec.SerializationTypeGet) 32 defer func() { 33 codec.RegisterSerializer(codec.SerializationTypeGet, s) 34 }() 35 codec.RegisterSerializer(codec.SerializationTypeGet, http.NewGetSerialization("json")) 36 serializer := codec.GetSerializer(codec.SerializationTypeGet) 37 require.NotNil(t, serializer) 38 } 39 40 func TestGetSerializer(t *testing.T) { 41 require := require.New(t) 42 s := codec.GetSerializer(codec.SerializationTypeGet) 43 44 type Data struct { 45 X int `json:"x"` 46 Y string `json:"y"` 47 Z []string `json:"z"` 48 } 49 50 var queries = []string{ 51 "x=1&y=nice&z=3", 52 "x=1&y=2&z", 53 "x=1&y=2", 54 "x=1&y=2&z=z1&z=z2", 55 } 56 57 var expects = []*Data{ 58 { 59 X: 1, 60 Y: "nice", 61 Z: []string{"3"}, 62 }, 63 { 64 X: 1, 65 Y: "2", 66 Z: []string{""}, 67 }, 68 { 69 X: 1, 70 Y: "2", 71 Z: nil, 72 }, 73 { 74 X: 1, 75 Y: "2", 76 Z: []string{"z1", "z2"}, 77 }, 78 } 79 80 var expectedQueries = []string{ 81 "{\"x\":1,\"y\":\"nice\",\"z\":[\"3\"]}", 82 "{\"x\":1,\"y\":\"2\",\"z\":[\"\"]}", 83 "{\"x\":1,\"y\":\"2\",\"z\":null}", 84 "{\"x\":1,\"y\":\"2\",\"z\":[\"z1\",\"z2\"]}", 85 } 86 87 for i, query := range queries { 88 data := &Data{} 89 s.Unmarshal([]byte(query), &data) 90 require.Equal(data.X, expects[i].X, "x should be equal") 91 require.Equal(data.Y, expects[i].Y, "y should be equal") 92 require.Equal(data.Z, expects[i].Z, "z should be equal") 93 } 94 95 for i, query := range expects { 96 buf, _ := s.Marshal(&query) 97 require.Equal(string(buf), expectedQueries[i], "x should be equal") 98 } 99 100 old := codec.GetSerializer(codec.SerializationTypeJSON) 101 defer func() { 102 codec.RegisterSerializer(codec.SerializationTypeJSON, old) 103 }() 104 codec.RegisterSerializer(codec.SerializationTypeJSON, &codec.NoopSerialization{}) 105 _, err := s.Marshal(queries) 106 require.NotNil(t, err, "json codec empty") 107 108 } 109 110 func TestGetUnmarshal(t *testing.T) { 111 require := require.New(t) 112 s := codec.GetSerializer(codec.SerializationTypeGet) 113 114 type formStruct struct{} 115 form := &formStruct{} 116 117 require.NotNil(s.Unmarshal([]byte("%gh&%ij"), &form)) 118 require.NotNil(s.Unmarshal([]byte("x=1&y=2"), (map[string]interface{})(nil))) 119 120 type queryStruct struct { 121 Query []byte `json:"query"` 122 } 123 query := &queryStruct{} 124 require.NotNil(s.Unmarshal([]byte("%gh&%ij"), &query)) 125 require.Nil(s.Unmarshal([]byte("x=1&y=2"), &query)) 126 127 // Test Chinese. 128 query = &queryStruct{} 129 require.Nil(s.Unmarshal([]byte("query=中文&y=2"), &query)) 130 require.Equal([]byte("中文"), query.Query) 131 } 132 133 func TestGetMarshal(t *testing.T) { 134 require := require.New(t) 135 s := codec.GetSerializer(codec.SerializationTypeGet) 136 old := codec.GetSerializer(codec.SerializationTypeJSON) 137 defer func() { 138 codec.RegisterSerializer(codec.SerializationTypeJSON, old) 139 }() 140 codec.RegisterSerializer(codec.SerializationTypeJSON, nil) 141 _, err := s.Marshal(require) 142 require.NotNil(err) 143 }