dubbo.apache.org/dubbo-go/v3@v3.1.1/protocol/dubbo/impl/codec_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 impl
    19  
    20  import (
    21  	"testing"
    22  	"time"
    23  )
    24  
    25  import (
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  import (
    30  	"dubbo.apache.org/dubbo-go/v3/common/constant"
    31  )
    32  
    33  func TestDubboPackage_MarshalAndUnmarshal(t *testing.T) {
    34  	pkg := NewDubboPackage(nil)
    35  	pkg.Body = []interface{}{"a"}
    36  	pkg.Header.Type = PackageHeartbeat
    37  	pkg.Header.SerialID = constant.SHessian2
    38  	pkg.Header.ID = 10086
    39  	pkg.SetSerializer(HessianSerializer{})
    40  
    41  	// heartbeat
    42  	data, err := pkg.Marshal()
    43  	assert.NoError(t, err)
    44  
    45  	pkgres := NewDubboPackage(data)
    46  	pkgres.SetSerializer(HessianSerializer{})
    47  
    48  	pkgres.Body = []interface{}{}
    49  	err = pkgres.Unmarshal()
    50  	assert.NoError(t, err)
    51  	assert.Equal(t, PackageHeartbeat|PackageRequest|PackageRequest_TwoWay, pkgres.Header.Type)
    52  	assert.Equal(t, constant.SHessian2, pkgres.Header.SerialID)
    53  	assert.Equal(t, int64(10086), pkgres.Header.ID)
    54  	assert.Equal(t, 0, len(pkgres.Body.([]interface{})))
    55  
    56  	// request
    57  	pkg.Header.Type = PackageRequest
    58  	pkg.Service.Interface = "Service"
    59  	pkg.Service.Path = "path"
    60  	pkg.Service.Version = "2.6"
    61  	pkg.Service.Method = "Method"
    62  	pkg.Service.Timeout = time.Second
    63  	data, err = pkg.Marshal()
    64  	assert.NoError(t, err)
    65  
    66  	pkgres = NewDubboPackage(data)
    67  	pkgres.SetSerializer(HessianSerializer{})
    68  	pkgres.Body = make([]interface{}, 7)
    69  	err = pkgres.Unmarshal()
    70  	reassembleBody := pkgres.GetBody().(map[string]interface{})
    71  	assert.NoError(t, err)
    72  	assert.Equal(t, PackageRequest, pkgres.Header.Type)
    73  	assert.Equal(t, constant.SHessian2, pkgres.Header.SerialID)
    74  	assert.Equal(t, int64(10086), pkgres.Header.ID)
    75  	assert.Equal(t, "2.0.2", reassembleBody["dubboVersion"].(string))
    76  	assert.Equal(t, "path", pkgres.Service.Path)
    77  	assert.Equal(t, "2.6", pkgres.Service.Version)
    78  	assert.Equal(t, "Method", pkgres.Service.Method)
    79  	assert.Equal(t, "Ljava/lang/String;", reassembleBody["argsTypes"].(string))
    80  	assert.Equal(t, []interface{}{"a"}, reassembleBody["args"])
    81  	tmpData := map[string]interface{}{
    82  		"dubbo":     "2.0.2",
    83  		"interface": "Service",
    84  		"path":      "path",
    85  		"timeout":   "1000",
    86  		"version":   "2.6",
    87  	}
    88  	assert.Equal(t, tmpData, reassembleBody["attachments"])
    89  }