dubbo.apache.org/dubbo-go/v3@v3.1.1/protocol/jsonrpc/http_test.go (about) 1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package jsonrpc 19 20 import ( 21 "context" 22 ) 23 24 import ( 25 perrors "github.com/pkg/errors" 26 ) 27 28 // import ( 29 // 30 // "context" 31 // "strings" 32 // "testing" 33 // "time" 34 // 35 // ) 36 // 37 // import ( 38 // 39 // "github.com/opentracing/opentracing-go" 40 // 41 // perrors "github.com/pkg/errors" 42 // 43 // "github.com/stretchr/testify/assert" 44 // 45 // ) 46 // 47 // import ( 48 // 49 // "dubbo.apache.org/dubbo-go/v3/common" 50 // "dubbo.apache.org/dubbo-go/v3/common/constant" 51 // "dubbo.apache.org/dubbo-go/v3/proxy/proxy_factory" 52 // "dubbo.apache.org/dubbo-go/v3/protocol" 53 // 54 // ) 55 type ( 56 User struct { 57 ID string `json:"id"` 58 Name string `json:"name"` 59 } 60 61 UserProvider struct { // user map[string]User 62 } 63 ) 64 65 // const ( 66 // 67 // mockJsonCommonUrl = "jsonrpc://127.0.0.1:20001/com.ikurento.user.UserProvider?anyhost=true&" + 68 // "application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&" + 69 // "environment=dev&interface=com.ikurento.user.UserProvider&ip=192.168.56.1&methods=GetUser%2C&" + 70 // "module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&" + 71 // "side=provider&timeout=3000×tamp=1556509797245&bean.name=UserProvider" 72 // 73 // ) 74 // 75 // func TestHTTPClientCall(t *testing.T) { 76 // methods, err := common.ServiceMap.Register("com.ikurento.user.UserProvider", "jsonrpc", "", "", &UserProvider{}) 77 // assert.NoError(t, err) 78 // assert.Equal(t, "GetUser,GetUser0,GetUser1,GetUser2,GetUser3,GetUser4", methods) 79 // 80 // // Export 81 // proto := GetProtocol() 82 // url, err := common.NewURL(mockJsonCommonUrl) 83 // assert.NoError(t, err) 84 // proto.Export(&proxy_factory.ProxyInvoker{ 85 // BaseInvoker: *protocol.NewBaseInvoker(url), 86 // }) 87 // time.Sleep(time.Second * 2) 88 // 89 // client := NewHTTPClient(&HTTPOptions{}) 90 // 91 // // call GetUser 92 // ctx := context.WithValue(context.Background(), constant.DUBBOGO_CTX_KEY, map[string]string{ 93 // "X-Proxy-ID": "dubbogo", 94 // "X-Services": url.Path, 95 // "X-Method": "GetUser", 96 // }) 97 // 98 // req := client.NewRequest(url, "GetUser", []interface{}{"1", "username"}) 99 // reply := &User{} 100 // err = client.Call(ctx, url, req, reply) 101 // assert.NoError(t, err) 102 // assert.Equal(t, "1", reply.ID) 103 // assert.Equal(t, "username", reply.Name) 104 // 105 // // call GetUser0 106 // ctx = context.WithValue(context.Background(), constant.DUBBOGO_CTX_KEY, map[string]string{ 107 // "X-Proxy-ID": "dubbogo", 108 // "X-Services": url.Path, 109 // "X-Method": "GetUser0", 110 // }) 111 // req = client.NewRequest(url, "GetUser0", []interface{}{"1", nil, "username"}) 112 // reply = &User{} 113 // err = client.Call(ctx, url, req, reply) 114 // assert.NoError(t, err) 115 // assert.Equal(t, "1", reply.ID) 116 // assert.Equal(t, "username", reply.Name) 117 // 118 // // call GetUser1 119 // ctx = context.WithValue(context.Background(), constant.DUBBOGO_CTX_KEY, map[string]string{ 120 // "X-Proxy-ID": "dubbogo", 121 // "X-Services": url.Path, 122 // "X-Method": "GetUser1", 123 // }) 124 // req = client.NewRequest(url, "GetUser1", []interface{}{}) 125 // reply = &User{} 126 // err = client.Call(ctx, url, req, reply) 127 // assert.True(t, strings.Contains(err.Error(), "500 Internal Server Error")) 128 // assert.True(t, strings.Contains(err.Error(), "\\\"result\\\":{},\\\"error\\\":{\\\"code\\\":-32000,\\\"message\\\":\\\"error\\\"}")) 129 // 130 // // call GetUser2 131 // ctx = context.WithValue(context.Background(), constant.DUBBOGO_CTX_KEY, map[string]string{ 132 // "X-Proxy-ID": "dubbogo", 133 // "X-Services": url.Path, 134 // "X-Method": "GetUser2", 135 // }) 136 // req = client.NewRequest(url, "GetUser2", []interface{}{"1", "username"}) 137 // reply1 := []User{} 138 // err = client.Call(ctx, url, req, &reply1) 139 // assert.NoError(t, err) 140 // assert.Equal(t, User{ID: "1", Name: "username"}, reply1[0]) 141 // 142 // // call GetUser3 143 // ctx = context.WithValue(context.Background(), constant.DUBBOGO_CTX_KEY, map[string]string{ 144 // "X-Proxy-ID": "dubbogo", 145 // "X-Services": url.Path, 146 // "X-Method": "GetUser3", 147 // }) 148 // req = client.NewRequest(url, "GetUser3", []interface{}{"1", "username"}) 149 // reply1 = []User{} 150 // err = client.Call(ctx, url, req, &reply1) 151 // assert.NoError(t, err) 152 // assert.Equal(t, User{ID: "1", Name: "username"}, reply1[0]) 153 // 154 // // call GetUser4 155 // ctx = context.WithValue(context.Background(), constant.DUBBOGO_CTX_KEY, map[string]string{ 156 // "X-Proxy-ID": "dubbogo", 157 // "X-Services": url.Path, 158 // "X-Method": "GetUser4", 159 // }) 160 // req = client.NewRequest(url, "GetUser4", []interface{}{0}) 161 // reply = &User{} 162 // err = client.Call(ctx, url, req, reply) 163 // assert.NoError(t, err) 164 // assert.Equal(t, &User{ID: "", Name: ""}, reply) 165 // 166 // ctx = context.WithValue(context.Background(), constant.DUBBOGO_CTX_KEY, map[string]string{ 167 // "X-Proxy-ID": "dubbogo", 168 // "X-Services": url.Path, 169 // "X-Method": "GetUser4", 170 // }) 171 // 172 // span := opentracing.StartSpan("Test-Inject-Tracing-ID") 173 // ctx = opentracing.ContextWithSpan(ctx, span) 174 // 175 // req = client.NewRequest(url, "GetUser4", []interface{}{1}) 176 // reply = &User{} 177 // err = client.Call(ctx, url, req, reply) 178 // assert.NoError(t, err) 179 // assert.Equal(t, &User{ID: "1", Name: ""}, reply) 180 // 181 // // destroy 182 // proto.Destroy() 183 // } 184 func (u *UserProvider) GetUser(ctx context.Context, req []interface{}, rsp *User) error { 185 rsp.ID = req[0].(string) 186 rsp.Name = req[1].(string) 187 return nil 188 } 189 190 func (u *UserProvider) GetUser0(id string, k *User, name string) (User, error) { 191 return User{ID: id, Name: name}, nil 192 } 193 194 func (u *UserProvider) GetUser1() error { 195 return perrors.New("error") 196 } 197 198 func (u *UserProvider) GetUser2(ctx context.Context, req []interface{}, rsp *[]User) error { 199 *rsp = append(*rsp, User{ID: req[0].(string), Name: req[1].(string)}) 200 return nil 201 } 202 203 func (u *UserProvider) GetUser3(ctx context.Context, req []interface{}) ([]User, error) { 204 return []User{{ID: req[0].(string), Name: req[1].(string)}}, nil 205 } 206 207 func (u *UserProvider) GetUser4(id float64) (*User, error) { 208 if id == 0 { 209 return nil, nil 210 } 211 return &User{ID: "1"}, nil 212 } 213 214 func (u *UserProvider) Reference() string { 215 return "UserProvider" 216 }