go.uber.org/yarpc@v1.72.1/encoding/json/inbound_test.go (about) 1 // Copyright (c) 2022 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package json 22 23 import ( 24 "bytes" 25 "context" 26 "encoding/json" 27 "errors" 28 "io" 29 "reflect" 30 "testing" 31 32 "github.com/stretchr/testify/assert" 33 "github.com/stretchr/testify/require" 34 "go.uber.org/yarpc" 35 "go.uber.org/yarpc/api/transport" 36 "go.uber.org/yarpc/api/transport/transporttest" 37 ) 38 39 type simpleRequest struct { 40 Name string 41 Attributes map[string]int32 42 } 43 44 type simpleResponse struct { 45 Success bool 46 } 47 48 func TestHandleStructSuccess(t *testing.T) { 49 h := func(ctx context.Context, body *simpleRequest) (*simpleResponse, error) { 50 assert.Equal(t, "simpleCall", yarpc.CallFromContext(ctx).Procedure()) 51 assert.Equal(t, "foo", body.Name) 52 assert.Equal(t, map[string]int32{"bar": 42}, body.Attributes) 53 54 return &simpleResponse{Success: true}, nil 55 } 56 57 handler := jsonHandler{ 58 reader: structReader{reflect.TypeOf(simpleRequest{})}, 59 handler: reflect.ValueOf(h), 60 } 61 62 resw := new(transporttest.FakeResponseWriter) 63 err := handler.Handle(context.Background(), &transport.Request{ 64 Procedure: "simpleCall", 65 Encoding: "json", 66 Body: jsonBody(`{"name": "foo", "attributes": {"bar": 42}}`), 67 }, resw) 68 require.NoError(t, err) 69 70 var response simpleResponse 71 require.NoError(t, json.Unmarshal(resw.Body.Bytes(), &response)) 72 73 assert.Equal(t, simpleResponse{Success: true}, response) 74 } 75 76 func TestHandleMapSuccess(t *testing.T) { 77 h := func(ctx context.Context, body map[string]interface{}) (map[string]string, error) { 78 assert.Equal(t, 42.0, body["foo"]) 79 assert.Equal(t, []interface{}{"a", "b", "c"}, body["bar"]) 80 81 return map[string]string{"success": "true"}, nil 82 } 83 84 handler := jsonHandler{ 85 reader: mapReader{reflect.TypeOf(make(map[string]interface{}))}, 86 handler: reflect.ValueOf(h), 87 } 88 89 resw := new(transporttest.FakeResponseWriter) 90 err := handler.Handle(context.Background(), &transport.Request{ 91 Procedure: "foo", 92 Encoding: "json", 93 Body: jsonBody(`{"foo": 42, "bar": ["a", "b", "c"]}`), 94 }, resw) 95 require.NoError(t, err) 96 97 var response struct{ Success string } 98 require.NoError(t, json.Unmarshal(resw.Body.Bytes(), &response)) 99 assert.Equal(t, "true", response.Success) 100 } 101 102 func TestHandleInterfaceEmptySuccess(t *testing.T) { 103 h := func(ctx context.Context, body interface{}) (interface{}, error) { 104 return body, nil 105 } 106 107 handler := jsonHandler{reader: ifaceEmptyReader{}, handler: reflect.ValueOf(h)} 108 109 resw := new(transporttest.FakeResponseWriter) 110 err := handler.Handle(context.Background(), &transport.Request{ 111 Procedure: "foo", 112 Encoding: "json", 113 Body: jsonBody(`["a", "b", "c"]`), 114 }, resw) 115 require.NoError(t, err) 116 117 assert.JSONEq(t, `["a", "b", "c"]`, resw.Body.String()) 118 } 119 120 func TestHandleSuccessWithResponseHeaders(t *testing.T) { 121 h := func(ctx context.Context, _ *simpleRequest) (*simpleResponse, error) { 122 require.NoError(t, yarpc.CallFromContext(ctx).WriteResponseHeader("foo", "bar")) 123 return &simpleResponse{Success: true}, nil 124 } 125 126 handler := jsonHandler{ 127 reader: structReader{reflect.TypeOf(simpleRequest{})}, 128 handler: reflect.ValueOf(h), 129 } 130 131 resw := new(transporttest.FakeResponseWriter) 132 err := handler.Handle(context.Background(), &transport.Request{ 133 Procedure: "simpleCall", 134 Encoding: "json", 135 Body: jsonBody(`{"name": "foo", "attributes": {"bar": 42}}`), 136 }, resw) 137 require.NoError(t, err) 138 139 assert.Equal(t, transport.NewHeaders().With("foo", "bar"), resw.Headers) 140 } 141 142 func TestHandleBothResponseError(t *testing.T) { 143 h := func(ctx context.Context, body *simpleRequest) (*simpleResponse, error) { 144 assert.Equal(t, "simpleCall", yarpc.CallFromContext(ctx).Procedure()) 145 assert.Equal(t, "foo", body.Name) 146 assert.Equal(t, map[string]int32{"bar": 42}, body.Attributes) 147 148 return &simpleResponse{Success: true}, errors.New("bar") 149 } 150 151 handler := jsonHandler{ 152 reader: structReader{reflect.TypeOf(simpleRequest{})}, 153 handler: reflect.ValueOf(h), 154 } 155 156 resw := new(transporttest.FakeResponseWriter) 157 err := handler.Handle(context.Background(), &transport.Request{ 158 Procedure: "simpleCall", 159 Encoding: "json", 160 Body: jsonBody(`{"name": "foo", "attributes": {"bar": 42}}`), 161 }, resw) 162 require.Equal(t, errors.New("bar"), err) 163 164 var response simpleResponse 165 require.NoError(t, json.Unmarshal(resw.Body.Bytes(), &response)) 166 167 assert.Equal(t, simpleResponse{Success: true}, response) 168 } 169 170 func jsonBody(s string) io.Reader { 171 return bytes.NewReader([]byte(s)) 172 }