github.com/polarismesh/polaris@v1.17.8/apiserver/httpserver/utils/handler_test.go (about)

     1  /**
     2   * Tencent is pleased to support the open source community by making Polaris available.
     3   *
     4   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     5   *
     6   * Licensed under the BSD 3-Clause License (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * https://opensource.org/licenses/BSD-3-Clause
    11   *
    12   * Unless required by applicable law or agreed to in writing, software distributed
    13   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    14   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    15   * specific language governing permissions and limitations under the License.
    16   */
    17  
    18  package utils
    19  
    20  import (
    21  	"fmt"
    22  	"net/http"
    23  	"net/http/httptest"
    24  	"strings"
    25  	"testing"
    26  
    27  	"github.com/emicklei/go-restful/v3"
    28  	"github.com/golang/protobuf/ptypes/wrappers"
    29  	apimodel "github.com/polarismesh/specification/source/go/api/v1/model"
    30  
    31  	"github.com/polarismesh/polaris/apiserver/httpserver/i18n"
    32  	api "github.com/polarismesh/polaris/common/api/v1"
    33  	"github.com/polarismesh/polaris/common/utils"
    34  )
    35  
    36  func init() {
    37  	i18n.LoadI18nMessageFile("../../../release/conf/i18n/en.toml")
    38  	i18n.LoadI18nMessageFile("../../../release/conf/i18n/zh.toml")
    39  }
    40  
    41  func Test_i18n(t *testing.T) {
    42  	type args struct {
    43  		hlang string // lang in header
    44  		qlang string // lang in query
    45  		rMsg  string // 实际响应体的resp.info
    46  		hMsg  string // resp header 的msg
    47  		wMsg  string // 期望的msg
    48  	}
    49  	code, codeEnMsg, codeZhMsg := uint32(200000), "execute success", "执行成功"
    50  
    51  	testCases := []args{
    52  		// 不支持的语言, 默认英语
    53  		{qlang: "ja", hlang: "ja", hMsg: codeEnMsg, rMsg: codeEnMsg, wMsg: codeEnMsg},
    54  		// 优先处理query指定, 否则按照header走
    55  		{qlang: "en", hlang: "zh", hMsg: codeEnMsg, rMsg: codeEnMsg, wMsg: codeEnMsg},
    56  		{qlang: "", hlang: "zh", hMsg: codeEnMsg, rMsg: codeEnMsg, wMsg: codeZhMsg},
    57  		{qlang: "zh", hlang: "en", hMsg: codeEnMsg, rMsg: codeEnMsg, wMsg: codeZhMsg},
    58  		{qlang: "", hlang: "", hMsg: codeEnMsg, rMsg: codeEnMsg, wMsg: codeEnMsg},
    59  		// 当header 与 resp.info 不一致, 不翻译
    60  		{qlang: "", hlang: "", hMsg: codeEnMsg, rMsg: "another msg", wMsg: "another msg"},
    61  		{qlang: "zh", hlang: "en", hMsg: codeEnMsg, rMsg: "another msg", wMsg: "another msg"},
    62  		{qlang: "", hlang: "en", hMsg: codeEnMsg, rMsg: "another msg", wMsg: "another msg"},
    63  	}
    64  	for _, item := range testCases {
    65  		h := Handler{}
    66  		h.Request = restful.NewRequest(&http.Request{
    67  			Header: map[string][]string{"Accept-Language": {item.hlang}},
    68  			Form:   map[string][]string{"lang": {item.qlang}},
    69  		})
    70  		h.Response = restful.NewResponse(httptest.NewRecorder())
    71  		h.Response.AddHeader(utils.PolarisMessage, item.hMsg)
    72  		resp := api.NewResponse(apimodel.Code(code))
    73  		resp.Info = &wrappers.StringValue{Value: item.rMsg}
    74  		if msg := h.i18nAction(resp).GetInfo().Value; msg != item.wMsg {
    75  			t.Errorf("handler.i18nAction() = %v, want %v", msg, item.wMsg)
    76  		}
    77  	}
    78  }
    79  
    80  func Test_ParseJsonBody(t *testing.T) {
    81  	type TestJsonObject struct {
    82  		Text string `json:"text"`
    83  	}
    84  
    85  	expectText := "this is a test"
    86  
    87  	httpReq, _ := http.NewRequest(
    88  		http.MethodPost,
    89  		"http://example.com",
    90  		strings.NewReader(fmt.Sprintf("{\"text\": \"%s\"}", expectText)))
    91  	req := restful.NewRequest(httpReq)
    92  
    93  	testResult := TestJsonObject{}
    94  	err := ParseJsonBody(req, &testResult)
    95  	if err != nil {
    96  		t.Errorf("ParseJsonBody err %v, want %v", err, expectText)
    97  	}
    98  	if testResult.Text != expectText {
    99  		t.Errorf("ParseJsonBody = %v, want %v", testResult.Text, expectText)
   100  	}
   101  
   102  }