hi-server/pkg/openinstall/channel_test.go
2026-02-08 18:49:14 -08:00

69 lines
1.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package openinstall
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
// TestChannelParameter 验证 OpenInstall 客户端是否正确传递了 channel 参数
func TestChannelParameter(t *testing.T) {
// 1. 启动 Mock Server
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// 验证请求路径
if r.URL.Path == "/data/sum/growth" {
// 验证 Query 参数
query := r.URL.Query()
channel := query.Get("channel")
// 核心验证点channel 参数必须等于即使的 inviteCode
if channel == "TEST_INVITE_CODE_123" {
w.WriteHeader(http.StatusOK)
// 返回假数据
w.Write([]byte(`{
"code": 0,
"body": [
{"key": "ios", "value": 100},
{"key": "android", "value": 200}
]
}`))
return
}
// 如果 channel 不匹配,返回错误
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(`{"code": 400, "error": "channel mismatch"}`))
return
}
w.WriteHeader(http.StatusNotFound)
}))
defer mockServer.Close()
// 2. 临时修改 apiBaseURL 指向 Mock Server
originalBaseURL := apiBaseURL
apiBaseURL = mockServer.URL
defer func() { apiBaseURL = originalBaseURL }()
// 3. 初始化客户端
client := NewClient("test-api-key")
// 4. 调用接口 (传入测试用的邀请码)
ctx := context.Background()
stats, err := client.GetPlatformDownloads(ctx, "TEST_INVITE_CODE_123")
// 5. 验证结果
assert.NoError(t, err)
assert.NotNil(t, stats)
// 验证数据正确解析 (iOS=100, Android=200, Total=300)
assert.Equal(t, int64(100), stats.IOS, "iOS count should match mock data")
assert.Equal(t, int64(200), stats.Android, "Android count should match mock data")
assert.Equal(t, int64(300), stats.Total, "Total count should match sum of mock data")
t.Logf("Success! Channel parameter 'TEST_INVITE_CODE_123' was correctly sent to server.")
}