github.com/aacfactory/fns@v1.2.86-0.20240310083819-80d667fc0a17/clusters/handler_test.go (about)

     1  /*
     2   * Copyright 2023 Wang Min Xiang
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   * 	http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   */
    17  
    18  package clusters_test
    19  
    20  import (
    21  	"fmt"
    22  	"github.com/aacfactory/avro"
    23  	"github.com/aacfactory/fns/clusters"
    24  	"github.com/aacfactory/fns/commons/avros"
    25  	"github.com/aacfactory/fns/services"
    26  	"testing"
    27  	"time"
    28  )
    29  
    30  func TestRequestBody(t *testing.T) {
    31  	rb := clusters.RequestBody{
    32  		ContextUserValues: nil,
    33  		Params:            avro.MustMarshal("123"),
    34  	}
    35  	p, encodeErr := avro.Marshal(rb)
    36  	if encodeErr != nil {
    37  		t.Error(encodeErr)
    38  		return
    39  	}
    40  	v := clusters.RequestBody{}
    41  	decodeErr := avro.Unmarshal(p, &v)
    42  	if decodeErr != nil {
    43  		t.Error(decodeErr)
    44  		return
    45  	}
    46  	param := avros.RawMessage(rb.Params)
    47  	s := ""
    48  	err := param.Unmarshal(&s)
    49  	if err != nil {
    50  		t.Error(err)
    51  		return
    52  	}
    53  	t.Log(s)
    54  }
    55  
    56  func TestResponseBody(t *testing.T) {
    57  	type Data struct {
    58  		Id  string
    59  		Now time.Time
    60  	}
    61  	p, _ := avro.Marshal([]Data{{
    62  		Id:  "id",
    63  		Now: time.Now(),
    64  	}})
    65  
    66  	rsb := clusters.ResponseBody{
    67  		Succeed:     true,
    68  		Data:        p,
    69  		Attachments: nil,
    70  	}
    71  	rsb.Attachments = append(rsb.Attachments, clusters.Entry{
    72  		Key:   []byte("key"),
    73  		Value: []byte("value"),
    74  	})
    75  
    76  	rp, _ := avro.Marshal(rsb)
    77  	t.Log(len(rp))
    78  	rsb = clusters.ResponseBody{}
    79  	decodeErr := avro.Unmarshal(rp, &rsb)
    80  	if decodeErr != nil {
    81  		t.Error(decodeErr)
    82  		return
    83  	}
    84  	data := make([]Data, 0)
    85  	decodeErr = avro.Unmarshal(rsb.Data, &data)
    86  	if decodeErr != nil {
    87  		t.Error(decodeErr)
    88  		return
    89  	}
    90  	t.Log(data)
    91  	t.Log(string(rsb.Attachments[0].Key), string(rsb.Attachments[0].Value))
    92  	d2, d2Err := services.ValueOfResponse[[]Data](avros.RawMessage(rsb.Data))
    93  	if d2Err != nil {
    94  		t.Error(fmt.Sprintf("%+v", d2Err))
    95  		return
    96  	}
    97  	t.Log(d2)
    98  }