Package: play-requests
play requestspytest-play plugin driving the famous Pythonrequestslibrary for makingHTTPcalls.More info and examples on:pytest-play, documentationcookiecutter-qa, seepytest-playin action with a working example if you want to start hackingFeaturesThispytest-playcommand provider let you drive a
PythonrequestsHTTP library using a YAML configuration file
containing a set ofpytest-playcommands.you can see apytest-playscript powered by a command provided
by theplay_requestsplugin:- 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.5The above example:performs a GET call tohttps://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 valueplay_requestssupports all the HTTP verbs supported by therequestslibrary:OPTIONSHEADGETPOSTPUTPATCHDELETEYou’ll find otherplay_requestscommand examples in the following sections.Authentication cookiesManages cookies and prepare them for you so you don’t have to create
cookie headers by yourself using theauth=('username','password'):- provider: play_requests
  type: GET
  url: http://something/authenticated
  parameters:
    auth:
      - username
      - passwordUpload filesPost a csv file:- provider: play_requests
  type: POST
  url: http://something/1
  parameters:
    files:
      filecsv:
      - report.csv
      - some,dataPost 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.csvassuming that you have a$base_pathvariable.Save the response to a variableYou can save a response elaboration to apytest-playvariable
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.5It the endpoint returns a non JSON response, useresponse.textinstead.Default payloadIf all your requests have a common payload it might be annoying
but thanks toplay_requestsyou can avoid repetitions.You can set variables in many ways programatically using thepytest-playexecute command or execute commands. You can also update variables using
theplay_pythonexeccommand:- 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 aplay_requestscommand provides any other header value, the resulting HTTP call will be performed
with merged header values (eg:Authorization+Host)if aplay_requestscommand provides a conflicting header value or any other default option,
theAuthorizationheader provided by the command will win and it will override just for the current
call the default conflicting header valueAssert 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: barof if you want you can use the expressionresponse.raise_for_status()instead of
checking the exact match of status code.Theraise_for_statuscall will raise anHTTPErrorif theHTTPrequest
returned an unsuccessful status code.Post raw dataIf 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"    }'RedirectionsBy defaultrequestswill perform location redirection for all verbs
except HEAD:http://docs.python-requests.org/en/master/user/quickstart/#redirection-and-historyYou can disable or enable redirects playing with theallow_redirectsoption:- 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: barTwitterpytest-playtweets happens here:@davidemoroCreditsThis package was created withCookiecutterand thecookiecutter-play-plugin(based onaudreyr/cookiecutter-pypackageproject template).CHANGES0.0.5 (2019-04-08)Manages cookies and prepare them for you so you don’t have to create cookie
headers by yourself using theauth=('username','password')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’sskip_condition)documentation improvements0.0.2 (2018-01-16)Refactor internal methodsRemove merge commands with default payload (already
implemented inpytest-play>= 1.1.0)0.0.1 (2018-01-10)First release