Skip to content

Commit

Permalink
improve test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
yndu13 authored and JacksonTian committed Sep 6, 2024
1 parent 702c377 commit 1645ac2
Show file tree
Hide file tree
Showing 11 changed files with 1,984 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: pip install alibabacloud-tea coverage pytest alibabacloud-tea-util alibabacloud_credentials alibabacloud_openapi_util alibabacloud_gateway_spi alibabacloud_tea_xml httpretty
run: pip install alibabacloud-tea coverage pytest alibabacloud-tea-util alibabacloud_credentials alibabacloud_openapi_util alibabacloud_gateway_spi alibabacloud_tea_xml httpretty alibabacloud_gateway_pop aioresponses
- name: Test with unittest
run: |
coverage run --source="./alibabacloud_tea_openapi" -m pytest tests/test_*
Expand Down
222 changes: 216 additions & 6 deletions golang/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package client

import (
"encoding/json"
"io/ioutil"
"net/http"
"regexp"
"strings"
"testing"
"time"

pop "github.com/alibabacloud-go/alibabacloud-gateway-pop/client"
openapiutil "github.com/alibabacloud-go/openapi-util/service"
util "github.com/alibabacloud-go/tea-utils/v2/service"
"github.com/alibabacloud-go/tea/tea"
Expand Down Expand Up @@ -61,6 +63,11 @@ func (mock *mockHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
", \"Description\":\"error description\", \"accessDeniedDetail\":{\"test\": 0}}"
w.WriteHeader(400)
w.Write([]byte(responseBody))
case "serverError":
responseBody = "{\"Code\":\"error code\", \"Message\":\"error message\", \"RequestId\":\"A45EE076-334D-5012-9746-A8F828D20FD4\"" +
", \"Description\":\"error description\", \"accessDeniedDetail\":{\"test\": 0}}"
w.WriteHeader(500)
w.Write([]byte(responseBody))
default:
w.WriteHeader(200)
w.Write([]byte(responseBody))
Expand Down Expand Up @@ -102,6 +109,31 @@ func TestConfig(t *testing.T) {
Cert: tea.String("config.cert"),
Ca: tea.String("config.ca"),
}
config.SetEndpoint("config.endpoint")
config.SetEndpointType("public")
config.SetNetwork("config.network")
config.SetSuffix("config.suffix")
config.SetProtocol("config.protocol")
config.SetMethod("config.method")
config.SetRegionId("config.regionId")
config.SetUserAgent("config.userAgent")
config.SetReadTimeout(3000)
config.SetConnectTimeout(3000)
config.SetHttpProxy("config.httpProxy")
config.SetHttpsProxy("config.httpsProxy")
config.SetNoProxy("config.noProxy")
config.SetSocks5Proxy("config.socks5Proxy")
config.SetSocks5NetWork("config.socks5NetWork")
config.SetMaxIdleConns(128)
config.SetSignatureVersion("config.signatureVersion")
config.SetSignatureAlgorithm("config.signatureAlgorithm")
config.SetGlobalParameters(globalParameters)
config.SetKey("config.key")
config.SetCert("config.cert")
config.SetCa("config.ca")
config.SetOpenPlatformEndpoint("openPlatform.aliyuncs.com")
config.SetDisableHttp2(true)

creConfig := &credential.Config{
AccessKeyId: tea.String("accessKeyId"),
AccessKeySecret: tea.String("accessKeySecret"),
Expand All @@ -111,7 +143,7 @@ func TestConfig(t *testing.T) {
cred, _err := credential.NewCredential(creConfig)
tea_util.AssertNil(t, _err)

config.Credential = cred
config.SetCredential(cred)
config.String()
config.GoString()
client, _err := NewClient(nil)
Expand Down Expand Up @@ -151,10 +183,10 @@ func TestConfig(t *testing.T) {
ty, _ = client.GetType()
tea_util.AssertEqual(t, "bearer", tea.StringValue(ty))

config.AccessKeyId = tea.String("ak")
config.AccessKeySecret = tea.String("secret")
config.SecurityToken = tea.String("token")
config.Type = tea.String("sts")
config.SetAccessKeyId("ak")
config.SetAccessKeySecret("secret")
config.SetSecurityToken("token")
config.SetType("sts")
client, _err = NewClient(config)
tea_util.AssertNil(t, _err)
ak, _ = client.GetAccessKeyId()
Expand All @@ -165,6 +197,25 @@ func TestConfig(t *testing.T) {
tea_util.AssertEqual(t, "token", tea.StringValue(token))
ty, _ = client.GetType()
tea_util.AssertEqual(t, "sts", tea.StringValue(ty))

config.SetType("bearer")
config.SetBearerToken("token")
config.SetAccessKeyId("")
config.SetAccessKeySecret("")
config.SetSecurityToken("")
client, _err = NewClient(config)
tea_util.AssertNil(t, _err)
ak, _ = client.GetAccessKeyId()
tea_util.AssertEqual(t, "", tea.StringValue(ak))
sk, _ = client.GetAccessKeySecret()
tea_util.AssertEqual(t, "", tea.StringValue(sk))
token, _ = client.GetSecurityToken()
tea_util.AssertEqual(t, "", tea.StringValue(token))
token, _ = client.GetBearerToken()
tea_util.AssertEqual(t, "token", tea.StringValue(token))
ty, _ = client.GetType()
tea_util.AssertEqual(t, "bearer", tea.StringValue(ty))

config.AccessKeyId = tea.String("ak")
config.AccessKeySecret = tea.String("secret")
config.Type = tea.String("access_key")
Expand Down Expand Up @@ -204,10 +255,77 @@ func TestConfig(t *testing.T) {
tea_util.AssertEqual(t, "config.key", tea.StringValue(client.Key))
tea_util.AssertEqual(t, "config.cert", tea.StringValue(client.Cert))
tea_util.AssertEqual(t, "config.ca", tea.StringValue(client.Ca))
tea_util.AssertEqual(t, true, tea.BoolValue(client.DisableHttp2))

globalParameters.SetHeaders(map[string]*string{
"global-key": tea.String("test"),
})
globalParameters.SetQueries(map[string]*string{
"global-query": tea.String("test"),
})
tea_util.AssertEqual(t, "test", tea.StringValue(client.GlobalParameters.Headers["global-key"]))
tea_util.AssertEqual(t, "test", tea.StringValue(client.GlobalParameters.Queries["global-query"]))
_err = client.CheckConfig(config)
tea_util.AssertNil(t, _err)
}

func TestOpenApiRequest(t *testing.T) {
query := map[string]*string{
"key": tea.String("value"),
}
body := map[string]interface{}{
"key": tea.String("value"),
}
headers := map[string]*string{
"key": tea.String("value"),
}
hostMap := map[string]*string{
"key": tea.String("value"),
}
req := &OpenApiRequest{}
req.SetQuery(query)
req.SetBody(body)
req.SetHeaders(headers)
req.SetHostMap(hostMap)
req.SetEndpointOverride("test")
req.SetStream(strings.NewReader("test"))
req.GoString()
req.String()

tea_util.AssertEqual(t, "value", tea.StringValue(req.Headers["key"]))
tea_util.AssertEqual(t, "value", tea.StringValue(req.Query["key"]))
tea_util.AssertEqual(t, body, req.Body)
tea_util.AssertEqual(t, "test", tea.StringValue(req.EndpointOverride))
tea_util.AssertEqual(t, "value", tea.StringValue(req.HostMap["key"]))
byt, _ := ioutil.ReadAll(req.Stream)
tea_util.AssertEqual(t, "test", string(byt))
}

func TestParams(t *testing.T) {
params := &Params{}
params.SetAction("test")
params.SetVersion("test")
params.SetProtocol("test")
params.SetPathname("test")
params.SetMethod("test")
params.SetAuthType("test")
params.SetBodyType("test")
params.SetReqBodyType("test")
params.SetStyle("test")
params.GoString()
params.String()

tea_util.AssertEqual(t, "test", tea.StringValue(params.Action))
tea_util.AssertEqual(t, "test", tea.StringValue(params.Version))
tea_util.AssertEqual(t, "test", tea.StringValue(params.Protocol))
tea_util.AssertEqual(t, "test", tea.StringValue(params.Pathname))
tea_util.AssertEqual(t, "test", tea.StringValue(params.Method))
tea_util.AssertEqual(t, "test", tea.StringValue(params.AuthType))
tea_util.AssertEqual(t, "test", tea.StringValue(params.BodyType))
tea_util.AssertEqual(t, "test", tea.StringValue(params.ReqBodyType))
tea_util.AssertEqual(t, "test", tea.StringValue(params.Style))
}

func CreateConfig() (_result *Config) {
globalParameters := &GlobalParameters{
Headers: map[string]*string{
Expand Down Expand Up @@ -1068,7 +1186,7 @@ func TestRequestBodyType(t *testing.T) {
config := CreateConfig()
runtime := CreateRuntimeOptions()
config.Protocol = tea.String("HTTP")
config.Endpoint = tea.String("127.0.0.1:9009")
config.Endpoint = tea.String("127.0.0.1:9010")
client, _err := NewClient(config)
tea_util.AssertNil(t, _err)
// formData
Expand Down Expand Up @@ -1302,3 +1420,95 @@ func TestResponseBodyTypeROA(t *testing.T) {
accessDeniedDetail, _ := err.AccessDeniedDetail["test"].(int)
tea_util.AssertEqual(t, 0, accessDeniedDetail)
}

func TestRetryWithError(t *testing.T) {
mux := http.NewServeMux()
mux.Handle("/", &mockHandler{content: "serverError"})
mux.Handle("/test", &mockHandler{content: "serverError"})
var server *http.Server
server = &http.Server{
Addr: ":9013",
WriteTimeout: time.Second * 4,
Handler: mux,
}
go server.ListenAndServe()
config := CreateConfig()
runtime := CreateRuntimeOptions()
runtime.Autoretry = tea.Bool(true)
runtime.MaxAttempts = tea.Int(3)
runtime.BackoffPolicy = tea.String("fix")
runtime.BackoffPeriod = tea.Int(1)

config.Protocol = tea.String("HTTP")
config.SignatureAlgorithm = tea.String("v2")
config.Endpoint = tea.String("127.0.0.1:9013")
client, _err := NewClient(config)
tea_util.AssertNil(t, _err)
request := CreateOpenApiRequest()

params := &Params{
Action: tea.String("TestAPI"),
Version: tea.String("2022-06-01"),
Protocol: tea.String("HTTPS"),
Pathname: tea.String("/"),
Method: tea.String("POST"),
AuthType: tea.String("Anonymous"),
Style: tea.String("RPC"),
ReqBodyType: tea.String("formData"),
BodyType: tea.String("json"),
}
_, _err = client.CallApi(params, request, runtime)
tea_util.AssertNotNil(t, _err)
err := _err.(*tea.SDKError)
tea_util.AssertEqual(t, "code: 500, error message request id: A45EE076-334D-5012-9746-A8F828D20FD4", tea.StringValue(err.Message))

params = &Params{
Action: tea.String("TestAPI"),
Version: tea.String("2022-06-01"),
Protocol: tea.String("HTTPS"),
Pathname: tea.String("/test"),
Method: tea.String("POST"),
AuthType: tea.String("Anonymous"),
Style: tea.String("ROA"),
ReqBodyType: tea.String("formData"),
BodyType: tea.String("json"),
}
_, _err = client.CallApi(params, request, runtime)
tea_util.AssertNotNil(t, _err)
err = _err.(*tea.SDKError)
tea_util.AssertEqual(t, "code: 500, error message request id: A45EE076-334D-5012-9746-A8F828D20FD4", tea.StringValue(err.Message))

params = &Params{
Action: tea.String("TestAPI"),
Version: tea.String("2022-06-01"),
Protocol: tea.String("HTTPS"),
Pathname: tea.String("/test"),
Method: tea.String("POST"),
AuthType: tea.String("Anonymous"),
Style: tea.String("ROA"),
ReqBodyType: tea.String("json"),
BodyType: tea.String("json"),
}
_, _err = client.CallApi(params, request, runtime)
tea_util.AssertNotNil(t, _err)
err = _err.(*tea.SDKError)
tea_util.AssertEqual(t, "code: 500, error message request id: A45EE076-334D-5012-9746-A8F828D20FD4", tea.StringValue(err.Message))

params = &Params{
Action: tea.String("TestAPI"),
Version: tea.String("2022-06-01"),
Protocol: tea.String("HTTPS"),
Pathname: tea.String("/test"),
Method: tea.String("POST"),
AuthType: tea.String("Anonymous"),
Style: tea.String("RPC"),
ReqBodyType: tea.String("json"),
BodyType: tea.String("json"),
}
client.ProductId = tea.String("test")
gatewayClient, _err := pop.NewClient()
tea_util.AssertNil(t, _err)
client.SetGatewayClient(gatewayClient)
_, _err = client.Execute(params, request, runtime)
tea_util.AssertNotNil(t, _err)
}
4 changes: 3 additions & 1 deletion golang/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ module github.com/alibabacloud-go/darabonba-openapi/v2
go 1.14

require (
github.com/alibabacloud-go/alibabacloud-gateway-pop v0.0.6
github.com/alibabacloud-go/alibabacloud-gateway-spi v0.0.4
github.com/alibabacloud-go/openapi-util v0.1.0
github.com/alibabacloud-go/tea v1.2.1
github.com/alibabacloud-go/tea v1.2.2
github.com/alibabacloud-go/tea-utils/v2 v2.0.6
github.com/alibabacloud-go/tea-xml v1.1.3
github.com/aliyun/credentials-go v1.3.1
github.com/clbanning/mxj/v2 v2.5.5 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
)
18 changes: 15 additions & 3 deletions java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.4</version>
<executions>
<execution>
<!-- <execution>
<id>default-instrument</id>
<goals>
<goal>instrument</goal>
Expand All @@ -156,8 +156,8 @@
<goals>
<goal>restore-instrumented-classes</goal>
</goals>
</execution>
<execution>
</execution> -->
<!-- <execution>
<id>report</id>
<phase>test</phase>
<goals>
Expand All @@ -166,6 +166,18 @@
<configuration>
<dataFile>${project.build.directory}/coverage.exec</dataFile>
</configuration>
</execution> -->
<execution>
<goals>
<goal>prepare-agent</goal> <!-- 用于在编译时启动JaCoCo代理 -->
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal> <!-- 生成HTML格式的报告 -->
</goals>
</execution>
</executions>
</plugin>
Expand Down
Loading

0 comments on commit 1645ac2

Please sign in to comment.