Test Scripts
Dothttp provides flexibility of small tests in javascript/python in same file. writing tests are faily simple.
- javascript
- python (preferred)
#
Javascript#
MethodMethod | Parameters | Description |
---|---|---|
test | testName (string), func (function) | Creates a test with the name testName and body func. All tests are executed after the response handler script. |
assert | condition (boolean), message (string) | Checks that the specified condition is true; throws an exception otherwise. The optional message parameter serves as an exception message. |
log | text | Prints text to the response handler or test stdout and then terminates the line. func (function) |
#
PropertiesThe global properties storage, which is used for setting, retrieving, or removing properties.
In your HTTP requests, you can access a variable saved in client.global as variable_name by using the {{variable_name}} syntax.
Method | Parameters | Description |
---|---|---|
set | varName (string), varValue (string) | Saves the variable with the varName name to the storage and sets its value to varValue. |
get | varName (string) | Returns the value of the varName variable. |
isEmpty | Checks whether the global object has no variables defined. | |
clear | varName (string) | Removes the varName variable from the variables storage. |
clearAll | Removes all variables from the variables storage. |
For more information and examples visit jetbrains-client-reference we took inspiration from jetbrains-client reference.
#
HTTP Response referencePropert | Description |
---|---|
body (string or object) | Response content, which is a string or a JSON object if the response's content-type is application/json. |
headers (ResponseHeaders) | The response headers storage object. |
status (int) | Response status, for example, 200 or 404. |
contentType (ContentType) | The contentType object, which holds the data on the Content-Type response header value. |
#
HeadersThe ResponseHeaders object is used for retrieving the data about response headers' values.
Method | Parameters | Description |
---|---|---|
valueOf | headerName (string) | Retrieves the first value of the headerName response header or null if the headerName response header does not exist. |
valuesOf | headerName (string) | Retrieves the array containing all values of the headerName response header. Returns an empty array if the headerName response header does not exist. |
#
ExampleFor more examples visit https://www.jetbrains.com/help/idea/http-response-handling-examples.html
#
Python#
ExecutionExecution follows of request follows this order.
- Load flags
- Init Script execution
- Property resolution (default properties in script)
- Dynamic Property resoluiton (
$randomStr
...) - Headers resolution
- Auth resolution
- Body resolution
- certificate (if exists)
- request completion
- Pre request script
- Request execution
- test script
#
Availaible librariesPre-request scripts operate within a secure sandbox environment to prevent any potential harm. Below is a list of available method libraries:
- math
- hashlib
- faker
- unittest
- csv
- uuid
- base64
- datetime
- yaml
- json
- open
- urllib
- cryptography
- requests
- jsonschema
client
(dothttp client)request
(current executing request)headers
query
payload
data
(exists if postdata is text)json
(exists if postdata is json/dict)header
(exists if posted data content-type is defined)filename
(exists if postdata is to be read from file)files
(exists if postdata is multipart)set_data(<data>)
(method to update post data)set_json(<json>)
(method to update json data)set_filename(<filename>)
(method to update post file name)set_files(<files>)
(method to update multipart file data)
response
(available for test scripts)properties
(Python dictionary)set()
(to update a property)clear()
(to delete a property)clear_all()
(to clear all properties)
infile_properties
get()
(to get defined infile property, set will not update or used)
env_properties
get()
(to get properties enabled from env, set will not update or used)
#
Pre-request scriptPre request script runs before executing each request. Currently support is availabile only for python script. It opens up more possibilites.
Some of its uses
- Custom authentication
- Faking request data
- Some computation
#
InIt ScriptIn addition to PreRequest, there is also an Init script available. Init script/functions are executed prior to loading URL, payload, headers, or any similar components. This functionality proves beneficial when you need to establish variables before loading URL, payload, or headers.
#
WritingPre request scripts are nothing but methods prefixed with pre
defined in script section of dothttp request.
Init scripts are essentially methods prefixed with init
that are defined within the script section of a dothttp request. These scripts execute prior to any pre-request actions, allowing for the initialization of variables or setup procedures before the request is made.
#
Test scriptTest scripts are unit test cases which will be executed. There are two types of writing test scripts
- methods prefixed with
test
. it should not accept any arguments - classes extending
unittest.TestCase
.
#
Writing/example#
Complete Example#
Limitations- failing pre request fails execution of request
- pre request script should not accept any arguments
import
andfilesystem
access is completely blocked and fails pre request.- All assignment on
client
andlibraries
are blocked and fails pre request.