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

Testing a tornado app which does a redirect #11

Open
pelson opened this issue Mar 25, 2021 · 1 comment
Open

Testing a tornado app which does a redirect #11

pelson opened this issue Mar 25, 2021 · 1 comment

Comments

@pelson
Copy link

pelson commented Mar 25, 2021

I wanted to test that a redirect was setup correctly, so I went for something like:

import pytest
import tornado.web

class RedirecterHandler(tornado.web.RequestHandler):
    async def get(self):
        self.redirect('/')

class MainHandler(tornado.web.RequestHandler):
    async def get(self):
        self.finish('Welcome!')

@pytest.fixture
def app():
    return tornado.web.Application([
        (r"/", MainHandler),
        (r"/redirect_me", RedirecterHandler),
    ])

async def test_http_server_client(http_server_client):
    resp = await http_server_client.fetch('/redirect_me')
    assert resp.code == 302

But unfortunately the fetch call never returns. I suspect there is a deadlock somewhere.

For what it is worth, my workaround is to do something like:

    resp = await http_server_client.fetch('/redirect_me', max_redirects=0, raise_error=False)
    assert resp.code == 302
    assert resp.headers['Location'] == "/"
@Joshix-1
Copy link

Joshix-1 commented Mar 10, 2022

Can confirm, the following doesn't work:

async def test_http_server_client_fetch(http_server_client):
    resp = await http_server_client.fetch("/")
    assert resp.code == 200
    assert resp.body.decode("utf8") == MESSAGE

    resp = await http_server_client.fetch("/redirect")
    assert resp.effective_url == http_server_client.get_url("/")
    assert resp.code == 200
    assert resp.body.decode("utf8") == MESSAGE

But, the following works:

async def test_http_client_fetch(http_client, http_server, http_server_port):
   url = "http://localhost:%s/" % http_server_port[1]
   resp = await http_client.fetch(url)
   assert resp.code == 200
   assert resp.body.decode("utf8") == MESSAGE

   url = "http://localhost:%s/redirect" % http_server_port[1]
   resp = await http_client.fetch(url)
   assert resp.effective_url == "http://localhost:%s/" % http_server_port[1]
   assert resp.code == 200
   assert resp.body.decode("utf8") == MESSAGE

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

No branches or pull requests

2 participants