Skip to main content

REST APIs

DrevOps\BehatSteps\Steps\Web\RestTrait - Source, Example, Helpers

Test REST APIs with lightweight steps.

  • Set HTTP headers for subsequent requests, literally or from a JSON path in the last response.
  • Send requests with any HTTP method (GET, POST, PUT, PATCH, DELETE), with a body written inline or read from a fixture file.
  • Assert response status codes, headers and body content.

Requests go through the rest Mink session, apart from the browser's: they carry no browser cookies, and the browser keeps its page. The JSON and XML steps read the same session.

The JSON path header steps require the optional softcreatr/jsonpath package: composer require --dev softcreatr/jsonpath.

Skip processing with tag: @behat-steps-skip:RestTrait.

Options​

OptionTypeDefaultTagDescription
rest.enabledbooleanTRUE@behat-steps-skip:RestTraitIf TRUE, clears the REST headers before each scenario. If FALSE, they aren't cleared, which changes nothing, because Behat builds a new context for each scenario and its headers start empty.

Steps​

@Given the REST header :name has the value :value

Set a REST header for subsequent requests

Given the REST header "Accept" has the value "application/json"
Given the REST header "Authorization" has the value "Bearer abc123"
@Given the REST header :name has the value from the JSON path :path

Set a REST header to the value at a JSON path in the last REST response

Given the REST header "X-Session-Id" has the value from the JSON path "$.session_id"
@Given the REST header :name has the value from the JSON path :path with the prefix :prefix

Set a REST header to a prefix and the value at a JSON path

Given the REST header "Authorization" has the value from the JSON path "$.access_token" with the prefix "Bearer "
@When I send a REST :method request to :url

Send a REST request to a URL

When I send a REST "GET" request to "/api/resource"
When I send a REST "DELETE" request to "/api/resource/1"
@When I send a REST :method request to :url with the body:

Send a REST request to a URL with a body

When I send a REST "POST" request to "/api/resource" with the body:
"""
{"name": "example"}
"""
@When I send a REST :method request to :url with the body from the file :filename

Send a REST request to a URL with a body read from a fixture file

When I send a REST "POST" request to "/api/import" with the body from the file "items.xml"
@When I print the last REST response

Print the last REST response

When I print the last REST response
@Then the REST response status code should be :code

Assert the REST response status code

Then the REST response status code should be 200
Then the REST response status code should be 404
@Then the REST response should contain :value

Assert the REST response contains text

Then the REST response should contain "success"
@Then the REST response should contain the header :name

Assert the REST response contains a header

Then the REST response should contain the header "Content-Type"
@Then the REST response should not contain the header :name

Assert the REST response does not contain a header

Then the REST response should not contain the header "Set-Cookie"
@Then the REST response header :name should contain :value

Assert a header of the REST response contains a value

Then the REST response header "Content-Type" should contain "application/json"
@Then the REST response header :name should not contain :value

Assert a header of the REST response does not contain a value

Then the REST response header "Cache-Control" should not contain "public"