【问题标题】:Dredd passing trailing square bracket to APIDredd 将尾随方括号传递给 API
【发布时间】:2015-10-24 17:04:48
【问题描述】:

我正在使用 Dredd 测试我编写的 API。在我尝试改变资源中的操作 uri 之前,它工作正常。当我有表单的动作时

## Retrieve Task [GET /task/{id}]

它向 Drakov 发送一个附加了 ] 的请求。此 Drakov 服务器正在运行蓝图文档。

Drakov 0.1.16      Listening on port 8090
[LOG] GET /task/myid]
[LOG] DELETE /task/myid]
[LOG] GET /task/myid]

你可以看到这个请求最后多了一个]

这是我的蓝图。它是来自Api Blueprint examples 的示例的子集:

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"
        }

我做错了什么?

【问题讨论】:

    标签: apiblueprint dredd


    【解决方案1】:

    您的 API 蓝图有多个错误。例如,

    + Parameters
      + status  `test` (string)
      + priority `1` (number)
    

    ...应该是:

    + Parameters
      + status: `test` (string)
      + priority: `1` (number)
    

    此外,您正在使用 URI 模板 /tasks/tasks{?status,priority} 定义资源 Tasks,然后您尝试使用不同的 URI 模板为资源定义 GET 操作。这令人困惑。

    我尝试创建一个示例 API 蓝图(另存为 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"
            }
    

    然后我在一个终端中启动了一个 Drakov 服务器,如下所示:

    drakov -f *.md
    

    然后我尝试运行 Dredd:

    dredd sample-blueprint.md http://localhost:3000
    

    一切都正确传递:

    $ 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
    

    这是您最初想要实现的目标吗?

    【讨论】:

    • 谢谢。我应该发现错字了。您提到的资源模板混淆出现在示例中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-12
    • 2012-01-10
    • 1970-01-01
    • 2016-01-25
    • 1970-01-01
    • 2020-09-12
    相关资源
    最近更新 更多