Skip to content

Response

This module contains step implementations that handles Request responses.

Arguments

expression

Valid for: step_response_save_matches, step_response_save, step_response_validate

  • expected_matches int (optional): number of expected matches that expression should get, a value of -1 means Any number of matches (default: 1)

  • as_json bool (optional): always return matches as a JSON list, by default if there's a single match it will be returned as a string (default: False)

Then save matches optional

Then save optional response {target:ResponseTarget} "{expression}" that matches "{match_with}" in variable "{variable}" with default value "{default_value}"

[view_source]

Save specified parts of a response, either from meta data (header) or payload (body), in a variable.

With this step it is possible to change variable values and as such use values from a response later on in the load test.

The Request task preceded by this step will not fail if the specified expression has no or more than one match, the value of variable will be set to default_value.

Example:

# only token is matched and saved in TOKEN, by using regexp match groups
And value for variable "TOKEN" is "none"
Then save response metadata "$.Authentication" that matches "Bearer (.*)$" in variabel "TOKEN"

# the whole value is saved, as long as Authentication starts with "Bearer"
And value for variable "HEADER_AUTHENTICATION" is "none"
Then save response metadata "$.Authentication" that matches "^Bearer .*$" in variable "HEADER_AUTHENTICATION"

# only the numerical suffix is saved in the variable
And value for variable "AtomicIntegerIncrementer.measurermentId" is "1"
Then save response payload "$.measurement.id" that matches "^cpu([\d]+)$" in "measurementId"

# the whole value is saved, as long as the value starts with "cpu"
And value for variable "measurementId" is "0"
Then save response payload "$.measurement.id" that matches "^cpu[\d]+$" in "measurementId"

# xpath example
And value for variable "xmlMeasurementId" is "none"
Then save response payload "//measurement[0]/id/text() | content_type=xml" that matches "^cpu[\d]+$" in "xmlMeasurementId"

Arguments:

  • target ResponseTarget - metadata or payload, depending on which part of the response should be used
  • expression str - JSON path or XPath expression for finding the property
  • match_with str - static value or a regular expression
  • variable str - name of the already initialized variable to save the value in
  • default_value str - value to set if there is no match

Then save matches

Then save response {target:ResponseTarget} "{expression}" that matches "{match_with}" in variable "{variable}"

[view_source]

Save specified parts of a response, either from meta data (header) or payload (body), in a variable.

With this step it is possible to change variable values and as such use values from a response later on in the load test.

The Request task preceded by this step will fail if the specified expression has no or more than one match.

Example:

# only token is matched and saved in TOKEN, by using regexp match groups
And value for variable "TOKEN" is "none"
Then save response metadata "$.Authentication" that matches "Bearer (.*)$" in variabel "TOKEN"

# the whole value is saved, as long as Authentication starts with "Bearer"
And value for variable "HEADER_AUTHENTICATION" is "none"
Then save response metadata "$.Authentication" that matches "^Bearer .*$" in variable "HEADER_AUTHENTICATION"

# only the numerical suffix is saved in the variable
And value for variable "AtomicIntegerIncrementer.measurermentId" is "1"
Then save response payload "$.measurement.id" that matches "^cpu([\d]+)$" in "measurementId"

# the whole value is saved, as long as the value starts with "cpu"
And value for variable "measurementId" is "0"
Then save response payload "$.measurement.id" that matches "^cpu[\d]+$" in "measurementId"

# xpath example
And value for variable "xmlMeasurementId" is "none"
Then save response payload "//measurement[0]/id/text() | content_type=xml" that matches "^cpu[\d]+$" in "xmlMeasurementId"

Arguments:

  • target ResponseTarget - metadata or payload, depending on which part of the response should be used
  • expression str - JSON path or XPath expression for finding the property
  • match_with str - static value or a regular expression
  • variable str - name of the already initialized variable to save the value in

Then save optional

Then save optional response {target:ResponseTarget} "{expression}" in variable "{variable}" with default value "{default_value}"

[view_source]

Save metadata (header) or payload (body) value from a response in a variable.

This step expression is the same as step_response_save_matches_optional if match_with is set to .*.

With this step it is possible to change variable values and as such use values from a response later on in the load test.

The Request task preceded by this step will not fail if the specified expression has no or more than one match, the value of variable will be set to default_value.

Example:

Then save optional response metadata "$.Authentication" in variable "HEADER_AUTHENTICATION" with default value "none"

Then save optional response payload "$.Result.ShipmentId" in variable "ShipmentId" with default value "SH1395865"

Then save optional response payload "//measurement[0]/id/text()" in "xmlMeasurementId" with default value "foobar"

Arguments:

  • target ResponseTarget - metadata or payload, depending on which part of the response should be used
  • expression str - JSON path or XPath expression for finding the property
  • variable str - name of the already initialized variable to save the value in
  • default_value str - value to set if there is no match

Then save

Then save response {target:ResponseTarget} "{expression}" in variable "{variable}"

[view_source]

Save metadata (header) or payload (body) value from a response in a variable.

This step expression is the same as step_response_save_matches if match_with is set to .*.

With this step it is possible to change variable values and as such use values from a response later on in the load test.

The Request task preceded by this step will fail if the specified expression has no or more than one match.

Example:

Then save response metadata "$.Authentication" in variable "HEADER_AUTHENTICATION"

Then save response payload "$.Result.ShipmentId" in variable "ShipmentId"

Then save response payload "//measurement[0]/id/text()" in "xmlMeasurementId"

Arguments:

  • target ResponseTarget - metadata or payload, depending on which part of the response should be used
  • expression str - JSON path or XPath expression for finding the property
  • variable str - name of the already initialized variable to save the value in

When validate

When response {target:ResponseTarget} "{expression}" {condition:Condition} "{match_with}" fail request

[view_source]

Fail a request based on the value of a response meta data (header) or payload (body).

Example:

And restart scenario on failure
When response metadata "$.['content-type']" is not ".*application/json.*" fail request
When response metadata "$.['x-test-command']" is "abort" fail request
When response metadata "$.Authentication" is not "Bearer .*$" fail request

And stop user on failure
When response payload "$.measurement.id" is not "cpu[0-9]+" fail request
When response payload "$.success" is "false" fail request
When response payload "/root/measurement[@id="cpu"]/success/text()" is "'false'" fail request

Arguments:

  • target ResponseTarget - metadata or payload, depending on which part of the response should be used
  • expression str - JSON path or XPath expression for finding the property
  • condition enum - "is" or "is not" depending on negative or postive matching
  • match_with str - static value or a regular expression

Then allow status codes

Then allow response status codes "{status_list}"

[view_source]

Set allowed response status codes for the latest defined request in the scenario.

By default 200 is the only allowed respoonse status code. By prefixing a code with minus (-), it will be removed from the list of allowed response status codes.

Example:

Then get request with name "test-get-1" from endpoint "/api/test"
And allow response status "200,302"

Then get request with name "test-failed-get-2" from endpoint "/api/non-existing"
And allow response status "-200,404"

Arguments:

  • status_list str - comma separated list of integers

Then allow status codes table

Then allow response status codes

[view_source]

Set allowed response status codes for the latest defined requests based on a data table.

Specifies a comma separeated list of allowed return codes for the latest requests in a data table.

By default 200 is the only allowed respoonse status code. By prefixing a code with minus (-), it will be removed from the list of allowed response status codes.

Number of rows in the table specifies which of the latest defined requests the allowed response status codes should map to.

The table must have the column header status.

Example:

Then get request with name "test-get-1" from endpoint "/api/test"
Then get request with name "test-get-2" from endpoint "/api/test"
And allow response status
 | status   |
 | 200, 302 |
 | 200,404  |

Allowed response status codes for test-get-1 is now 200 and 302, and for test-get-2 is now 200 and 404.

Then content type

Then set response content type to "{content_type:ContentType}"

[view_source]

Set the content type of a response, instead of guessing it.

This is applicable when there is a step_response_validate or step_response_save is included in the scenario, and is valid only for the latest defined request.

Example:

And set response content type to "json"
And set response content type to "application/json"
And set response content type to "xml"
And set response content type to "application/xml"
And set response content type to "plain"
And set response content type to "text/plain"

Arguments:

  • content_type ContentType - expected content type of response