Skip to content

aiohttp and raise_for_status, unexpected behaviours #635

@Parselon

Description

@Parselon

Hi!
I have encountered some strange behaviors when using the library, which I would like to fix.

1 Cassette isn't recording on status != 200:

  • If you perform a request with aiohttp, with flag raise_for_status=True, and the status code isn't 200, vcrpy will raise the aiohttp.ClientError without recording the cassette.

Steps to reproduce:

import aiohttp
import asyncio

import vcr


async def main():
    try:
        async with aiohttp.ClientSession(raise_for_status=True) as session:
            async with session.get('http://httpbin.org/post') as resp:  # httpbin will repond with a 405
                print("Status:", resp.status)
                print(await resp.text())
    except Exception as err:
        print("Error", err)


test_vcr = vcr.VCR()


with test_vcr.use_cassette('test3.yaml'):
    asyncio.run(main())
  • Result: Cassette was not created.
$ python test_aiohttp_raise_for_status.py 
Error 405, message='METHOD NOT ALLOWED', url=URL('http://httpbin.org/post')
$ cat test3.yaml
cat: test3.yaml: No existe el archivo o el directorio
  • Expected: The cassette should be created in order to be replicated later

2 raise_for_status is ignored if status in existing cassette is != 200:

  • If a cassette with the status different to 200 exists, raise_for_status is ignored:

Steps to reproduce:
Create a test3.yaml file with a response, and run the same script above.

interactions:
- request:
    body: null
    headers: {}
    method: GET
    uri: http://httpbin.org/post
  response:
    body:
      string: '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">

        <title>405 Method Not Allowed</title>

        <h1>Method Not Allowed</h1>

        <p>The method is not allowed for the requested URL.</p>

        '
    headers:
      Access-Control-Allow-Credentials:
      - 'true'
      Access-Control-Allow-Origin:
      - '*'
      Allow:
      - OPTIONS, POST
      Connection:
      - keep-alive
      Content-Length:
      - '178'
      Content-Type:
      - text/html
    status:
      code: 405
      message: METHOD NOT ALLOWED
    url: http://httpbin.org/post
version: 1
  • Results: raise_for_status is ignored, and no exception is catched (should print Error).
$ python test_aiohttp_raise_for_status.py 
Status: 405
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>405 Method Not Allowed</title>
<h1>Method Not Allowed</h1>
<p>The method is not allowed for the requested URL.</p>
  • Expected: except catches the ClientResponseError and print the error.

Summay

raise_for_status is not handled and causes several unexpected behaviours.
aiohttp client documentation

I will propose a PR soon.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions