This commit is contained in:
@@ -1,68 +0,0 @@
|
||||
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.")
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
package openinstall
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestClient_GetPlatformDownloads_WithChannel(t *testing.T) {
|
||||
// Mock Server
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// Verify URL parameters
|
||||
assert.Equal(t, "GET", r.Method)
|
||||
assert.Equal(t, "/data/sum/growth", r.URL.Path)
|
||||
assert.Equal(t, "test-api-key", r.URL.Query().Get("apiKey"))
|
||||
assert.Equal(t, "test-channel", r.URL.Query().Get("channel")) // Verify channel is passed
|
||||
assert.Equal(t, "total", r.URL.Query().Get("sumBy"))
|
||||
assert.Equal(t, "0", r.URL.Query().Get("excludeDuplication"))
|
||||
|
||||
// Return mock response
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(`{
|
||||
"code": 0,
|
||||
"body": [
|
||||
{"key": "ios", "value": 10},
|
||||
{"key": "android", "value": 20}
|
||||
]
|
||||
}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
// Redirect base URL to mock server (This requires modifying the constant in real code,
|
||||
// but for this test script we can just verify the logic or make the URL configurable.
|
||||
// Since apiBaseURL is a constant, we cannot change it.
|
||||
// However, this test demonstrates the logic we implemented.
|
||||
// For actual running, we might need to inject the URL or make it a variable.)
|
||||
|
||||
// NOTE: Since apiBaseURL is constant in standard Go we can't patch it easily without unsafe or changing code.
|
||||
// But `getDeviceDistribution` constructs the URL using `apiBaseURL`.
|
||||
// For the sake of this example, we assume we can test the parameter construction logic
|
||||
// or we would need to refactor `apiBaseURL` to be a field in `Client`.
|
||||
|
||||
// Since I cannot change the constant easily to point to localhost in the compiled package
|
||||
// without refactoring, I will provide a test that *would* work if we refactored,
|
||||
// OR I can make the test just run against the real API but that requires a key.
|
||||
|
||||
// Plan B: Create a test that instantiates the client and checks the URL construction if we extracted that method,
|
||||
// but we didn't.
|
||||
|
||||
// Let's refactor Client to allow base URL injection for testing?
|
||||
// Or just provide a shell script for the user to run against real env provided they have keys.
|
||||
// The user asked for a "Test Script", commonly meaning a shell script to run the API.
|
||||
|
||||
t.Log("This is a structural test example. To fully unit test HTTP requests with constants, refactoring is recommended.")
|
||||
}
|
||||
Reference in New Issue
Block a user