play requests
pytest-play plugin driving the famous Python requests library for making HTTP calls.
More info and examples on:
pytest-play, documentation
cookiecutter-qa, see pytest-play in action with a working example if you want to start hacking
Features
This pytest-play command provider let you drive a Python requests HTTP library using a YAML configuration file containing a set of pytest-play commands.
you can see a pytest-play script powered by a command provided by the play_requests plugin:
- provider: play_requests
type: GET
assertion: "'pytest-play' in response.json()"
url: https://www.google.it/complete/search
parameters:
headers:
Host: www.google.it
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:57.0) Gecko/20100101
Firefox/57.0
Accept: "*/*"
Accept-Language: en-US,en;q=0.5
Referer: https://www.google.it/
Connection: keep-alive
params:
- - client
- psy-ab
- - hl
- it
- - gs_rn
- '64'
- - gs_ri
- psy-ab
- - gs_mss
- pytest-
- - cp
- '11'
- - gs_id
- '172'
- - q
- pytest-play
- - xhr
- t
timeout: 2.5
The above example:
performs a GET call to https://www.google.it/complete/search?client=psy-ab&hl=it&… with the provided headers, a timeout (if it takes more than 2.5 seconds a timeout exception will be raised) and an assertion expression that verifies that the response meets the expected value
play_requests supports all the HTTP verbs supported by the requests library:
OPTIONS
HEAD
GET
POST
PUT
PATCH
DELETE
NOTES: cookies and auth implementations supported by requests are not yet implemented because this package is still under development.
You’ll find other play_requests command examples in the following sections.
Upload files
Post a csv file:
- provider: play_requests
type: POST
url: http://something/1
parameters:
files:
filecsv:
- report.csv
- some,data
Post a csv file with custom headers:
- provider: play_requests
type: POST
url: http://something/1
parameters:
files:
filecsv:
- report.csv
- some,data
- application/csv
- Expires: '0'
Post a file providing the path:
- provider: play_requests
type: POST
url: http://something/1
parameters:
files:
filecsv:
- file.csv
- path:$base_path/file.csv
assuming that you have a $base_path variable.
Save the response to a variable
You can save a response elaboration to a pytest-play variable and reuse in the following commands:
- provider: play_requests
type: POST
url: http://something/1
variable: myvar
variable_expression: response.json()
assertion: variables['myvar']['status'] == 'ok'
parameters:
json:
foo: bar
timeout: 2.5
It the endpoint returns a non JSON response, use response.text instead.
Default payload
If all your requests have a common payload it might be annoying but thanks to play_requests you can avoid repetitions.
You can set variables in many ways programatically using the pytest-play execute command or execute commands. You can also update variables using the play_python exec command:
- provider: python
type: store_variable
name: bearer
expression: "'BEARER'"
- provider: python
type: store_variable
name: play_requests
expression: "{'parameters': {'headers': {'Authorization': '$bearer'}}}"
- provider: play_requests
type: GET
url: "$base_url"
and all the following HTTP calls will be performed with the authorization bearer provided in the default payload.
Merging rules:
if a play_requests command provides any other header value, the resulting HTTP call will be performed with merged header values (eg: Authorization + Host)
if a play_requests command provides a conflicting header value or any other default option, the Authorization header provided by the command will win and it will override just for the current call the default conflicting header value
Assert response status code
- provider: play_requests
type: POST
url: http://something/1
variable: myvar
variable_expression: response.json()
assertion: response.status_code == 200
parameters:
json:
foo: bar
of if you want you can use the expression response.raise_for_status() instead of checking the exact match of status code.
The raise_for_status call will raise an HTTPError if the HTTP request returned an unsuccessful status code.
Post raw data
If you want to send some POST data or send a JSON payload without automatic JSON encoding:
- provider: play_requests
type: POST
url: http://something/1
parameters:
data: '{"foo" : "bar" }'
Redirections
By default requests will perform location redirection for all verbs except HEAD:
You can disable or enable redirects playing with the allow_redirects option:
- provider: play_requests
type: POST
url: http://something/1
variable: myvar
variable_expression: response.json()
assertion: response.status_code == 200
parameters:
allow_redirects: false
json:
foo: bar
pytest-play tweets happens here:
Credits
This package was created with Cookiecutter and the cookiecutter-play-plugin (based on audreyr/cookiecutter-pypackage project template).
CHANGES
0.0.4 (2019-01-25)
Supports new pytest-play >= 2.0 YAML based syntax (json no more supported)
0.0.3 (2018-01-22)
remove condition option (already implemented by pytest-play’s skip_condition)
documentation improvements
0.0.2 (2018-01-16)
Refactor internal methods
Remove merge commands with default payload (already implemented in pytest-play >= 1.1.0)
0.0.1 (2018-01-10)
First release
Release files for play-requests 0.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| play_requests-0.0.4.tar.gz | 20.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| play_requests-0.0.4-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
Total release size: 30.1 kB
Release files / play_requests-0.0.4.tar.gz
| Download URL | play_requests-0.0.4.tar.gz |
|---|---|
| Size | 20.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c4387dc43df0944624aa89e3b956a1e305414e53ca0863c7105c3f7f7bca4874
|
|
BLAKE2b-256 checksum How to use checksums |
2cf1b308d7f8913554c43562d7accf9f6883f2e1cf371876d9603de5eff284e2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.0 CPython/3.6.7
|
Release files / play_requests-0.0.4-py2.py3-none-any.whl
| Download URL | play_requests-0.0.4-py2.py3-none-any.whl |
|---|---|
| Size | 9.9 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
27f6eb64345c30dab0cf2a6d3213e20516c9e33ff82e2078438dee9c9817a18d
|
|
BLAKE2b-256 checksum How to use checksums |
6727b44b0592cac007c83eef5dcdd3206185afe9720b3f4d5147faec601b53bc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.0 CPython/3.6.7
|