我正在使用Dredd来测试我编写的API.它工作正常,直到我尝试改变资源中的动作uri.当我有一个表格的动作
## Retrieve Task [GET /task/{id}]
Run Code Online (Sandbox Code Playgroud)
它向]附加的Drakov发送请求.这个Drakov服务器正在运行蓝图文档.
Drakov 0.1.16 Listening on port 8090
[LOG] GET /task/myid]
[LOG] DELETE /task/myid]
[LOG] GET /task/myid]
Run Code Online (Sandbox Code Playgroud)
你可以看到这个请求最后有一个额外]的.
这是我的蓝图.它是Api Blueprint示例中示例的一个子集:
FORMAT: 1A
# Advanced Action API
A resource action is – in fact – a state transition. This API example demonstrates an action - state transition - to another resource.
## API Blueprint
# Tasks [/tasks/tasks{?status,priority}]
+ Parameters
+ status `test` (string)
+ priority `1` (number)
## Retrieve Task [GET /task/{id}]
This is a state transition to another resource
+ Parameters
+ id: `myid` (string)
+ Response 200 (application/json)
{
"id": 123,
"name": "Go to gym",
"done": false,
"type": "task"
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
您的API蓝图有多个错误.例如,
+ Parameters
+ status `test` (string)
+ priority `1` (number)
Run Code Online (Sandbox Code Playgroud)
...应该:
+ Parameters
+ status: `test` (string)
+ priority: `1` (number)
Run Code Online (Sandbox Code Playgroud)
此外,您正在Tasks使用URI模板定义资源,/tasks/tasks{?status,priority}然后您尝试GET使用不同的URI模板为资源定义操作.这令人困惑.
我尝试创建一个示例API Blueprint(保存为sample-blueprint.md),如下所示:
FORMAT: 1A
# My API
## Task [/task/{id}]
### Retrieve Task [GET]
+ Parameters
+ id: `123` (string)
+ Response 200 (application/json)
{
"id": 123,
"name": "Go to gym",
"done": false,
"type": "task"
}
Run Code Online (Sandbox Code Playgroud)
然后我在一个终端中启动了一个Drakov服务器,如下所示:
drakov -f *.md
Run Code Online (Sandbox Code Playgroud)
然后我试着运行Dredd:
dredd sample-blueprint.md http://localhost:3000
Run Code Online (Sandbox Code Playgroud)
一切都正确通过:
$ dredd sample-blueprint.md http://localhost:3000
info: Beginning Dredd testing...
pass: GET /task/123 duration: 42ms
complete: 1 passing, 0 failing, 0 errors, 0 skipped, 1 total
complete: Tests took 50ms
Run Code Online (Sandbox Code Playgroud)
这是你原本想要实现的吗?
| 归档时间: |
|
| 查看次数: |
112 次 |
| 最近记录: |