postman测试salesforce接口

好的,让我教你怎么用 Postman 测试接口。以下是详细步骤:

📌 第一步:设置认证

1. 获取访问令牌 (Access Token)

POST https://login.salesforce.com/services/oauth2/token
Content-Type: application/x-www-form-urlencoded

grant_type=password
&client_id=你的Consumer Key
&client_secret=你的Consumer Secret
&username=你的用户名
&password=你的密码+安全令牌

示例:

username: yourname@example.com
password: yourpasswordyoursecuritytoken

2. 获取响应

你会得到类似这样的响应:

{
    "access_token": "00Dxxxxxxxxxxxxxx!xxxxxxxx",
    "instance_url": "https://your-domain.my.salesforce.com",
    "id": "https://login.salesforce.com/id/00Dxxxxxxxxxxxxxx/005xxxxxxxxxxxxxx",
    "token_type": "Bearer",
    "issued_at": "1672531200",
    "signature": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}

📌 第二步:配置 Postman

1. 设置全局变量

在 Postman 的 Environment 中设置:

baseUrl: https://your-domain.my.salesforce.com
access_token: 00Dxxxxxxxxxxxxxx!xxxxxxxx

2. 设置 Headers

Authorization: Bearer {{access_token}}
Content-Type: application/json
Accept: application/json

📌 第三步:测试你的接口

假设你的接口路径是:/services/apexrest/psp/api/v1/*

示例 1:测试患者注册接口

POST {{baseUrl}}/services/apexrest/psp/api/v1/insert

Body (JSON):

{
    "requestId": "test_001",
    "entityType": "Patient",
    "openId": "test_openid_001",
    "customData": {
        "patientData": {
            "name": "张三",
            "phone": "13800138000",
            "birthDate": "1990-01-01"
        }
    }
}

示例 2:测试用药提醒接口

POST {{baseUrl}}/services/apexrest/psp/api/v1/insert
{
    "requestId": "test_002",
    "entityType": "SyncReminder",
    "openId": "test_openid_002",
    "customData": {
        "reminderData": {
            "carePlanId": "a0x000000000001",
            "activitys": [
                {
                    "carePlanActivityName": "降压药",
                    "frequency": "Daily",
                    "reminderTime": "08:00"
                }
            ]
        }
    }
}

📌 第四步:检查响应

成功响应示例:

{
    "statusCode": 200,
    "message": "请求处理完成",
    "results": [
        {
            "requestId": "test_001",
            "status": "Success",
            "message": "患者注册成功",
            "customData": {
                "patientId": "001000000000001AAA"
            }
        }
    ]
}

错误响应示例:

{
    "statusCode": 400,
    "message": "请求处理失败",
    "results": [
        {
            "requestId": "test_001",
            "status": "Fail",
            "message": "参数校验失败: openId不能为空"
        }
    ]
}

📌 第五步:测试不同的场景

场景 1:参数缺失

{
    "requestId": "test_003",
    "entityType": "Patient",
    // 故意不传 openId
    "customData": {}
}

场景 2:无效数据

{
    "requestId": "test_004",
    "entityType": "Patient",
    "openId": "test_openid_004",
    "customData": {
        "patientData": {
            "phone": "123"  // 无效的手机号
        }
    }
}

场景 3:批量请求

{
    "requestId": "batch_001",
    "entityType": "Batch",
    "items": [
        {
            "entityType": "Patient",
            "openId": "batch_openid_1",
            "customData": {}
        },
        {
            "entityType": "SyncReminder",
            "openId": "batch_openid_1",
            "customData": {}
        }
    ]
}

📌 第六步:查看调试信息

1. 检查 Salesforce 日志

  • 打开 Developer Console
  • 点击 Debug > Open Execute Anonymous Window
  • 查看系统调试日志

2. Postman Console

  • 在 Postman 底部点击 Console
  • 查看请求和响应的详细信息

📌 第七步:自动化测试脚本

你可以在 Postman 中创建 Tests 选项卡:

// 验证响应状态
pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

// 验证响应结构
pm.test("Response has correct structure", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData).to.have.property('statusCode');
    pm.expect(jsonData).to.have.property('message');
    pm.expect(jsonData).to.have.property('results');
});

// 验证业务逻辑
pm.test("Patient registration successful", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.results[0].status).to.equal('Success');
    pm.expect(jsonData.results[0]).to.have.property('customData');
});

📌 第八步:创建测试集合

在 Postman 中:

  1. New Collection → 命名为 "PSP API Tests"
  2. 创建文件夹:Insert APIs、Update APIs、Query APIs
  3. 为每个接口创建请求
  4. 使用 Runner 批量运行所有测试

📌 快速调试技巧

1. 获取接口路径

如果你不知道接口路径:

GET {{baseUrl}}/services/apexrest

这会列出所有 REST 资源。

2. 查看接口定义

检查 Apex 类中的注解:

@RestResource(urlMapping='/psp/api/v1/*')
global class psp_GenericApiService {
    // ...
}

3. 临时调试代码

在 Apex 类中添加调试:

System.debug('=== 请求参数 ===');
System.debug(JSON.serializePretty(req));

📌 常见问题排查

问题 原因 解决方法
401 Unauthorized Token 过期 重新获取 Token
404 Not Found 接口路径错误 检查 urlMapping
400 Bad Request 参数格式错误 检查 JSON 格式
500 Internal Error Apex 代码异常 查看系统日志

📌 下一步建议

  1. 先测试简单的接口:如患者查询
  2. 逐步增加复杂度:从 insert 到 update
  3. 记录测试用例:保存成功的请求作为测试用例
  4. 编写断言:验证业务逻辑正确性

需要我帮你写具体的测试用例吗?或者你现在遇到了什么问题?

posted @ 2025-12-24 00:15  快乐星猫i  阅读(0)  评论(0)    收藏  举报