1

是否可以在 API 蓝图中为给定端点定义一组可能的响应?

例如,如果我有一个端点,例如 /movie/{id} 我希望能够定义一组电影记录,以便在模拟服务器中我可以 GET /movie/1 或 GET /movie/2 或 GET /movie/3 并获取相关记录。

我见过的所有例子似乎都只定义了一种可能的反应。

4

3 回答 3

1

您可以添加多个请求块,如下所示:

### Register [POST]
Registers an account

+ Request Already existing username

    + Body

            {
                "app": 3,
                "username": "already-existing-username",
                "password": "password"
            }

+ Response 200 (application/json)

    + Body

            {
                "success": false,
                "error": "The username specified is already registered to an account.",
                "error_field": "username"
            }

+ Request Invalid password

    + Body

            {
                "app": 3,
                "username": "username",
                "password": "password"
            }

+ Response 200 (application/json)

    + Body

            {
                "success": false,
                "error": "Please supply a valid password.",
                "error_field": "password"
            }

也可以在官方文档中找到

于 2017-08-12T13:19:08.933 回答
0

使用单个操作无法模拟此操作,但有一种解决方法。

FORMAT: 1A

# Multi

## GET /movie/1

+ Response 200 (application/json)

        { "id": 1, "title": "First" }

## GET /movie/2

+ Response 200 (application/json)

        { "id": 2, "title": "Second" }

## GET /movie/3

+ Response 200 (application/json)

        { "id": 3, "title": "Third" }

## GET /movie/{id}

+ Parameters
    + id (required, number, `42`) ... Blah.

+ Response 200 (application/json)

        { "id": 42, "title": "First" }

现在,如果您点击/movie/2,模拟服务器会发送适当的响应。谢谢。

于 2014-11-19T11:42:27.353 回答
0

这就是您使用 Mulesoft(API 设计器)在 RAML 文件中提供多个响应的方式,但是如果您使用模拟服务进行测试,您将始终获得您为测试设置的示例响应

/{id}:
    get:
      headers:
       Requester-Id:
        required: true
      responses:
        200:
          body:
            application/json:
              type: Account
              example:
                !include exapmle/AccountExample.raml

        400:
          body:
            application/json:
              example:
                {"message":"Error retrieving account for the provided id"}   
于 2019-11-26T07:25:59.917 回答