Skip to content

🌐 CoAP 协议测试指南 ​

📖 概述 ​

CoAP 协议支持为 Ryze 测试框架提供了 CoAP 资源请求的测试能力,适用于 IoT 受限设备通信场景的自动化测试。支持 GET/POST/PUT/DELETE 四种方法,以及 Observe 资源观察功能。

📊 配置项参考表 ​

CoAP 请求配置 ​

配置项类型默认值必需描述
uriString-✅CoAP 资源 URI
methodStringGET❌请求方法 (GET/POST/PUT/DELETE)
content_formatStringapplication/json❌内容格式
payloadString-❌请求体
confirmablebooleantrue❌是否使用 CON(确认)消息
observebooleanfalse❌是否启用观察模式
timeoutint10000❌超时时间(毫秒)
acceptString-❌期望响应格式
block_sizeint-❌块传输大小
dtls_enabledbooleanfalse❌启用 DTLS 安全传输

配置优先级: 取样器配置 > CoAP 默认配置

默认端口: CoAP 端口 5683,DTLS 安全端口 5684

🚀 依赖引入 ​

xml

<dependency>
    <groupId>io.github.xiaomisum</groupId>
    <artifactId>ryze-coap</artifactId>
    <version>${version}</version>
</dependency>

🏃 快速开始 ​

1. 配置 CoAP 默认参数 ​

yaml
testclass: coap
ref_name: coap_default
config:
  uri: 'coap://192.168.1.100:5683'
  confirmable: true
  timeout: 10000
  content_format: 'application/json'

2. 发送 GET 请求 ​

yaml
title: 获取设备温度
testclass: coap
config:
  uri: 'coap://192.168.1.100:5683/sensor/temperature'
  method: GET
  accept: 'application/json'

3. 发送 POST 请求 ​

yaml
title: 注册新设备
testclass: coap
config:
  uri: 'coap://192.168.1.100:5683/devices'
  method: POST
  payload: '{"deviceId":"sensor-001","type":"temperature"}'

⚙️ 配置元件 ​

CoAP 默认配置 ​

yaml
testclass: coap
ref_name: coap_default
config:
  uri: 'coap://192.168.1.100:5683'
  confirmable: true
  timeout: 10000
  content_format: 'application/json'

DTLS 安全配置 ​

yaml
testclass: coap
ref_name: coap_secure
config:
  uri: 'coaps://192.168.1.100:5684'
  dtls_enabled: true
  confirmable: true
  timeout: 15000

📊 各方法 YAML 配置示例 ​

GET - 获取资源 ​

yaml
title: 获取设备温度
testclass: coap
config:
  uri: 'coap://192.168.1.100:5683/sensor/temperature'
  method: GET
  accept: 'application/json'
  confirmable: true
  timeout: 10000

POST - 创建资源 ​

yaml
title: 注册新设备
testclass: coap
config:
  uri: 'coap://192.168.1.100:5683/devices'
  method: POST
  content_format: 'application/json'
  payload: '{"deviceId":"sensor-003","type":"humidity","location":"room-201"}'
  confirmable: true

PUT - 更新资源 ​

yaml
title: 更新设备配置
testclass: coap
config:
  uri: 'coap://192.168.1.100:5683/device/sensor-001/config'
  method: PUT
  content_format: 'application/json'
  payload: '{"reportInterval":30,"threshold":28.0}'
  confirmable: true

DELETE - 删除资源 ​

yaml
title: 删除设备注册
testclass: coap
config:
  uri: 'coap://192.168.1.100:5683/devices/sensor-003'
  method: DELETE
  confirmable: true

Observe - 观察资源变化 ​

yaml
title: 观察温度变化
testclass: coap
config:
  uri: 'coap://192.168.1.100:5683/sensor/temperature'
  method: GET
  observe: true
  timeout: 30000
  accept: 'application/json'

Non-confirmable 消息 ​

yaml
title: 上报环境数据(NON消息)
testclass: coap
config:
  uri: 'coap://192.168.1.100:5683/sensor/environment'
  method: POST
  payload: '{"temperature":25.6,"humidity":65}'
  confirmable: false  # 不需要确认,适合周期性数据
  timeout: 5000

📘 消息类型对照 ​

类型标识可靠性适用场景
ConfirmableCON可靠设备控制、配置更新
Non-confirmableNON不可靠周期性数据上报

📘 CoAP 响应码对照表 ​

响应码含义对应 HTTP说明
2.01Created201资源创建成功
2.02Deleted200资源删除成功
2.03Valid304资源未修改
2.04Changed200资源更新成功
2.05Content200返回资源内容
4.00Bad Request400请求格式错误
4.01Unauthorized401未授权
4.03Forbidden403禁止访问
4.04Not Found404资源不存在
4.05Method Not Allowed405方法不允许
4.15Unsupported Format415不支持的内容格式
5.00Internal Server Error500服务器内部错误
5.03Service Unavailable503服务不可用

📋 完整 YAML 配置示例 ​

yaml
# CoAP设备管理完整测试
title: CoAP智能设备管理测试套件
configure_elements:
  - testclass: coap
    ref_name: coap_default
    config:
      uri: 'coap://192.168.1.100:5683'
      confirmable: true
      timeout: 10000
      content_format: 'application/json'

preprocessors:
  - testclass: coap
    config:
      uri: 'coap://192.168.1.100:5683/device/init'
      method: POST
      payload: '{"action":"reset"}'
      confirmable: true

children:
  - title: 获取设备列表
    testclass: coap
    config:
      uri: 'coap://192.168.1.100:5683/devices'
      method: GET
      accept: 'application/json'

  - title: 注册温度传感器
    testclass: coap
    config:
      uri: 'coap://192.168.1.100:5683/devices'
      method: POST
      content_format: 'application/json'
      payload: '{"deviceId":"temp-001","name":"温度传感器","location":"room-101"}'

  - title: 获取传感器数据
    testclass: coap
    config:
      uri: 'coap://192.168.1.100:5683/sensor/temp-001/value'
      method: GET
      accept: 'application/json'
      confirmable: true

  - title: 更新传感器配置
    testclass: coap
    config:
      uri: 'coap://192.168.1.100:5683/device/temp-001/config'
      method: PUT
      content_format: 'application/json'
      payload: '{"reportInterval":15,"unit":"seconds"}'

  - title: 观察传感器数据变化
    testclass: coap
    config:
      uri: 'coap://192.168.1.100:5683/sensor/temp-001/value'
      method: GET
      observe: true
      timeout: 30000

  - title: 删除传感器
    testclass: coap
    config:
      uri: 'coap://192.168.1.100:5683/devices/temp-001'
      method: DELETE
      confirmable: true

postprocessors:
  - testclass: coap
    config:
      uri: 'coap://192.168.1.100:5683/device/cleanup'
      method: DELETE
      confirmable: true

❓ 常见问题 ​

  1. 连接超时:检查设备是否在线,网络是否连通,可增大 timeout 值
  2. 资源未找到(4.04):确认 URI 路径是否正确拼写
  3. 方法不允许(4.05):该资源不支持当前请求方法,换用其他方法
  4. Observe 无通知:确认服务端支持 Observe 扩展,且资源确实有变化
  5. DTLS 握手失败:确认使用 coaps:// 协议和端口 5684
  6. 内容格式不匹配:检查 content_format 和 accept 是否与服务端一致

📚 相关文档 ​


💡 提示: CoAP URI 格式为 coap://host:port/path,安全连接使用 coaps://host:port/path。默认端口 5683 可省略。

Released under the MIT License.