Newer
Older
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package jsonrpc
import (
"context"
"strings"
"testing"
"time"
)
import (
"github.com/stretchr/testify/assert"
)
import (

AlexStocks
committed
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/common/constant"
"github.com/apache/dubbo-go/protocol"
)
type (
User struct {
Id string `json:"id"`
Name string `json:"name"`
}
UserProvider struct {
user map[string]User
}
)
func TestHTTPClient_Call(t *testing.T) {
methods, err := common.ServiceMap.Register("jsonrpc", &UserProvider{})
assert.NoError(t, err)
assert.Equal(t, "GetUser,GetUser0,GetUser1,GetUser2,GetUser3", methods)
url, err := common.NewURL(context.Background(), "jsonrpc://127.0.0.1:20001/com.ikurento.user.UserProvider?anyhost=true&"+
"application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&"+
"environment=dev&interface=com.ikurento.user.UserProvider&ip=192.168.56.1&methods=GetUser%2C&"+
"module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&"+
"side=provider&timeout=3000×tamp=1556509797245")
assert.NoError(t, err)
proto.Export(protocol.NewBaseInvoker(url))
client := NewHTTPClient(&HTTPOptions{})
// call GetUser
ctx := context.WithValue(context.Background(), constant.DUBBOGO_CTX_KEY, map[string]string{
"X-Proxy-Id": "dubbogo",
"X-Services": url.Path,
"X-Method": "GetUser",
})
req := client.NewRequest(url, "GetUser", []interface{}{"1", "username"})
reply := &User{}
err = client.Call(ctx, url, req, reply)
assert.NoError(t, err)
assert.Equal(t, "1", reply.Id)
assert.Equal(t, "username", reply.Name)
ctx = context.WithValue(context.Background(), constant.DUBBOGO_CTX_KEY, map[string]string{
"X-Proxy-Id": "dubbogo",
"X-Services": url.Path,
"X-Method": "GetUser",
})
req = client.NewRequest(url, "GetUser0", []interface{}{"1", "username"})
reply = &User{}
err = client.Call(ctx, url, req, reply)
assert.NoError(t, err)
assert.Equal(t, "1", reply.Id)
assert.Equal(t, "username", reply.Name)
// call GetUser1
ctx = context.WithValue(context.Background(), constant.DUBBOGO_CTX_KEY, map[string]string{
"X-Proxy-Id": "dubbogo",
"X-Services": url.Path,
"X-Method": "GetUser1",
})
req = client.NewRequest(url, "GetUser1", []interface{}{""})
reply = &User{}
err = client.Call(ctx, url, req, reply)
assert.True(t, strings.Contains(err.Error(), "500 Internal Server Error"))
assert.True(t, strings.Contains(err.Error(), "\\\"result\\\":{},\\\"error\\\":{\\\"code\\\":-32000,\\\"message\\\":\\\"error\\\"}"))
// call GetUser2
ctx = context.WithValue(context.Background(), constant.DUBBOGO_CTX_KEY, map[string]string{
"X-Proxy-Id": "dubbogo",
"X-Services": url.Path,
"X-Method": "GetUser",
})
req = client.NewRequest(url, "GetUser2", []interface{}{"1", "username"})
reply1 := []User{}
err = client.Call(ctx, url, req, &reply1)
assert.NoError(t, err)
assert.Equal(t, User{Id: "1", Name: "username"}, reply1[0])
// call GetUser3
ctx = context.WithValue(context.Background(), constant.DUBBOGO_CTX_KEY, map[string]string{
"X-Proxy-Id": "dubbogo",
"X-Services": url.Path,
"X-Method": "GetUser",
})
req = client.NewRequest(url, "GetUser3", []interface{}{"1", "username"})
reply1 = []User{}
err = client.Call(ctx, url, req, &reply1)
assert.NoError(t, err)
assert.Equal(t, User{Id: "1", Name: "username"}, reply1[0])
// destroy
proto.Destroy()
}
func (u *UserProvider) GetUser(ctx context.Context, req []interface{}, rsp *User) error {
rsp.Id = req[0].(string)
rsp.Name = req[1].(string)
return nil
}
func (u *UserProvider) GetUser0(id string, name string) (User, error) {
return User{Id: id, Name: name}, nil
func (u *UserProvider) GetUser1(ctx context.Context, req []interface{}, rsp *User) error {
func (u *UserProvider) GetUser2(ctx context.Context, req []interface{}, rsp *[]User) error {
*rsp = append(*rsp, User{Id: req[0].(string), Name: req[1].(string)})
return nil
}
func (u *UserProvider) GetUser3(ctx context.Context, req []interface{}) ([]User, error) {
return []User{{Id: req[0].(string), Name: req[1].(string)}}, nil
}
func (u *UserProvider) Service() string {
return "com.ikurento.user.UserProvider"
}
func (u *UserProvider) Version() string {
return ""
}