ulff/behat-rest-api-extension

用于测试REST API调用的Behat上下文

安装: 52,110

依赖项: 0

建议者: 0

安全性: 0

星级: 10

关注者: 3

分支: 1

开放问题: 1

类型:behat-extension

1.3.0 2017-06-13 20:03 UTC

README

用于测试REST API响应的Behat上下文,扩展了MinkContext。目前仅支持JSON响应类型。使用此扩展,您可以向REST API发出HTTP调用并严格检查响应状态码和内容。

设置

步骤1:安装扩展

使用composer安装扩展

php composer.phar require "ulff/behat-rest-api-extension:^1.0"

步骤2:加载扩展

将以下内容添加到behat.yml

default:
  extensions:
    Ulff\BehatRestApiExtension\ServiceContainer\BehatRestApiExtension: ~

用法

创建自己的上下文类作为基类RestApiContext的扩展

use Ulff\BehatRestApiExtension\Context\RestApiContext;

class YourContext extends RestApiContext
{
    // ...
}

通过命令列出可用的场景步骤

behat -di

扩展提供的额外步骤

当我发出请求 :method :uri

指定http方法和uri发出请求。

示例

@When I make request "GET" "/api/v1/categories"
@When I make request "DELETE" "/api/v1/companies/{id}"
@When I make request "HEAD" "/api/v1/presentations/{id}"
当我发出请求 :method :uri,带有以下JSON内容

指定http方法、uri和参数作为JSON。

示例

@When I make request "POST" "/api/v1/posts" with following JSON content:
"""
{
    "user": "user-id",
    "title": "Some title"
    "number": 12
}
"""

@When I make request "PUT" "/api/v1/users/{id}" with following JSON content:
"""
{
    "education": [
        {
            "school": "A primary school",
            "address": "Some Street 10, SomeCity"
        },
        {
            "school": "High School",
            "address": "Another Street 1, SomeCity"
        }
    ],
    "workplace": {
        "name": "A company",
        "phone": "+48 111 222 333"
    }
}
"""
当我发出请求 :method :uri,带有参数

指定http方法、uri和参数作为TableNode。TableNode的值也可以是ParameterBag参数。

示例

@When I make request "POST" "/api/v1/posts" with params:
    | user      | user-id              |
    | title     | Some title           |
    | content   | Content here         |
@When I make request "PUT" "/api/v1/users/{id}" with params:
    | user  | user-id           |
    | name  | User Name Here    |
    | email | user@email.here   |
然后响应应该是JSON

检查响应是否为正确的JSON。

然后响应JSON应该是一个集合

检查响应JSON是否为集合(数组)。

然后响应JSON集合不应该为空

检查响应JSON集合(数组)不为空。

然后响应JSON集合应该为空

检查响应JSON集合(数组)为空。

然后响应JSON应该是一个单个对象

检查响应JSON是否为单个对象,不是一个集合(数组)。

然后响应JSON应该有 :property 字段

检查响应JSON对象是否有给定名称的属性。

示例

@Then the response JSON should have "id" field
然后响应JSON应该有 :property 字段,其值为 :expectedValue

检查响应JSON对象是否有给定名称的属性,并且该属性具有期望值。

示例

@Then the response JSON should have "name" field with value "User name"
@Then the response JSON should have "email" field with value "user@email.com"
然后响应JSON应该有 :property 字段,其值为 null

检查响应JSON对象是否有给定名称的属性,并且该属性具有 null 值。

示例

@Then the response JSON should have "end_date" field with null value"
@Then the response JSON should have "participants" field with null value
然后响应JSON应该有 :property 字段,其值精确为 :expectedValue

检查响应JSON对象是否有给定名称的属性,并且该属性具有期望的精确值(包括类型)。

示例

@Then the response JSON should have "name" field with exact value "User name"
@Then the response JSON should have "email" field with exact value "user@email.com"
然后响应JSON应该有 :property 字段,其值类似于 :expectedValueRegexp

检查响应JSON对象是否有给定名称的属性,并且其值与给定的正则表达式匹配。

示例

@Then the response JSON should have "error" field with value like "Missing param: [a-z]+"
@Then the response JSON should have "zipcode" field with value like "[0-9]{2}-[0-9]{3}"
然后响应JSON应该有 :property 字段设置为 :expectedValue

检查响应JSON对象是否有给定名称的属性,并且该属性具有期望的 BOOLEAN 值。

示例

@Then the response JSON should have "has_access" field set to "false"
@Then the response JSON should have "is_valid" field set to "true"
然后响应JSON应该有 :property 字段,其值为数组 :expectedArray

当响应JSON是单个对象时,它检查该对象是否有给定名称的属性,并且该属性是精确的数组

示例

@Then the response JSON should have "colors" field with array "['red', 'green', 'blue']" as value
@Then the response JSON should have "options" field with array "array('one', 'two')" as value
然后所有响应集合项都应该有 :property 字段

当响应JSON是集合(数组)时,它检查所有集合项都具有给定名称的属性

示例

@Then all response collection items should have "id" field
然后所有响应集合项都应该有 :property 字段,其值为 :expectedValue

当响应JSON是集合(数组)时,它检查所有集合项都具有给定名称的属性,并且这些属性具有期望的值。

示例

@Then all response collection items should have "default" field with value "1"
@Then all response collection items should have "color" field with value "red"
然后所有响应收集项都应该有 :property 字段,其值精确为 :expectedValue

当响应 JSON 是一个集合(数组)时,它检查所有集合项是否都具有给定名称的属性,并且这些属性具有预期的确切值(包括类型)。

示例

@Then all response collection items should have "default" field with exact value "1"
@Then all response collection items should have "color" field with exact value "red"
然后所有响应集合项都应该有嵌套字段 :property,其值为 :expectedValue

当响应 JSON 是一个集合(数组)时,它检查所有集合项是否都具有给定路径的嵌套属性,并且这些属性具有预期的值。对于嵌套属性,在预期的属性名称中使用 "->"。

示例

@Then all response collection items should have "owner->personal_data->name" field with value "John"
@Then all response collection items should have "root->property" field with value "1"
然后所有响应集合项都应该有嵌套字段 :property,其值为 :expectedValue

当响应 JSON 是一个集合(数组)时,它检查所有集合项是否都具有给定路径的嵌套属性,并且这些属性具有预期的确切值(包括类型)。对于嵌套属性,在预期的属性名称中使用 "->"。

示例

@Then all response collection items should have "owner->personal_data->name" field with exact value "John"
@Then all response collection items should have "root->property" field with exact value "1"
然后所有响应集合项的 :property 字段应设置为 :expectedBoolean

当响应 JSON 是一个集合(数组)时,它检查所有集合项是否都具有给定名称的属性,并且这些属性具有预期的 BOOLEAN 值。

示例

@Then all response collection items should have "is_default" field set to "true"
@Then all response collection items should have "has_access" field set to "false"
然后响应 JSON 的 :fieldName 字段应是一个集合

当响应 JSON 是一个单独的对象时,它检查该对象是否具有给定名称的属性,并且该属性是一个集合(数组)。

示例

@Then the response JSON "settings" field should be a collection
@Then the response JSON "allowed_colors" field should be a collection
然后所有嵌套的 :collectionFieldName 集合项都应该具有 :nestedFieldName 字段

当响应 JSON 是一个单独的对象时,它检查该对象是否具有给定名称的属性,该属性是集合(数组),并且所有该集合项都具有给定路径的嵌套字段。

示例

@Then all nested "owners" collection items should have "user" field
@Then all nested "themes" collection items should have "font" field
然后所有嵌套的 :collectionFieldName 集合项都应该有 :nestedFieldName 字段设置为 :expectedValue

当响应 JSON 是一个单独的对象时,它检查该对象是否具有给定名称的属性,该属性是集合(数组),并且所有该集合项都具有给定路径的嵌套字段和给定的 BOOLEAN 值。

示例

@Then all nested "owners" collection items should have "has_access" field set to "false"
@Then all nested "themes" collection items should have "is_default" field set to "true"
然后所有嵌套的 :collectionFieldName 集合项都应该有 :nestedFieldName 字段,其值为 :expectedValue

当响应 JSON 是一个单独的对象时,它检查该对象是否具有给定名称的属性,该属性是集合(数组),并且所有该集合项都具有给定路径的嵌套字段和给定的值。

示例

@Then all nested "owners" collection items should have "user" field with value "John"
@Then all nested "themes" collection items should have "font" field with value "Verdana"
然后所有嵌套的 :collectionFieldName 集合项都应该有 :nestedFieldName 字段,其值为 :expectedValue

当响应 JSON 是一个单独的对象时,它检查该对象是否具有给定名称的属性,该属性是集合(数组),并且所有该集合项都具有给定路径的嵌套字段和给定的确切值(包括类型)。

示例

@Then all nested "owners" collection items should have "user" field with exact value "John"
@Then all nested "themes" collection items should have "font" field with exact value "Verdana"
然后所有嵌套的 :collectionFieldName 集合项都应该有嵌套的 :nestedFieldName 字段,其值为 :expectedValue

当响应 JSON 是一个单独的对象时,它检查该对象是否具有给定名称的属性,该属性是集合(数组),并且所有该集合项都具有给定路径的嵌套字段和给定的值。对于嵌套属性,在预期的属性名称中使用 "->"。

示例

@Then all nested "owners" collection items should have nested "user->name" field with value "John"
@Then all nested "themes" collection items should have nested "font->color" field with value "Red"
然后所有嵌套的 :collectionFieldName 集合项都应该有嵌套的 :nestedFieldName 字段,其值为 :expectedValue

当响应 JSON 是一个单独的对象时,它检查该对象是否具有给定名称的属性,该属性是集合(数组),并且所有该集合项都具有给定路径的嵌套字段和给定的确切值(包括类型)。对于嵌套属性,在预期的属性名称中使用 "->"。

示例

@Then all nested "owners" collection items should have nested "user->name" field with value "John"
@Then all nested "themes" collection items should have nested "font->color" field with value "Red"
然后恰好有一个嵌套的 :collectionFieldName 集合项应该有 :nestedFieldName 字段,其值为 :expectedValue

当响应 JSON 是一个单独的对象时,它检查该对象是否具有给定名称的属性,该属性是集合(数组),并且恰好有一个集合项具有给定路径的嵌套字段和给定的值。

示例

@Then exactly one nested "users" collection items should have "login" field with value "johny63"
@Then exactly one nested "members" collection items should have "position" field with value "leader"
然后至少有一个嵌套的 :collectionFieldName 集合项应该有 :nestedFieldName 字段,其值为 :expectedValue

当响应JSON是一个单个对象时,它会检查该对象是否具有给定名称的属性,并且该属性是一个集合(数组),并且至少有一个该集合项具有给定路径和给定值的嵌套字段。

示例

@Then at least one nested "users" collection items should have "firstname" field with value "John"
@Then at least one nested "members" collection items should have "position" field with value "worker"
然后响应JSON应该具有嵌套的:nestedFieldName字段,值为:expectedValue。

当响应JSON是一个单个对象时,它检查该对象是否具有给定路径和给定值的属性。对于嵌套属性,在预期属性名称中使用"->"。

示例

@Then the response JSON should have nested "recipient->phone_number" field with value "123456789"
然后响应JSON应该具有嵌套的:nestedFieldName字段,值为null。

当响应JSON是一个单个对象时,它检查该对象是否具有给定路径的null值属性。对于嵌套属性,在预期属性名称中使用"->"。

示例

@Then the response JSON should have nested "forever_alone->friends" field with null value
然后响应集合中应该有:expectedValue个项。

当响应JSON是一个集合(数组)时,它检查集合中项的数量。

示例

@Then the response collection should count "4" items
然后至少有一个集合项应该具有:fieldName字段,值为:expectedValue。

当响应JSON是一个集合(数组)时,它检查任何集合项是否具有给定值的字段。

示例

Then at least one of the collection items should have field "name" with value "abcdef"