github.com/polarismesh/polaris@v1.17.8/test/integrate/i18n_test.go (about)

     1  //go:build integration
     2  // +build integration
     3  
     4  /**
     5   * Tencent is pleased to support the open source community by making Polaris available.
     6   *
     7   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     8   *
     9   * Licensed under the BSD 3-Clause License (the "License");
    10   * you may not use this file except in compliance with the License.
    11   * You may obtain a copy of the License at
    12   *
    13   * https://opensource.org/licenses/BSD-3-Clause
    14   *
    15   * Unless required by applicable law or agreed to in writing, software distributed
    16   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    17   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    18   * specific language governing permissions and limitations under the License.
    19   */
    20  package test
    21  
    22  import (
    23  	"fmt"
    24  	"testing"
    25  
    26  	"github.com/golang/protobuf/jsonpb"
    27  	apimodel "github.com/polarismesh/specification/source/go/api/v1/model"
    28  	apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage"
    29  
    30  	"github.com/polarismesh/polaris/common/utils"
    31  	"github.com/polarismesh/polaris/test/integrate/http"
    32  )
    33  
    34  // TestI18n 测试国际化信息
    35  func TestI18n(t *testing.T) {
    36  	t.Log("test i18n")
    37  	type args struct {
    38  		lang string
    39  		want string
    40  	}
    41  	tests := []args{
    42  		{lang: "zh", want: "命名空间名称非法"},
    43  		{lang: "en", want: "invalid namespace name"},
    44  	}
    45  	for _, item := range tests {
    46  		ret, err := reqCreateIllegalNamespace(item.lang)
    47  		if err != nil {
    48  			t.Fatalf("create namespace fail for i18n test: %s", err.Error())
    49  		}
    50  		if msg := ret.GetInfo().Value; msg != item.want {
    51  			t.Errorf("test i18n by create namespace resp msg = %v, want: %v", msg, item.want)
    52  		}
    53  	}
    54  }
    55  
    56  func reqCreateIllegalNamespace(lang string) (*apiservice.BatchWriteResponse, error) {
    57  	c := http.NewClient(httpserverAddress, httpserverVersion)
    58  	url := fmt.Sprintf("http://%v/naming/%v/namespaces?lang=%s", c.Address, c.Version, lang)
    59  	body, err := http.JSONFromNamespaces([]*apimodel.Namespace{{
    60  		Name:    utils.NewStringValue("+$#@+"),
    61  		Comment: utils.NewStringValue("test"),
    62  		Owners:  utils.NewStringValue("test"),
    63  	}})
    64  	if err != nil {
    65  		return nil, err
    66  	}
    67  	response, err := c.SendRequest("POST", url, body)
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  	ret := &apiservice.BatchWriteResponse{}
    72  	if ierr := jsonpb.Unmarshal(response.Body, ret); ierr != nil {
    73  		return nil, ierr
    74  	}
    75  	return ret, nil
    76  }