143 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
		
		
			
		
	
	
			143 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
|  | package alicloud | ||
|  | 
 | ||
|  | import ( | ||
|  | 	"encoding/json" | ||
|  | 	"fmt" | ||
|  | 	"testing" | ||
|  | ) | ||
|  | 
 | ||
|  | func TestRealAlicloudAPI(t *testing.T) { | ||
|  | 	// 使用真实的阿里云API配置 | ||
|  | 	host := "https://kzidcardv1.market.alicloudapi.com" | ||
|  | 	appCode := "d55b58829efb41c8aa8e86769cba4844" | ||
|  | 
 | ||
|  | 	service := NewAlicloudService(host, appCode) | ||
|  | 
 | ||
|  | 	// 测试真实的身份证验证 | ||
|  | 	name := "张荣宏" | ||
|  | 	idCard := "45212220000827423X" | ||
|  | 
 | ||
|  | 	fmt.Printf("开始测试阿里云二要素验证API...\n") | ||
|  | 	fmt.Printf("姓名: %s\n", name) | ||
|  | 	fmt.Printf("身份证: %s\n", idCard) | ||
|  | 
 | ||
|  | 	// 构建请求参数 | ||
|  | 	params := map[string]interface{}{ | ||
|  | 		"name":   name, | ||
|  | 		"idcard": idCard, | ||
|  | 	} | ||
|  | 
 | ||
|  | 	// 调用真实API | ||
|  | 	responseBody, err := service.CallAPI("api-mall/api/id_card/check", params) | ||
|  | 	if err != nil { | ||
|  | 		t.Logf("API调用失败: %v", err) | ||
|  | 		fmt.Printf("错误详情: %v\n", err) | ||
|  | 		t.Fail() | ||
|  | 		return | ||
|  | 	} | ||
|  | 
 | ||
|  | 	// 打印原始响应数据 | ||
|  | 	fmt.Printf("API响应成功!\n") | ||
|  | 	fmt.Printf("原始响应数据: %s\n", string(responseBody)) | ||
|  | 
 | ||
|  | 	// 解析完整响应结构 | ||
|  | 	var response struct { | ||
|  | 		Msg     string `json:"msg"` | ||
|  | 		Success bool   `json:"success"` | ||
|  | 		Code    int    `json:"code"` | ||
|  | 		Data    struct { | ||
|  | 			Birthday string `json:"birthday"` | ||
|  | 			Result   int    `json:"result"` | ||
|  | 			Address  string `json:"address"` | ||
|  | 			OrderNo  string `json:"orderNo"` | ||
|  | 			Sex      string `json:"sex"` | ||
|  | 			Desc     string `json:"desc"` | ||
|  | 		} `json:"data"` | ||
|  | 	} | ||
|  | 
 | ||
|  | 	if err := json.Unmarshal(responseBody, &response); err != nil { | ||
|  | 		t.Logf("响应数据解析失败: %v", err) | ||
|  | 		t.Fail() | ||
|  | 		return | ||
|  | 	} | ||
|  | 
 | ||
|  | 	// 检查响应状态 | ||
|  | 	if response.Code != 200 { | ||
|  | 		t.Logf("API返回错误: code=%d, msg=%s", response.Code, response.Msg) | ||
|  | 		t.Fail() | ||
|  | 		return | ||
|  | 	} | ||
|  | 
 | ||
|  | 	idCardData := response.Data | ||
|  | 
 | ||
|  | 	// 打印详细响应结果 | ||
|  | 	fmt.Printf("验证结果: %d\n", idCardData.Result) | ||
|  | 	fmt.Printf("描述: %s\n", idCardData.Desc) | ||
|  | 	fmt.Printf("生日: %s\n", idCardData.Birthday) | ||
|  | 	fmt.Printf("性别: %s\n", idCardData.Sex) | ||
|  | 	fmt.Printf("地址: %s\n", idCardData.Address) | ||
|  | 	fmt.Printf("订单号: %s\n", idCardData.OrderNo) | ||
|  | 
 | ||
|  | 	// 将完整响应转换为JSON并打印 | ||
|  | 	jsonResponse, _ := json.MarshalIndent(idCardData, "", "  ") | ||
|  | 	fmt.Printf("完整响应JSON:\n%s\n", string(jsonResponse)) | ||
|  | 
 | ||
|  | 	// 判断验证结果 | ||
|  | 	if idCardData.Result == 1 { | ||
|  | 		fmt.Printf("验证结果: 通过\n") | ||
|  | 	} else { | ||
|  | 		fmt.Printf("验证结果: 失败\n") | ||
|  | 	} | ||
|  | } | ||
|  | 
 | ||
|  | // TestAlicloudAPIError 测试错误响应 | ||
|  | func TestAlicloudAPIError(t *testing.T) { | ||
|  | 	// 使用真实的阿里云API配置 | ||
|  | 	host := "https://kzidcardv1.market.alicloudapi.com" | ||
|  | 	appCode := "d55b58829efb41c8aa8e86769cba4844" | ||
|  | 
 | ||
|  | 	service := NewAlicloudService(host, appCode) | ||
|  | 
 | ||
|  | 	// 测试无效的身份证号码 | ||
|  | 	name := "张三" | ||
|  | 	invalidIdCard := "123456789" | ||
|  | 
 | ||
|  | 	fmt.Printf("测试错误响应 - 无效身份证号\n") | ||
|  | 	fmt.Printf("姓名: %s\n", name) | ||
|  | 	fmt.Printf("身份证: %s\n", invalidIdCard) | ||
|  | 
 | ||
|  | 	// 构建请求参数 | ||
|  | 	params := map[string]interface{}{ | ||
|  | 		"name":   name, | ||
|  | 		"idcard": invalidIdCard, | ||
|  | 	} | ||
|  | 
 | ||
|  | 	// 调用真实API | ||
|  | 	responseBody, err := service.CallAPI("api-mall/api/id_card/check", params) | ||
|  | 	if err != nil { | ||
|  | 		fmt.Printf("网络请求错误: %v\n", err) | ||
|  | 		return | ||
|  | 	} | ||
|  | 
 | ||
|  | 	// 解析响应 | ||
|  | 	var response struct { | ||
|  | 		Msg  string `json:"msg"` | ||
|  | 		Code int    `json:"code"` | ||
|  | 		Data interface{} `json:"data"` | ||
|  | 	} | ||
|  | 
 | ||
|  | 	if err := json.Unmarshal(responseBody, &response); err != nil { | ||
|  | 		fmt.Printf("响应解析失败: %v\n", err) | ||
|  | 		return | ||
|  | 	} | ||
|  | 
 | ||
|  | 	// 检查是否为错误响应 | ||
|  | 	if response.Code != 200 { | ||
|  | 		fmt.Printf("预期的错误响应: code=%d, msg=%s\n", response.Code, response.Msg) | ||
|  | 		fmt.Printf("错误处理正确: API返回错误状态\n") | ||
|  | 	} else { | ||
|  | 		t.Error("期望返回错误,但实际成功") | ||
|  | 	} | ||
|  | } | ||
|  | 
 | ||
|  |   |