2017 © Pedro Peláez
 

behat-extension jsonapi-behat-extension

Behat extension for testing JSON APIs

image

kielabokkie/jsonapi-behat-extension

Behat extension for testing JSON APIs

  • Tuesday, November 14, 2017
  • by kielabokkie
  • Repository
  • 1 Watchers
  • 13 Stars
  • 5,735 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 5 Forks
  • 1 Open issues
  • 12 Versions
  • 8 % Grown

The README.md

JSON API Behat Extension

Author Packagist Version Software License, (*1)

The JSON API Behat Extension provides step definitions for common testing scenarios specific to JSON APIs. It comes with easy ways to handle authentication through OAuth., (*2)

Requirements

  • PHP >= 5.4
PHP Package Version
5.4 v3.0.0
8.x v4.0+

Installation

Recommended installation is by running the composer require command. This will install the latest stable version of this extension., (*3)

composer require kielabokkie/jsonapi-behat-extension --dev

Configuration

To use this extension you will have to add it under the extensions in your behat.yml file., (*4)

default:
    extensions:
        Kielabokkie\BehatJsonApi: ~

Default parameters

Out of the box this extension has the following default parameters:, (*5)

parameter value
base_url http://localhost:8000
oauth_uri /v1/oauth/token
oauth_client_id testclient
oauth_client_secret testsecret
oauth_use_bearer_token false
oauth_password_grant_requires_client_credentials false

You can overwrite any of these parameters in the behat.yml file as needed., (*6)

default:
    extensions:
        Kielabokkie\BehatJsonApi:
            base_url: http://api.yourapp.dev
            parameters:
                oauth:
                    uri: /v1/oauth/token
                    client_id: myClientId
                    client_secret: myClientSecret
                    use_bearer_token: true
                    password_grant_requires_client_credentials: true

Optional parameters

To avoid having to use OAuth to retrieve an access token for each API call you can also specify an optional access_token in the parameters:, (*7)

default:
    extensions:
        Kielabokkie\BehatJsonApi:
            parameters:
                access_token: 90dabed99acef998fd3e35280f2a0a3c30c00c8d

Usage

To use the step definitions provided by this extension just load the context class in your suites:, (*8)

default:
  suites:
    default:
      contexts:
        - Kielabokkie\BehatJsonApi\Context\JsonApiContext

You will then have access to the following step definitions:, (*9)

@Given I use the access token
@Given I use access token :token
@Given I oauth with :username and :password
@Given I oauth with :username and :password and scope :scope
@Given I oauth using the client credentials grant
@Given I oauth using the client credentials grant with scope :scope
@Given I oauth using the client credentials grant with :id and :secret
@Given I oauth using the client credentials grant with :id and :secret and scope :scope
@Given I add a :header header with the value :value
@Given I have the payload:
@When /^I request "(GET|PUT|PATCH|POST|DELETE) ([^"]*)"$/
@Then I get a :statuscode response
@Then scope into the :scope property
@Then scope into the first :scope element
@Then the structure matches:
@Then the :field property is an object
@Then the :field property is an array
@Then the :field property is an array with :count items
@Then the :field property is an empty array
@Then the :field property is an integer
@Then the :field property is a integer equaling/equalling :expected
@Then the :field property is a string
@Then the :field property is a string equaling/equalling :expected
@Then the :field property is a boolean
@Then the :field property is a boolean equaling/equalling :expected
@Then /^echo last request$/
@Then /^echo last response$

Note: The last two definitions are for debugging purposes and only show output when you use the pretty formatting option of Behat, i.e. ./vendor/bin/behat -f pretty., (*10)

To get a list of all available step definitions including examples you can run the following command:, (*11)

$ vendor/bin/behat -di

Override the base url

In some cases you might want to override the base url for a specific suite. Below is an example of a behat.yml file. Here the custom url http://hooks.yourapp.dev is passed to the FeatureContext under the hooks suite., (*12)

default:
    autoload:
        - %paths.base%/tests/Behat/features/bootstrap
    suites:
        api:
            paths:
                - %paths.base%/tests/Behat/features/api
            contexts:
                - Kielabokkie\BehatJsonApi\Context\JsonApiContext: ~
        hooks:
            paths:
                - %paths.base%/tests/Behat/features/hooks
            contexts:
                - Kielabokkie\BehatJsonApi\Context\JsonApiContext:
                    - http://hooks.yourapp.dev
    extensions:
        Kielabokkie\BehatJsonApi: ~

The Versions

14/11 2017

dev-laravel-support

dev-laravel-support

Behat extension for testing JSON APIs

  Sources   Download

MIT

The Requires

 

by Wouter Peschier

api test json rest behat context

13/09 2017

dev-master

9999999-dev

Behat extension for testing JSON APIs

  Sources   Download

MIT

The Requires

 

by Wouter Peschier

api test json rest behat context

13/09 2017

v2.0.1

2.0.1.0

Behat extension for testing JSON APIs

  Sources   Download

MIT

The Requires

 

by Wouter Peschier

api test json rest behat context

13/09 2017

v2.0.0

2.0.0.0

Behat extension for testing JSON APIs

  Sources   Download

MIT

The Requires

 

by Wouter Peschier

api test json rest behat context

25/03 2017

v1.0.4

1.0.4.0

Behat extension for testing JSON APIs

  Sources   Download

MIT

The Requires

 

by Wouter Peschier

api test json rest behat context

25/03 2017

1.0.x-dev

1.0.9999999.9999999-dev

Behat extension for testing JSON APIs

  Sources   Download

MIT

The Requires

 

by Wouter Peschier

api test json rest behat context

20/09 2016

v1.0.3

1.0.3.0

Behat extension for testing JSON APIs

  Sources   Download

MIT

The Requires

 

by Wouter Peschier

api test json rest behat context

15/09 2016

v1.0.3-beta1

1.0.3.0-beta1

Behat extension for testing JSON APIs

  Sources   Download

MIT

The Requires

 

by Wouter Peschier

api test json rest behat context

15/09 2016

dev-develop

dev-develop

Behat extension for testing JSON APIs

  Sources   Download

MIT

The Requires

 

by Wouter Peschier

api test json rest behat context

09/08 2016

v1.0.2-beta1

1.0.2.0-beta1

Behat extension for testing JSON APIs

  Sources   Download

MIT

The Requires

 

by Wouter Peschier

api test json rest behat context

12/07 2015

v1.0.1-beta1

1.0.1.0-beta1

Behat extension for testing JSON APIs

  Sources   Download

MIT

The Requires

 

by Wouter Peschier

api test json rest behat context

07/07 2015

v1.0.0-beta1

1.0.0.0-beta1

Behat extension for testing JSON APIs

  Sources   Download

MIT

The Requires

 

by Wouter Peschier