Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disconnect event not received when using TestClient #1247

Closed
2 tasks done
AGhost-7 opened this issue Jul 20, 2021 · 3 comments
Closed
2 tasks done

Disconnect event not received when using TestClient #1247

AGhost-7 opened this issue Jul 20, 2021 · 3 comments
Labels
testclient TestClient-related

Comments

@AGhost-7
Copy link

AGhost-7 commented Jul 20, 2021

Checklist

  • The bug is reproducible against the latest release and/or master.
  • There are no similar issues or pull requests to fix it yet.

Describe the bug

When using the TestClient provided by the starlette framework, http.disconnect are not received. This is a problem if trying to test streaming.

To reproduce

Implement simple app:

async def app(scope, receive, send):
    await send({
        'type': 'http.response.start',
        'status': 200,
    })
    while True:
        message = await receive()
        print('message', message)
        if message['type'] == 'http.disconnect':
            break

Write test using provided TestClient, which closes the connection during "processing":

from bug import app
from starlette.testclient import TestClient

client = TestClient(app)

def test_client():
    response = client.post('/', stream=True)
    print('response.status', response.status)
    response.close()

Expected behavior

The server behaviour should be the same if using TestClient or Uvicorn. If I run the application provided above with Uvicorn:

uvicorn bug:app

And write a test using the normal requests as follows:

import requests

def test_server():
    response = requests.post('http://localhost:8000/', stream=True)
    print('response.status', response.status)
    response.close()

The http.disconnect event is received. This should be the same for the TestClient

Actual behavior

No http.disconnect event is received by the app when using TestClient.

Debugging material

Environment

  • OS: Linux
  • Python version: 3.8.10
  • Starlette version: 0.16.0

Additional context

@JayH5 JayH5 added the testclient TestClient-related label Aug 16, 2021
@Kludex
Copy link
Sponsor Member

Kludex commented Nov 27, 2021

We should take care of this for #652 , I guess.

@Kludex
Copy link
Sponsor Member

Kludex commented May 9, 2022

I think this will always be a limitation of the TestClient. I think we should add a test case, and maybe add a few lines on the documentation on how to test this.

@Kludex
Copy link
Sponsor Member

Kludex commented Aug 17, 2022

A way to test an ASGI application with http.disconnect without the TestClient: #1727

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
testclient TestClient-related
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants